使用 C#/托管代码为免注册 COM 互操作设置 DLL

Setting up a DLL for registration-free COM Interop with C# / Managed code(使用 C#/托管代码为免注册 COM 互操作设置 DLL)

本文介绍了使用 C#/托管代码为免注册 COM 互操作设置 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 COM 新手,必须为 C++ dll 创建一个 COM 包装器,以使托管代码可以访问其中的某些功能.我主要关注 this tutorial on codeproject 来创建一个 COM ATL dll.我创建了一个后期构建步骤,它在 COM dll 上调用 tlbimp.exe 来创建包装.

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.

添加对 tlbimp.exe 创建的 dll 的引用有效,并且我可以完成我的 COM dll 上的方法的代码,并且可以编译使用该 dll 的托管代码.但是,运行该代码会导致以下异常:

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)).
//...

如果我正确理解此异常和 MSDN 上的文档,下一步将是设置免注册 COM 互操作的免注册"部分.我查看了各种指南,例如 this one 和 这个,但我还是不明白是什么我要做.

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.

所以我的问题是:

我需要采取哪些基本步骤才能在不注册的情况下加载我新创建的.Net 兼容 COM dll?

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

我是否必须为使用 COM dll 的 .Net 应用程序编写清单?我必须为我的 COM dll 编写清单吗?还是 tblimp.exe 从中生成的 DLL?

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?

推荐答案

我相信您在解决方案资源管理器中看不到 Isolated 属性,因为您使用 tlmbimp.exe 手动生成了 Interop 程序集,然后将其添加到项目中.

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.

不要那样做.删除该现有引用,然后添加一个新引用,您可以在其中浏览原始非托管 COM DLL.在 Reference Manager 中,单击左侧的 COM,单击 Browse 按钮,然后浏览并选择您的非托管 COM DLL.Visual Studio 将完成剩下的工作.

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.

这篇关于使用 C#/托管代码为免注册 COM 互操作设置 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 C#/托管代码为免注册 COM 互操作设置 DLL

基础教程推荐