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

时间:2015-01-29 19:43:45

标签: c++ .net com com-interop side-by-side

问:我是否可以使用免费注册COM创建一个使用.NET COM组件的本机客户端?

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


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

2 个答案:

答案 0 :(得分:2)

这是MSDN的一篇文章(来自2005年),其中介绍了一个工作示例:

  

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

答案 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 for" export"):

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;
}