Setting up a DLL for registration-free COM Interop

2019-01-28 21:24发布

问题:

I am new to COM and have to create a COM wrapper for a C++ dll, to make some functionality in it accessible for managed code. I mainly followed this tutorial on code project to create a COM ATL dll. I created a post-build-step that calls tlbimp.exe on the COM dll to create the wrapping.

Adding a reference to the dll that tlbimp.exe creates works, and I get code completion for methods on my COM dll, and can compile managed code that uses that dll. However, running that code leads to the following exception:

Unhandled Exception: System.Runtime.InteropServices.COMException: Retrieving the COM class factory 
for component with CLSID {FD6F73D2-0208-455A-953A-C911EC2CAEAB} failed due to the following error: 
80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
//...

If I understand this exception and the documentation on MSDN correctly, the next step would be to set up the 'registration-free' part of registration-free COM interop. I have looked at various guides such as this one and this one, but I still don't understand what I have to do.

So my question is:

What are the basic steps I need to take to make my newly created .Net-compatible COM dll load without registration?

Do I have to write a manifest for my .Net app that uses the COM dll? Do I have to write a manifest for my COM dll? Or the DLL that tblimp.exe generates from it?

回答1:

I believe you don't see the Isolated property in Solution Explorer, because you manually generated the Interop assembly with tlmbimp.exe, then added it to the project.

Don't do that. Remove that existing reference, then add a new one, where you'd browse for the original unmanaged COM DLL. In Reference Manager, Click COM at the left, click Browse button, then browse for and select your unmanaged COM DLL. Visual Studio will do the rest.