注册自由COM互操作与C#,可能吗? [英] Reg free COM interop with C#, possible?

查看:177
本文介绍了注册自由COM互操作与C#,可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能使用注册免费COM与Dotnet互操作和C#?如果是这样,怎么在C#项目中添加对COM对象的引用?

Is it possible to use registration free COM with Dotnet interop and C#? If so how does one add a reference to the COM object in the C# project?

我有一个reg自由的ATL COM服务器DLL与嵌入清单和两个测试客户端,一个cpp另一个c#。 cpp客户端使用import语句和

I have a reg free ATL COM server dll with an embedded manifest and two test clients, one cpp the other c#. The cpp client correctly references the COM object using an import statement and either

#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组件在同一目录中。

or setting 'Additional Manifest Dependencies' to "type='win32' name='TestComSvr1' version='1.0.0.0'" under Linker->Manifest File options, after which the cpp client will run correctly just so long as the COM component is in the same directory.

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

The c# client though refuses to play at all.

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

Attempting to add a file reference to either the unregistered COM component dll or unregistered tlb results in the error:


对blah blah的引用

"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#项目无法构建:

Registering just the type library with 'regtlib TestComSvr' and then creating either a file or COM reference to that results in the c# project failing to build with:


加载类型库/ Dll时出错。 (来自HRESULT的异常:0x80029C4A(TYPE_E_CANTLOADLIBRARY))。

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

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

Registering the COM component and creating a reference normally in the c# project, setting the reference to Isolated, building the c# project then unregistering the component and running the c# project results in this exception:


检索COM类工厂对于具有CLSID {B1D0A80F-0050-4856-BACD-87D664E58CBE}的组件,由于以下错误而失败:80040154类未注册(来自HRESULT的异常:0x80040154(REGDB_E_CLASSNOTREG))。

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

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

Note: Even if this worked it wouldn't be a useful case anyway, since ultimately it still requires registration of the component, I just tested it for thoroughness.

到目前为止,我能够从C#引用COM对象的唯一方法是通过注册COM对象本身,这当然完全失败的点,从那时它不是reg-

So far the only way I've been able to reference the COM object from C# at all is by registering the COM object itself, which of course utterly defeats the point since then it isn't reg-free at all.

(这是在VS2010 sp1的WinXP上)

Anyone got any ideas?

/ p>

(This is on WinXP with VS2010 sp1).

推荐答案

您需要:

  • add the registration free COM entries to your assembly manifest xml file, and
  • build that manifest into your C# executable.

  1. 您需要提供免注册的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>

/ strong>我有一个类似的清单在我的问题免注册COM从ASP.NET?

然后您需要在C#exe中包含此应用程序清单:

Then you need to include this application manifest in your C# exe:


  1. 在解决方案资源管理器中右键单击项目。

  2. 单击添加新项目

  3. 选择应用程序清单文件

  1. Right-click on the project in the Solution Explorer.
  2. Click "Add New Item".
  3. Select "Application Manifest File".


这些步骤类似于MSDN上的向托管应用程序添加清单

These steps are similar to what can be found on MSDN for adding a manifest to a managed application.

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

You also need to ensure that your Neutrino.TestComSvr2 assembly contains it's registration-free COM entries:


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,你应该工作。

And blingo blango, you should be working.

这篇关于注册自由COM互操作与C#,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆