c++ Cx将委托传递给原生c++
C++ Cx passing delegates to native c++
我需要找到一种方法来检索回调从c++/Cx回本机c++。在c++代码中,这些指针存储为函数指针,而在c++/Cx中,它们存储在委托中。我发现在c#和c++/CLI中有一个类似GetFunctionPointerForDelegate()
的函数,但似乎在c++/CX中没有类似的函数。这个问题真的有解决办法吗?
c++/Cx是c#应用程序和c++原生库之间的中间层。我设法将指针从c#传递到WinRt(c++/Cx)层,如下所示:
del d = new del(callBackFunction); //declare a delegate and
//add a function to delegate
IntPtr p = GetFunctionPointerForDelegate(d); //retrieve pointer
w.retrieveCallbackFunction(p.toInt32()); //pass a pointer as int to WinRt layer
在WinRt层内部的retrieveCallbackFunction:
void* x=(void*)p; //cast it to void*
callbackFunctionType ptr = (callbackFunctionType)x; // cast it to a type
// of function pointer
ptr(...); //call function
我知道这不是最好的解决方案,但是这个方法现在对我很有效。
编辑
防止垃圾收集器收集函数或委托也是一个好主意
GCHandle callbackHandle = GCHandle.Alloc(d); // !!!!
我对c++/CX没有任何线索,但快速搜索后发现这对我来说听起来像是你想要完成的:
codeproject.com:将委托转换为函数指针以实现回调函数
相关文章:
- Android Studio 中带有静态库的原生C++代码
- 如何在 Android Studio 4 中编译 C/C++ 原生代码
- Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让原生C / C++
- 在Android原生C++中使用Facemark opencv contrib
- CMake:Android Studio 原生活动项目无法在项目根目录之外找到第三方库
- Android NDK - C++ 异常会导致第三方原生库崩溃
- 如何在 gradle 原生项目中包含二进制库?
- 使用 Flutter 从 Android 上的原生编译共享库中调用函数
- exteranl原生构建问题,构建命令失败,android工作室
- build命令失败,外部原生问题android工作室
- 如何在electrolon项目中集成qt原生窗口
- Lua :在原生lua中编译,但在使用LuaJIT和sol2时C++出错
- 如何将.cpp C++原生库导入 Android Studio
- 原生 WebRTC 丢帧
- 如何使用 JNI 创建原生类?
- 如何为原生UI工具包扩展Ranorex?
- 如何从反应原生代码调用 C 函数?
- 安卓设备上的原生C++ WiFi P2P 网络是否可行?
- Android Studio 调试原生代码 lldb 服务器工作无穷大
- Android 服务和原生线程