Solved

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

Posted on 2014-12-08
3
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 40491112
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
ID: 40491421
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
ID: 40492050
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 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