方法
- P/Invoke Interop Assistant (微软出品,很久没更新了,功能一般)
- swig (支持多语言,通用强大,历史久远)
- swig c# 示例
Example for SWIG to wrap C++ library in .Net 6 (iamsorush.com)
- CppSharp (Mono组织开发,.NET专用,推荐!)
- bottlenoselabs/c2cs: Generate C# bindings from a C header. (github.com)
- CLR(使用CLR项目进行C#调用C++)
- [CLP混合编程]使用 C++/CLI 进行 .NET 编程 | Microsoft Docs
- SharpGenTools/SharpGenTools: Accurate and high performance C++ interop code generator for C#. (github.com)
- [CppAst.CodeGen](xoofx/CppAst.CodeGen: An extensible library providing C# PInvoke codegen from C/C++ files for .NET (github.com))
- p/invok
- xoofx/CppAst.NET: CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros --- xoofx/CppAst.NET:CppAst是一个.NET库,为由Clang/libclang提供支持的头文件提供C / C++解析器,可以访问完整的AST,注释和宏 (github.com)
- 官方文档:
本机互操作性 - .NET | Microsoft Docs
- p/invok处理union类型时:
P/invoke with unions in C# | yizhang82’s blog
P/Invoke各种总结(八、在C#中使用Union联合体) - 编程猎人 (programminghunter.com)
C# 中的并集 - 与非对象字段错误地对齐或重叠|易学教程 (e-learn.cn)
- p/invok处理指针类型时:
P/Invoke各种总结(五、在C#中使用指针类型) - zhaotianff - 博客园 (cnblogs.com)
各种绑定工具对比
参考
cppsharp和swig哪个好用? - 知乎 (zhihu.com)
windows api 绑定
dll
dll export 函数查看
- vs developer cmd
dumpbin /exports *.dll
c/c++ 导出函数
导出 C 函数以用于 C 或 C++ 语言可执行文件 | Microsoft Learn
示例:
exportl_dll.h
#pragma once
#include "./Cnc.h"
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) CncConnectionInterface* new_cnc(char* hostName, char* hostAddress, int hostPort);
__declspec(dllexport) void del_cnc(CncConnectionInterface* cnc);
__declspec(dllexport) bool isConnect(CncConnectionInterface* cnc);
__declspec(dllexport) bool connect(CncConnectionInterface* cnc);
__declspec(dllexport) bool dis_connect(CncConnectionInterface* cnc);
__declspec(dllexport) char* read(CncConnectionInterface* cnc, char* param, char* sub_param);
__declspec(dllexport) void del_read(char* v);
#ifdef __cplusplus
}
#endif
exportl_dll.cpp
#include "exportl_dll.h"
CncConnectionInterface* new_cnc(char* hostName, char* hostAddress, int hostPort) {
return Cnc::CreateConnectionInterface(hostName, hostAddress, hostPort);
}
void del_cnc(CncConnectionInterface* cnc) {
delete cnc;
cnc = nullptr;
}
bool isConnect(CncConnectionInterface* cnc) {
return cnc->CncConnectionEstabilished();
}
bool connect(CncConnectionInterface* cnc) {
return cnc->ConnectCnc();
}
bool dis_connect(CncConnectionInterface* cnc) {
return cnc->DisconnectCnc();
}
char* read(CncConnectionInterface* cnc, char* param, char* sub_param) {
std::string v = cnc->ReadCncStringParameter(param, sub_param);
char* vv = new char[v.length() + 1];
std::strcpy(vv, v.c_str());
return vv;
}
void del_read(char* v) {
delete[] v;
v = nullptr;
}
dumpbin 查看函数暴露情况:
Dump of file lib_fidia.dll
File Type: DLL
Section contains the following exports for lib_fidia.dll
00000000 characteristics
FFFFFFFF time date stamp
0.00 version
1 ordinal base
13 number of functions
13 number of names
ordinal hint RVA name
1 6 00024555 connect = @ILT+1360(_connect)
2 7 00025B0D del_cnc = @ILT+6920(_del_cnc)
3 8 00025E19 del_read = @ILT+7700(_del_read)
4 9 000251CB dis_connect = @ILT+4550(_dis_connect)
5 A 00025B35 isConnect = @ILT+6960(_isConnect)
6 B 00024672 new_cnc = @ILT+1645(_new_cnc)
7 C 00025702 read = @ILT+5885(_read)
Summary
1000 .00cfg
2000 .data
4000 .idata
1000 .msvcjmc
C000 .rdata
4000 .reloc
1000 .rsrc
4F000 .text
23000 .textbss
1000 .tls