是否可以使用注册免费COM与Dotnet互操作和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选项下将'Additional Manifest Dependencies'设置为“type ='win32'name ='TestComSvr1'version ='1.0.0.0'”,之后cpp客户端将正常运行因为COM组件在同一目录中。
c#客户端虽然拒绝播放。
尝试向未注册的COM组件dll或未注册的tlb添加文件引用会导致错误:
“无法添加对'blah blah'的引用。请确保该文件是可访问的,并且它是有效的程序集或COM组件”。
仅使用'regtlib TestComSvr'注册类型库,然后创建文件或COM引用,导致c#项目无法构建:
“加载类型库/ Dll时出错。(HRESULT异常:0x80029C4A(TYPE_E_CANTLOADLIBRARY))”。
通常在c#项目中注册COM组件并创建引用,将引用设置为Isolated,构建c#项目然后取消注册组件并运行c#项目会导致此异常:
检索具有CLSID {B1D0A80F-0050-4856-BACD-87D664E58CBE}的组件的COM类工厂因以下错误而失败:80040154未注册类(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG))。
注意:即使这样做也无论如何都不是一个有用的案例,因为最终它仍然需要注册组件,我只是为了彻底性而进行了测试。
到目前为止,我能够从C#引用COM对象的唯一方法是注册COM对象本身,这当然完全违背了这一点,因为它根本不是免费注册的。< / p>
有人有任何想法吗?
(这是在带有VS2010 sp1的WinXP上)。
答案 0 :(得分:12)
你需要:
您需要提供免注册的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中包含此应用程序清单:
这些步骤类似于MSDN上adding a manifest to a managed application的内容。
您还需要确保 Neutrino.TestComSvr2 程序集包含它的无注册COM条目:
<强> Neutrino.TestComSvr2.manifest 强>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity type="win32" name="Neutrino.TestComSvr2" version="1.0.0.0" /> <file name = "ContosoFrobber.dll"> <comClass progid="Frobber.Gizmo" clsid="{00028C00-0000-0000-0000-000000000046}" description="Gizmo Frobber by Contoso" threadingModel = "Apartment" /> <typelib tlbid="{00028C01-0000-0000-0000-000000000046}" version="1.0" helpdir=""/> </file> </assembly>
和blingo blango,你应该工作。