Tips and Tricks with C++ COM and .NET

 If you have written a C++ COM dll and if you are looking to use it in a C# program, then you are in the right place! :) Read on...

Trust me, using a COM dll in a C# program is far simpler and relieves you of all those ref counting hazzles. Managed code even simplifies the standard COM function calls to QueryInterface, AddRef etc. Making a QueryInterface call is as simple as casting an object of type A to type B and you don't have to make any calls to AddRefs and Releases!

There are 3 simple steps to using your C++ COM dll in your C# program.  

Step 1: Generate a managed COM wrapper dll for your C++ COM dll (let us call it MyComDll.dll). Let us call the generated dll as MyManagedDll.dll.

Step 2: Use the generated MyManagedDll.dll in your C# application (let us cal it MyDotNetApp.exe)

Step 3: Register MyComDll.dll 

Step 1: Generating a managed com wrapper

The first step is to generate a Managed wrapper for MyComDll.dll so that you can add it as a reference into your MyDotNetApp.exe project.

· If you have been building this COM dll all by yourself, then build your IDL solution separately. This will generate a .TLB file once the compilation succeeds. In this case when we build MyComDll.IDL we get a MyComDll.TLB.

· Visual Studio 2005 ships with a tool called TlbImp.exe. This tool uses your .TLB file and generates a managed wrapper for it. In this case, when we run Tlbimp.exe on MyComDll.TLB, we will get a managed dll. This is MyManagedDll.dll of which we spoke above.

      You can read more about Tlbimp.exe at https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrftypelibraryimportertlbimpexe.asp.

 

By the end of step 1, you are all set to use the generated managed dll in your MyDotNetApp project!

 

Step 2: using the managed com wrapper in your application

Now let us see how we can use the built managed COM wrapper dll in your MyDotNetApp project.

Open your MyDotNetApp project and add this built MyManagedDll.dll as a reference and add the reference in your .cs file via the using keyword. Something like:

using System.Runtime.InteropServices;

using ComDll;

 

Now you create an instance of the public class exposed from your COM dll (let us call this class as MyAppClass) by doing the following:

MyAppClass application = new MyAppClass();

 

This will do the CoCreateInstance equivalent part of your COM dll and return you an instance based on its UUID.

 

Now you are all set to interact with the interfaces of your COM dll. Here is how you do QueryInterface: if you want to query Interface A from a class B, then here is how you do it:

IMyAppA settings = (IMyAppA) myAppBObject;

 

Step 3: registering your com dll before use

Now, COM dlls either need to be registered or not before they can be used depending on whether it expects a manifest file or not.

If your COM dll is built in a way that it expects a manifest file, then please keep the .manifest file in the same location where your application binary and the dll both reside. In this case when you build MyDotNetApp solution file, VS will create a MyDotNetApp.exe.manifest file for you. The contents of this file will be something like:

<? xml version="1.0" encoding="UTF-8" standalone="yes" ?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">

    <assemblyIdentity version="1.1.0.0" processorArchitecture="x86" name="MyDotNetApp.exe" type="win32" />

<dependency>

<dependentAssembly>

<assemblyIdentity type="win32" name="Microsoft.VC80.CRT" version="8.0.50608.0" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b" />

       </dependentAssembly>

</dependency>

<dependency>

<dependentAssembly>

<assemblyIdentity version="2.0.0.0" processorArchitecture="x86" name="MyComDll" type="win32" />

</dependentAssembly>

</dependency>

</assembly>

 

So, make sure that you keep MyDotNetApp.exe.manifest, MyDotNetApp.exe, MyManagedDll.dll and MyComDll.dll all in the same location before you run.

 

Instead, if your COM dll is built in a way that you need to register it before you can instantiate the class, then make sure that you register it on the machine where you are planning to run the application (under HKCR\CLSID\{…}\ InprocServer32 \). Make sure that you give the correct location where MyComDll.dll resides on your machine in the registration path.

 

You can read more about manifest files at: https://windowssdk.msdn.microsoft.com/en-us/library/ms717746.aspx

 

Run your application!


(This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm)