C#动态调用C++DLL中的函数

DLL中导出的函数

cpp 复制代码
typedef void (*HQ_MSG_CALLBACK)(void *h, int nMsg, int nMsgType, int nReqNo, const char *szData, int nSize);
void SetMsgFunc(void *h, HQ_MSG_CALLBACK pmsgCallBack);

C#动态调用上述函数

csharp 复制代码
public delegate void CALLBACK(IntPtr h, int nMsg, int nMsgType, int nReqNo, IntPtr data, int nSize);
[DllImport(DllPath, CallingConvention = CallingConvention.Cdecl)]
private static extern void SetMsgFunc(CALLBACK pmsgCallBack);


public static void HQCallBack(IntPtr h, int nMsg, int nMsgType, int nReqNo, IntPtr data, int nSize)
{}
HQ_MSG_CALLBACK callBackFunc = new HQ_MSG_CALLBACK(HQCallBack);
SetMsgFunc(callBackFunc);//也可直接传HQCallBack函数名

其中函数指针由委托delegate替代,使用时可以传委托对象,也可以直接传函数名;指针由IntPtr替代

相关推荐
挖矿大亨1 小时前
C++中深拷贝与浅拷贝的原理
开发语言·c++·算法
Bruce_kaizy1 小时前
c++图论——生成树之Kruskal&Prim算法
c++·算法·图论
雾岛听蓝2 小时前
C++:模拟实现string类
开发语言·c++
XFF不秃头2 小时前
力扣刷题笔记-合并区间
c++·笔记·算法·leetcode
编程之路,妙趣横生2 小时前
STL(七) unordered_set 与 unordered_map 基本用法 + 模拟实现
c++
寂柒3 小时前
c++--
c++
wregjru3 小时前
【读书笔记】Effective C++ 条款3:尽可能使用const
开发语言·c++
kylezhao20193 小时前
C#手写串口助手
开发语言·c#
向宇it3 小时前
2025年技术总结 | 在Unity游戏开发路上的持续探索与沉淀
游戏·unity·c#·游戏引擎
历程里程碑4 小时前
滑动窗口秒解LeetCode字母异位词
java·c语言·开发语言·数据结构·c++·算法·leetcode