Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

c++ - Setting up a DLL for registration-free COM Interop with C# / Managed code

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...