c++ - 将“this”传递给 C API 回调,获取 void* 用户数据
问题描述
我正在用 C++/CLI 包装器包装 C API,以便从 C# 项目中使用它。C API 的callback
其中一项功能需要 a。回调签名采用void*
用户数据参数。
我想this
作为void*
用户数据参数传递给 C API,但我没有成功(编译失败)。此外,传递“this”并不像this
引用托管类那么容易,所以我当然必须在传递它之前先固定引用。
我也不确定如何将void*
参数转换回ManagedClass^
回调函数中的 a 。
代码:
public ref class ManagedClass
{
public:
static void __stdcall CallbackFunc( int, void* userData )
{
// How can I cast the void* parameter back to
// ManagedClass^ ??
}
void Open( )
{
// I know I need to pin the reference to myself
// but I'm unsure how to go about it. How can I store
// this in a pin_ptr??
ptr = this;
// Open the third party C-API passing in
// my callback function and a reference to
// myself.
thirdPartyApiOpen( &CallbackFunc, ptr );
}
private:
pin_ptr<ManagedClass^> ptr;
};
解决方案
推荐阅读
- python - Python Lambda 函数无法应用于多列
- python - 烧瓶 api 路由请求获取参数丢失错误
- pandas - 具有多个条件的 Pandas drop_duplicates
- multidimensional-array - 如何快速对多维数组进行排序
- node.js - 如何呈现特定选项卡
- css - 在图像上显示版权作为标题
- c++ - cmake/cpack 分发一个库及其依赖项
- javascript - node.js net.connect() 错误,尽管 try-catch
- powershell - 术语“Install-ChocolateyPackage”未被识别为 cmdlet、函数的名称
- python - 将列中的值移动到该列中每个空白行之前的一行