Unmanaged C++ COM and Managed C++ .NET4 interop
I have an ATL COM service which I can connect to via my Windows Forms Application .NET4 application, written for the most part in managed c++.
I can successfully retrieve a pointer to the interface (via CoCreateInstance()), but run into issues when attempting to implement an event sink. I wish to create an unmanaged c++ class in the application which implements an interface defined by the COM server, and receive events from the ConnectionPoint. What I've got so far:
I can retrieve the IConnectionPoint pointer for the interface, but the call to Advise() returns E_NOTIMPL. This leads me to believe that i've missed implementing some ATL base functionality somewhere, but I'm not sure where to start.
As an aside, i've found very little resources on the net about using COM in a managed c++ project. The closest i've come to is this article. Does anybody have any good reading on this subject? It's mangling my brain at the moment.
In your managed C++ application add a reference to the COM server using "Add reference" option, this will create a managed wrapper (assembly) for your COM component which you will be able to consume in your "managed c++ code". The events in your COM component should be available as .NET events which can be handled using regular event handling in managed C++.
From your description it seems that you are trying to consume the COM component in your managed C++ application using unmanaged code, which is making things complicated. Mixing managed and unmanaged code should have clear boundaries and jumping too much in and out of these boundaries will make things complex and will lead to other serious issues. Hence I suggest you to consume the COM component using "managed code" (c++) only bu creating the managed wrapper for the COM component.
链接地址: http://www.djcxy.com/p/81312.html