?
Solved

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

Posted on 2014-12-08
3
Medium Priority
?
207 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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

800 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