首页 文章

本机应用程序可以使用免注册COM来使用.NET COM组件吗?

提问于
浏览
1

Q: Can I make a native client that consumes a .NET COM component, using reg free COM?

即我可以在其清单中使用COM信息创建一个C应用程序,而不是在注册表中注册组件(COM'stuff')吗?


注意:我开始考虑这个问题的reg免费COM,并没有通过网络搜索找到快速回答 . 当我找到答案时,我想我会在SO上发帖,以防其他人搜索......

2 回答

  • 2

    是 .

    这里有一篇MSDN有一篇文章(来自2005年),它通过以下工作示例:

    ...本地客户端通过COM互操作免费注册基于.NET Framework的组件 . (11页打印)https://msdn.microsoft.com/en-us/library/ms973915.aspx(2015年1月访问)

  • 2

    Daryn,

    此博客文章向您解释如何执行此操作:http://blogs.msdn.com/b/rodneyviana/archive/2015/08/24/pure-native-c-consuming-net-classes-without-com-registration.aspx

    基本上你使用一个入口点返回指向接口的指针(使用DllExport Nuget进行“导出”):

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using RGiesecke.DllExport;
    
    namespace ContosoCom
    {
    
     public static class Exports
     {
         [DllExport(CallingConvention = CallingConvention.Cdecl)]
         public static void GetClass([Out] [MarshalAs((UnmanagedType.Interface))] out IMyClass pInterface)
         {
             pInterface = new MyClass();
         }
     }
    
    
     [ComVisible(true)]
     [System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown)]
     public interface IMyClass
     {
         void DisplayMessageBox([MarshalAs(UnmanagedType.BStr)] string Text);
         void GetTicksAndDate([Out] out MyStruct Structure);
     }
    
     [ComVisible(true)]
     [StructLayout(LayoutKind.Sequential, Pack = 8)]
     public struct MyStruct
     {
         public long TicksOfNow;
         public int Day;
         public int Month;
         public int Year;
     }
    
     [ComVisible(true)]
     [ClassInterface(ClassInterfaceType.None)]
     [ComDefaultInterface(typeof(IMyClass))]
     public class MyClass : IMyClass
     {
    
         public void DisplayMessageBox(string Text)
         {
             MessageBox.Show(Text);
         }
    
         public void GetTicksAndDate(out MyStruct Structure)
         {
             Structure.TicksOfNow = DateTime.Now.Ticks;
             Structure.Day = DateTime.Now.Day;
             Structure.Month = DateTime.Now.Month;
             Structure.Year = DateTime.Now.Year;
         }
     }
    

    }

    这就是C代码的样子:

    #include "stdafx.h"
    #import "..\..\bin\Debug\ContosoCom.tlb" auto_rename
    
    
    using namespace ContosoCom;
    typedef void(*pGetClass)(IMyClass **iMyClass);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
     pGetClass getClass = NULL;
     IMyClass *mc = NULL;
    
     HINSTANCE hDLL = 0;
     // load the DLL
    
     hDLL = ::LoadLibrary(L"ContosoCom.dll");
    
     ::CoInitialize(NULL);
    
     if(!hDLL)
     {
         printf("ERROR: Unable to load library ContosoCom.dll\n");
         return -1;
     }
    
    
     //
     // TO DO: Add code here to get an instance of MyClass
     //
     getClass = (pGetClass)GetProcAddress(hDLL, "GetClass");
    
     if(!getClass)
     {
         printf("ERROR: Unable to find entry for GetClass()\n");
         return -1;
    
     }
    
     getClass(&mc);
    
     // At this point we do not have how to get a pointer even with the libray loaded
    
     // End of TO DO
    
     if(!mc)
     {
         printf("ERROR: Unable to get a pointer for MyClass\n");
         return -1;
     }
    
     mc->DisplayMessageBox("Hello World from native to .NET without registration");
     MyStruct st;
     ZeroMemory(&st, sizeof(MyStruct));
     mc->GetTicksAndDate(&st);
     printf("Ticks %I64i\n",st.TicksOfNow);
     printf("Today is %i/%i/%i\n",st.Month,st.Day,st.Year);
    
    
     printf("SUCCESS: Leaving gracefully\n");
     return 0;
    }
    

相关问题