开发者

Unmanaged C++ COM and Managed C++ .NET4 interop

开发者 https://www.devze.com 2023-03-02 11:37 出处:网络
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 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:

  • Realising this isn't possible using a managed (ref class) c++ class, I'm using a normal unmanaged class.
  • Not being able to use any ATL macros,开发者_如何学编程 I need to implement the IUnknown abstract functions (AddRef, Release, QueryInterface etc) in the sink class.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号