Solved

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

Posted on 2014-12-08
3
181 Views
Last Modified: 2014-12-14
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
0
Comment
Question by:jxbma
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
Comment Utility
It really depends on the kind of methods you have in your classes. A COM class can contain FooA and FooB as members and the COM methods would simply delegate calls to the aggregated objects of FooA and FooB. This works for most of the cases, though when you have to pass say FooA to a method of FooB then things become a bit tricky with the COM interfaces. To illustrate the problem

IFooA {
  void DoSomethingWithFooB(IFooB foob)
}

Inside the implementation of this method there is often a tendency to get hold of the FooB object (though a violation of the SOLID principles) , given just the interface IFooB.

It probably makes more sense to wrap each class into a separate COM class unless there is a good reason. Even if you wrap inside the same COM object, you can keep a separate interface for the aggregated classes, for example

IFooA { methods of FooA }
IFooB { methods of FooB }

On a related note: If the intent is just to make classes available to .NET clients then an easier and elegant alternative is to use C++ CLI, wherein you can write .NET wrapper classes for each of the FooA and FooB. The compiler support makes it vanilla simple to mix native and managed code.
0
 
LVL 1

Author Comment

by:jxbma
Comment Utility
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?
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now