views:

59

answers:

1

Hello,

Does anyone know of any recent examples of how to create a simple COM component in C++ using Visual Studio 2008 so I can learn how to marshal different kinds of data between unmanaged and managed code, and back again. I've found a few tutorials that suggest a C++ ATL project, but project settings page no longer has the settings mentioned in the article.

What I want to do is implement a C++ DLL that would be used from C# like this:

MyComComponent myComp = new MyComComponent();
myComp.Register(new ClassWithCallbacks()) /* implements IMyCallback */

The COM component would, at timed intervals, call various methods on the provided IMyCallback instance to obtain information. Similarly, the C# code might call various methods directly, when needed.

This mechanism is to enable an application to be monitored either by (the C++ side) polling the C# side, or by the C# side reporting back to the C++ side.

+1  A: 

You can start from Walkthrough: Creating a COM Server Using Text Editor. It shows how to do it using COM attributes instead of an IDL, but for your purposes this should be sufficient.

If you want more ATL samples, the ATL General Samples section has bunch of samples that implement various COM features, both as in-proc or out-of-proc server, and using IDL instead of attributes. You can download any of them and tweak it to your purposes. I'd probably start with the BEEPER.

Meanwhile, Interoperating With Unmanaged Code has a lot of information about the managed side, especially when it comes to more advanced stuff like custom marshaling, events, etc.

Franci Penov