通过Interop / pinvoke传递C#回调函数
我正在编写一个C#应用程序,它使用Interop服务来访问本机C ++ DLL中的函数。 我已经使用了大约10种不同的function。
现在我不知道如何处理将回调作为参数传递,以便DLL可以调用我的代码。
这是DLL的函数原型:
typedef void (WINAPI * lpfnFunc)(const char *arg1, const char *arg2)
并且允许我传递上述类型的函数:
int WINAPI SetFunc(lpfnFunc f)
这是委托和函数定义的C#代码:
public delegate void Func(string arg1, string arg2); public static void MyFunc(string arg1, string arg2)
这是我的SetFunc Interop函数的C#代码:
[DllImport("lib.dll", CharSet = CharSet.Ansi)] public static extern int SetFunc(Func lpfn);
最后这里是我调用SetFunc函数并将其传递给我的回调的代码:
SetFunc(new Func(MyFunc));
不幸的是,我的function应该被调用。 SetFunc函数的返回值是返回Success的错误代码,所以要么它没有调用我的函数,要么因为我的代码错误而无法正常工作。
这对我有用:
Calc.h(Calc.dll,C ++):
extern "C" __declspec(dllexport) double Calc(double x, double y, double __stdcall p(double, double));
Calc.cpp(Calc.dll,C ++):
#include "calc.h" __declspec(dllimport) double Calc(double x, double y, double __stdcall p(double, double)) { double s = p(x*x, y*y); return x * y + s; }
Program.cs(Sample.exe,C#):
class Program { delegate double MyCallback(double x, double y); [DllImport("Calc.dll", CallingConvention = CallingConvention.Cdecl)] static extern double Calc(double x, double y, [MarshalAs(UnmanagedType.FunctionPtr)]MyCallback func); static void Main(string[] args) { double z = Calc(1, 2, (x, y) => 45); } }
你能尝试将Func委托更改为
public delegate void Func([In, MarshalAs(UnmanagedType.LPStr)] string arg1, [In, MarshalAs(UnmanagedType.LPStr)] string arg2);
和SetFunc方法一样
[DllImport("lib", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Winapi)] public static extern int SetFunc(Func lpfn);