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
Solved

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

Posted on 2014-12-08
3
198 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
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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