C++调用 c#生成的dll
详细见:Interoping .NET and C++ through COM - CodeProject,
个人理解:
1、C#生成的dll相当于com server
2、C++调用相当于com client
3、client 要访问 server 的动态库,需要进行assembly注册,见:Registering Assemblies with COM - .NET Framework | Microsoft Learn。
4、注册:管理员权限C:\Windows\Microsoft.NET\Framework64\v4.0.30319>regasm D:\wjw\gitee\SoftwareLicense-master\Register\bin\Debug\Register.dll
5、注册tlb:C:\Windows\Microsoft.NET\Framework64\v4.0.30319>regasm D:\wjw\gitee\SoftwareLicense-master\Register\bin\Debug\Register.dll /tlb:Register.tlb
6、dll的位置:
-
The global assembly cache (must be a strong-named assembly).C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools>gacutil /i D:\wjw\gitee\SoftwareLicense-master\Register\bin\Debug\Register.dll
-
In the application directory. Assemblies loaded from the application path are only accessible from that application.
-
Along an file path specified with the /codebase option to Regasm.exe.
7、将生成的tlb拷贝到c++项目当前文件夹
8、导入c++:
#include<atlcomcli.h>
#import "Register.tlb"
9、调用:
CoInitialize(NULL);
myInterfacePtr dll(__uuidof(ComDll));
BSTR str = SysAllocString(L"");
bool b = dll->check("", &str);
char* p = _com_util::ConvertBSTRToString(str);
qInfo() << p;
SysFreeString(str);
以上代码,myInterface为ComDll对应的接口,ComDll为com server 类,check函数str在c#为out string.
10、regasm替代:
reg import pathtoreg\Register.reg 实现静默导入
reg为regasm /regFile选项生成的注册表项,用于客户端注册表的导入
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南