我正在编写一个C#应用程序,它使用Interop服务来访问本机C++ DLL中的函数.我已经使用了大约10种不同的功能.
现在我不知道如何处理将回调作为参数传递,以便DLL可以调用我的代码.
这是DLL的函数原型:
typedef void (WINAPI * lpfnFunc)(const char *arg1, const char *arg2)
Run Code Online (Sandbox Code Playgroud)
并且允许我传递上述类型的函数:
int WINAPI SetFunc(lpfnFunc f)
Run Code Online (Sandbox Code Playgroud)
这是委托和函数定义的C#代码:
public delegate void Func(string arg1, string arg2);
public static void MyFunc(string arg1, string arg2)
Run Code Online (Sandbox Code Playgroud)
这是我的SetFunc Interop函数的C#代码:
[DllImport("lib.dll", CharSet = CharSet.Ansi)]
public static extern int SetFunc(Func lpfn);
Run Code Online (Sandbox Code Playgroud)
最后这里是我调用SetFunc函数并将其传递给我的回调的代码:
SetFunc(new Func(MyFunc));
Run Code Online (Sandbox Code Playgroud)
不幸的是,我的功能应该被调用.SetFunc函数的返回值是返回Success的错误代码,所以要么它没有调用我的函数,要么因为我的代码错误而无法正常工作.