从非托管C ++调用托管代码(c#)的最佳方法

从非托管C ++调用托管代码(c#)的最佳方法

问题描述:

我们开发了一个由C#开发的对象组成的s / w架构。他们广泛使用事件来通知客户端状态更改等。

We have developed a s/w architecture consisting of set of objects developed in C#. They make extensive use of events to notify the client of changes in status, etc.

原意是允许旧代码通过COM互操作服务使用这些管理对象。这在设计规范中很容易写,但我看到实际实现它更有问题。我搜索了很多小时,寻找一个很好的事件处理示例使用这种方法。在我们走这条路之前,我想确保COM互操作是允许旧代码调用我们的新代码的最佳方式。

The intention originally was to allow legacy code to use these managed objects through COM interop services. That was easy to write in the design specification but, I'm seeing that actually implementing it is more problematic. I've searched for many hours looking for a good sample of event handling using this method. Before we march down that path, I want to make sure that COM interop is the best way to allow legacy code to call our new code.

它显示有几个不同的选项:1)COM interop,2)写非托管包装类3)使用/ clr编译器开关启用托管对象的调用,4 )使用某种反转pInvoke调用。我缺少任何?

It appears there are several different options: 1) COM interop, 2) write unmanaged wrapper classes 3) use the /clr compiler switch to enable calling of managed objects, 4) use some sort of reverse pInvoke call. Am I missing any?

每个选项都有它的好处,缺点和我想知道什么是最好的方法是。这里是每个的具体问题/意见

Each option will have its benefits & drawbacks and I'm wondering what the best way to go is. Here are specific questions/comments for each

COM INTEROP - 它似乎事件处理是一个障碍。我们使用具有变量类型的事件作为参数。事件参数可以具有事件ID和对象。基于事件ID,对象将是某种类型。这可以用COM互操作处理吗?许多暴露的对象都有属性。你不能在接口中声明属性,所以所有的属性都需要一个相应的get / set方法。

COM INTEROP - It appears event handling is a hurdle. We use events that have variable types as parameters. An event parameter may have an event ID and an object. Based on the event ID, the object will be of a certain type. Can this be handled with COM interop? Many of the objects that are exposed have properties. You can't declare properties in an interface so all properties will need a corresponding get/set method.

WRITE UNMANAGED WRAPPER - 我假设这意味着使用/ clr选项允许创建和调用受管对象并暴露非托管对象。这些客户端是不可管理的。我以前没有这样做过。这是什么是好处/缺点?

WRITE UNMANAGED WRAPPER - I assume this means creating a DLL using the /clr option to allow creating and calling managed objects and exposing unmanaged objects. Would the client of these unmanaged. I haven't done this before. What are benefits/drawbacks of this?

使用/ CLR开关 - 我理解这意味着添加对托管对象的支持。这种方法有什么缺点?此选项是否支持上述事件?我们可以说,这里是托管库。使用/ clr编译器选项与您的旧代码,并在它?我不知道这个的后果。是否有一个很好的示例如何工作? (我确定有,我只是没有找到它)

USE THE /CLR SWITCH - I understand this means to add support for managed objects. What are the drawbacks of this approach? Does this option support events as described above? Can we say, "here's the managed library. Use the /clr compiler option with your legacy code and have at it?" I don't know the ramifications of this. Is there a good sample of how this works around? (I'm sure there is, I just haven't found it)

使用反转PINVOKE - 我不知道这将如何工作,但从什么我已经能够找到,这不是一个可能有效的解决方案。

USE A REVERSE PINVOKE - I'm not sure exactly how this would work but, from what I've been able to find, this is not a likely valid solution.

那么,决策树是什么样子找到正确的方向?任何帮助是值得赞赏的。

So, what does the decision tree look like to find the correct direction? Any help is appreciated.


  • DP

我认为你最初的解决方案是最好的。 COM互操作是稳定的,并有相当好的文件。所有你需要做的是确保所有可能弹出给定的事件处理程序的不同的事件对象实现相同的COM可见基本事件对象接口(具有事件类型id,等)。从那里,单个对象可以实现任何其他所需的接口,并且您的非托管代码可以根据您要定义的任何标准,QI正确的细节界面。这真的不是那么难。有关端到端示例的代码项目文章,包括非托管事件处理程序。

I think your initial solution is the best one. COM interop is stable and reasonably well documented. All you need to do is ensure that all the different event objects that might pop out of a given event handler implement the same COM-visible base event object interface (that has the event type id, etc). From there, individual objects can implement whatever other interfaces they want, and your unmanaged code can QI for the right "detail" interface based on whatever criteria you want to define. It's really not that hard. Have a look at this CodeProject article for an end-to-end sample including unmanaged event handlers.