如何在COM对象中包装多个非托管C ++类?

问题描述:

自从我进入微软COM世界以来,已经很长时间了。



我有几个课程(FooA和FooB) )这是静态非托管C ++库的一部分。



我想把这个库包装在一个COM中,而不是弄清楚头文件包含分辨率的噩梦宾语。这样做会使库可供C#(托管)客户使用。



COM对象的第一遍是单线程公寓(我们可能会切换到多线程以后)。



这是我有点生疏的地方,让我有点困惑。

我有FooA和FooB课程包含在库中。

FooA和FooB有我希望向客户公开的方法。



It's been a (long) while since I've been in the world of Microsoft COM.

I've got a couple of classes (FooA and FooB) which are part of a static unmanaged C++ library.

Rather than figuring out the nightmare of header file inclusion resolution, I'd like to wrap this library in a COM object. Doing so would make the library available for consumption by C# (managed) clients.

The 1st pass of the COM object is to be single thread apartment (we might switch to multi thread later).

This is where I'm a little rusty and get a little confused.
I have classes FooA and FooB contained in the library.
FooA and FooB have methods which I'd like to expose to clients.

class FooA {
  public Method1;
  public Method2;
}

class FooB {
  public Method3;
  public Method4;
}



如何通过COM接口公开这些类和方法的方法?

最终我需要用于引用该对象上的方法的类(对象)的实例化。

我是否需要返回并将ptr传递给COM对象上公开的接口的实例化对象?



很多我见过的例子很标准:加/加两个整数



有人可以流一些点亮这个对我来说也许是指向一个有用的例子?



谢谢,

JohnB


How do I expose those classes and methods their methods through a COM interface?
Ultimately I need an instantiation of the class (an object) to reference methods on that object.
Do I need to return and pass around a ptr to the instantiated object to interfaces exposed on the COM object?

A lot of the examples I've seen are pretty standard: add/multiply two integers

Can someone shed some light on this for me and perhaps point me towards a useful example?

Thanks,
JohnB

您可以通过创建一个包含整个方法的接口来实现该目标,并实现该接口并调用原始类中的方法。

You can achieve that goal by creating one interface that contains the whole of the methods and, a class that implement that interface and call the methods in the original classes.

但是,如果您的目标是公开C#客户端使用的本机代码,则可以使用P / Invoke或C ++ / CLI。您可以在我的CP文章中了解更多相关信息:将本机公开给托管 - C ++ / CLI与P / Invoke

But, if your goal is exposing a native code to be used by C# clients, you can use P/Invoke or C++/CLI instead. You can read more about it in my CP article about: Exposing native to managed - C++/CLI vs. P/Invoke.