在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、新建一个C#-Windows-类库(用于创建C#类库(.dll)的项目)类型的工程 2、对于普通C++函数 XXXX_API void cppFun(int i); 在cs代码中添加 [DllImport("c++xxxx.dll")] public static extern void cppFun(int i); 3、对于C++中的结构体,因为C++中结构体默认访问控制为public,而C#中结构体默认为private,所以要显示指定一下 typedef struct { C#对应 public struct NET_POSA_ETHERNET
将string转为IntPtr:IntPtr System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto(string) 将IntPtr转为string:string System.Runtime.InteropServices.MarshalPtrToStringAuto(IntPtr) 类型对照: BSTR --------- StringBuilder LPCTSTR --------- StringBuilder LPCWSTR --------- IntPtr handle---------IntPtr hwnd-----------IntPtr char *----------string int * -----------ref int int &-----------ref int void *----------IntPtr unsigned char *-----ref byte Struct需要在C#里重新定义一个Struct CallBack回调函数需要封装在一个委托里 typedef void(* CallbackResult)(int progress, char *result); extern"C" _declspec(dllexport) void subscribe0(char* eventName, CallbackResult callback) { callback(8, "AAABBB"); } C# public delegate void CallBack(int progress, string lParam); [DllImport("deepstream_for_csharp")] public static extern void subscribe0(string eventName, CallBack func); CallBack myCallBack = new CallBack(ReportView); subscribe0("adsm", myCallBack); public static void ReportView(int progress, string lParam) { Debug.Log("AAAAAA"); }
C++: EXPORTS_U3DDLL void subscribe(char* eventName,void(*func)(const char*)) { if (client == 0) { std::cout << "client has not been created!!"; return; } const deepstream::SubscriptionId event_sub_id = client->event.subscribeForUnity(eventName, func); subEventID[eventName] = event_sub_id; } SubscriptionId subscribeForUnity(const std::string &name, std::function<void(const char* data)> callback) C#: [DllImport("deepstream_for_csharp")] public static extern void subscribe(string eventName, SubscribeCallBack callback); public delegate void SubscribeCallBack(string data); public static void eventPosCallBack(string data) { Debug.Log("pos:" + data+data.Length); } subscribe("pos", new SubscribeCallBack(eventPosCallBack));
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论