更新时间:2021-07-19 23:09:54
在C ++ / CLI中,您可以具有静态函数(带有本机C签名,可以用作C库的回调) ),调用托管代表:
In C++/CLI, you can have static functions (with native C signature, which can work as a callback from a C library), calling managed delegates:
// MyDispatcherClass.h
#pragma once
public delegate void MyDelegateType();
public ref class MyDispatcherClass
{
public:
static MyDelegateType^ MyDelegate;
};
static void MyCallback(/*...*/)
{
if (MyDispatcherClass::MyDelegate != nullptr)
MyDispatcherClass::MyDelegate(/* do some type mapping here if needed */);
}
// MyDispatcherClass.cpp:
#include "stdafx.h"
#include "MyDispatcherClass.h"
因此在C库中注册 MyCallback
,将C#委托注册为 MyDispatcherClass :: MyDelegate
完成。
So register MyCallback
at your C library, register your C# delegate to MyDispatcherClass::MyDelegate
and you are done.