c#和c++数据交互二
1.c#和C++数据交互 一CLR篇
2.c#和c++数据交互二
1:新建c++模板,生成类型动态库
2:c++里类型
点击查看代码
class A
{
public:
int a;
};
typedef struct
{
int up;
int down;
char infos[4];
//LPWSTR infos;
}info,*LPinfo;
点击查看代码
#define CVAPI extern "C" __declspec(dllexport)
CVAPI int Add1(int a,int b ,int len);
对应c#
[DllImport(Dllpath, CallingConvention = CallingConvention.StdCall)]
public static extern int Add1(int a,int b ,int len);
点击查看代码
//定义函数模型
int _declspec(dllexport)_stdcall FuncCsharp(int*);
回调
int CallBackAdd(int a, intptr_t callBack)
{
decltype(FuncCsharp)* callHander = NULL;
if (callBack != 0)
{
callHander = (decltype(FuncCsharp)*)callBack;
}
int b = 6;
if (callHander != NULL)
{
b = callHander(&a);
}
return b;
}
c#使用
[DllImport(Dllpath, CallingConvention = CallingConvention.StdCall)]
public static extern int CallBackAdd(int a, IntPtr strPtr);
public delegate int CsharpCall(ref int a);
CsharpCall c = new CsharpCall(CsharpMethod);
IntPtr ptr = Marshal.GetFunctionPointerForDelegate(c);
var p2 = ptr.ToPointer();
var val=CallBackAdd(5, ptr);
5 基础数据类型对应
点击查看代码
c# c++
int,short,byte,long int,short,byte,long
ref int,short,byte,long int*,short*,byte*,long*
结构体c#里是托管类型,在封送时需要注意以下特性
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public static extern int StringToChar([MarshalAs(UnmanagedType.LPStr)] string str);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
2023-08-15 对象拷贝方法