C# · 12月 30, 2021

注册免费COM interop与C#可能吗?

可以使用注册免费COM与Dotnet interop和C#吗?那么如何在C#项目中添加COM对象的引用呢?

我有一个免费的ATL COM服务器dll与嵌入式清单和两个测试客户端,一个cpp其他c#. cpp客户端使用import语句正确引用COM对象

#pragma comment(linker,”\”/manifestdependency:type=’win32′ name=’TestComSvr2′ version=’1.0.0.0’\””)

或者在“Linker-> Manifest File”选项下将“附加清单依赖关系”设置为“type =’win32’name =’TestComSvr1’version =’1.0.0.0’”,之后,只要COM组件在同一目录中.

c#客户端虽然拒绝播放.

尝试向未注册的COM组件dll或未注册的tlb添加文件引用会导致错误:

“A reference to ‘blah blah’ Could not be added. Please make sure that the file is accessible,and that it is a valid assembly or COM component”.

仅使用“regtlib TestComSvr”注册类型库,然后创建一个文件或COM引用,导致c#项目无法构建:

“Error loading type library/Dll. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))”.

注册COM组件并在c#项目中正常创建引用,将引用设置为Isolated,构建c#项目,然后取消注册组件并运行c#项目会导致此异常:

Retrieving the COM class factory for component with CLSID {B1D0A80F-0050-4856-BACD-87D664E58CBE} Failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

注意:即使这样工作也不是一个有用的情况,因为最终它仍然需要注册组件,我只是测试了它的彻底性.

到目前为止,我已经能够从C#引用COM对象的唯一方法是注册COM对象本身,这当然完全失败了,因为它不是免费的.

任何人有任何想法?

(这是在WinXP与VS2010 sp1).

解决方法 你需要:

>添加registration free COM entries to your assembly manifest xml file,和
> build that manifest into your C# executable.

>您需要提出无注册的COM汇编清单,您可以在Neutrino.TestComSvr2程序集中声明您的依赖关系:

<?xml version=”1.0″ encoding=”utf-8″?><asmv1:assembly manifestVersion=”1.0″ xmlns=”urn:schemas-microsoft-com:asm.v1″ xmlns:asmv1=”urn:schemas-microsoft-com:asm.v1″ xmlns:asmv2=”urn:schemas-microsoft-com:asm.v2″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”> <assemblyIdentity version=”1.0.0.0″ name=”MyApplication.app”/> <!– We depend on our COM object –> <dependency> <dependentAssembly> <assemblyIdentity type=”win32″ name=”Neutrino.TestComSvr2″ version=”1.0.0.0″ /> </dependentAssembly> </dependency> <!– Everything else in this sample manifest is not relavent to this answer,but every developer should be doing their job and writing correct Windows programs –> <!– Disable file and registry virtualization. –> <trustInfo xmlns=”urn:schemas-microsoft-com:asm.v2″> <security> <requestedPrivileges xmlns=”urn:schemas-microsoft-com:asm.v3″> <requestedExecutionLevel level=”asInvoker” uiAccess=”false” /> </requestedPrivileges> </security> </trustInfo> <!– We are high-dpi aware on Windows Vista –> <asmv3:application xmlns:asmv3=”urn:schemas-microsoft-com:asm.v3″> <asmv3:windowsSettings xmlns=”http://schemas.microsoft.com/SMI/2005/WindowsSettings”> <dpiAware>true</dpiAware> </asmv3:windowsSettings> </asmv3:application> <!– Declare that we were designed and tested on Windows 7–> <compatibility xmlns=”urn:schemas-microsoft-com:compatibility.v1″> <application> <!–The ID below indicates application support for Windows 7 –> <supportedOS Id=”{35138b9a-5d96-4fbd-8e2d-a2440225f93a}”/> <!–The ID below indicates application support for Windows Vista –> <!– supportedOS Id=”{e2011457-1546-43c5-a5fe-008deee3d3f0}”/ –> </application> </compatibility> <!– We depend on Common Controls version 6 (i.e. “enable themes”) –> <dependency> <dependentAssembly> <assemblyIdentity type=”win32″ name=”Microsoft.Windows.Common-Controls” version=”6.0.0.0″ processorArchitecture=”*” publicKeyToken=”6595b64144ccf1df” language=”*” /> </dependentAssembly> </dependency>

注意:我在我的问题Registration-Free COM from ASP.NET?中有类似的表现
>那么你需要将这个应用程序清单包含在你的C#exe中:

>在解决方案资源管理器中右键单击该项目.
>点击“添加新项目”.
>选择“应用清单文件”.

这些步骤类似于adding a manifest to a managed application在MSDN上可以找到的步骤.

您还需要确保您的Neutrino.TestComSvr2程序集包含其无注册COM条目:

Neutrino.TestComSvr2.manifest

06001

和blingo blango,你应该工作.