非托管 C++ COM 和托管 C++ .NET4互操作

发布于 2024-11-05 02:53:12 字数 642 浏览 4 评论 0原文

我有一个 ATL COM 服务,可以通过我的 Windows 窗体应用程序 .NET4 应用程序连接到该服务,该应用程序大部分是用托管 c++ 编写的。

我可以成功检索指向接口的指针(通过 CoCreateInstance()),但在尝试实现事件接收器时遇到问题。我希望在应用程序中创建一个非托管 C++ 类,该类实现 COM 服务器定义的接口,并从 ConnectionPoint 接收事件。到目前为止我所得到的:

  • 意识到使用托管(引用类)c++ 类这是不可能的,我正在使用普通的非托管类。
  • 由于无法使用任何 ATL 宏,我需要在接收器类中实现 IUnknown 抽象函数(AddRef、Release、QueryInterface 等)。

我可以检索接口的 IConnectionPoint 指针,但对 Advise() 的调用返回 E_NOTIMPL。这让我相信我错过了在某个地方实现一些 ATL 基本功能,但我不知道从哪里开始。

顺便说一句,我在网上发现关于在托管 C++ 项目中使用 COM 的资源非常少。我最接近的是这篇文章。有人对这个主题有什么好的读物吗?此刻它正在破坏我的大脑。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-11-12 02:53:12

在您的托管 C++ 应用程序中,使用“添加引用”选项添加对 COM 服务器的引用,这将为您的 COM 组件创建一个托管包装器(程序集),您将能够在“托管 C++ 代码”中使用该包装器。 COM 组件中的事件应作为 .NET 事件提供,可以使用托管 C++ 中的常规事件处理来处理这些事件。

从您的描述来看,您似乎正在尝试使用非托管代码使用托管 C++ 应用程序中的 COM 组件,这使事情变得复杂。混合托管和非托管代码应该有明确的界限,过多地进出这些界限将使事情变得复杂,并导致其他严重问题。因此,我建议您仅使用“托管代码”(c++) 来使用 COM 组件,而不是为 COM 组件创建托管包装器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文