Reg free COM interop with C#, possible?

落花浮王杯 提交于 2019-12-03 00:53:15
Ian Boyd

You need to:


  1. You need to come up with your registration-free COM assembly manifest, where you state your dependancy on Neutrino.TestComSvr2 assembly:

    <?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>
    

    Note: i had a similar manifest in my question Registration-Free COM from ASP.NET?

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

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

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

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>

And blingo blango, you should be working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!