Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

How do I wrap several C++ unmanaged classes into a single COM Object?

This might seem like a silly question, but 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 {
  Method1;
  Method2;
}

class FooB {
  Method3;
  Method4;
}

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
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jxbma

ASKER

Thanks for the feed back.
Given the existing architecture, I'm stuck using unmanaged C++.
This COM object is to be consumed by other unmanaged C++ libraries.
My thought was to encapsulate the implementation through COM.
I could have always included the full blown integrated library (and all it's supporting libraries) directly
with the other libraries.

My idea was that the performance overhead of calling a COM object from an unmanaged C++ client
should be only marginally worse than including the library directly (I guess that this depends on the marshaling
of the parameters). Thoughts?
Hard to say anything about performance overhead, which could be from marginal to significant depending on the usage pattern among other things. Marshaling and Apartments would definitely be a lot slower than direct native calls.

In most cases a benchmark would help develop a sense of performance overhead.