繁体   English   中英

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

[英]Can a native application use registration-free COM to consume a .NET COM component?

问:我可以使用免注册COM制作使用.NET COM组件的本机客户端吗?

即我可以使用清单中的COM信息来制作C ++应用程序,而不是在注册表中注册组件(COM'stuff')吗?


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

是。

这是MSDN上的一篇文章(来自2005年),其中包含以下工作示例:

...本机客户端通过COM互操作对基于.NET Framework组件的免注册激活。 (11页印刷)
https://msdn.microsoft.com/zh-cn/library/ms973915.aspx(2015年 1月访问)

达琳

此博客条目向您说明了如何执行此操作: http : //blogs.msdn.com/b/rodneyviana/archive/2015/08/24/pure-native-c-consumping-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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM