是否可以将免注册 COM 与 Dotnet 互操作和 C# 一起使用?如果是这样,如何在 C# 项目中添加对 COM 对象的引用?
我有一个带有嵌入式 list 和两个测试客户端的无注册 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'”,之后只要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 对象本身,这当然完全违背了这一点,因为它根本不是无注册的。
有人有什么想法吗?
(这是在带有 VS2010 sp1 的 WinXP 上)。
最佳答案
你需要:
- 添加registration free COM entries to your assembly manifest xml file , 和
- build that manifest into your C# executable .
您需要提出您的免注册 COM 程序集 list ,其中声明您对 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? 中有类似的 list
然后您需要在您的 C# exe 中包含此应用程序 list :
- 在解决方案资源管理器中右键单击该项目。
- 点击“添加新项目”。
- 选择“应用程序 list 文件”。
这些步骤类似于在 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,你应该工作。
关于c# - 注册免费 COM 与 C# 互操作,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9586000/