[英]How to pass function pointer from C# to a C++ Dll?
The function defined in C++ dll is: C ++ dll中定义的函数是:
static double (*Func1)(double);
EXTERN_C __declspec(dllexport) __stdcall double TestDelegate(double (*fun)(double))
{
Func1 = fun;
return Func1(25.0);
}
void My_Real_purpose()
{
SomeClass a;
a.SetFunction(Func1);//Define behaviour of a by C# in runtime
a.DoSomething();//Even I want it runs in another thread!
}
And I tried to call it in C# like this: 我试着用C#这样调用它:
class A
{
[DllImport("DllName.dll")]
public extern static double TestDelegate(IntPtr f);
public delegate double MyFuncDelegate(double x);
public static double MyFunc(double x)
{
return Math.Sqrt(x);
}
static MyFuncDelegate ff;
static GCHandle gch;
public static double Invoke()
{
ff = new MyFuncDelegate(MyFunc);
gch = GCHandle.Alloc(ff);
double c = TestDelegate(Marshal.GetFunctionPointerForDelegate(ff));//Error occurs this line
gch.Free();
return c;
}
}
It is compiled without error.But when it runs,VS2012 display an error of "Access Violation Exception". 它编译时没有错误。但是当它运行时,VS2012会显示“访问冲突异常”错误。
I have searched and tried a lot of ways,such as passing a delegate rather than a IntPtr,but all of them turned out to be failed. 我已经搜索并尝试了很多方法,例如传递委托而不是IntPtr,但所有这些方法都失败了。
So,what is the correct way to use an API function in a dll which contains function pointer?Or how to realize "My_Real_purpose" function? 那么,在包含函数指针的dll中使用API函数的正确方法是什么?或者如何实现“My_Real_purpose”函数?
Your delegate uses the cdecl
calling convention. 您的委托使用cdecl
调用约定。 In C# you would therefore declare the delegate like this: 因此,在C#中,您将声明代理如下:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate double CallbackDelegate(double x);
As an alternative, you could decide to declare the function pointer in C++ as __stdcall
, in which case you would remove the UnmanagedFunctionPointer
attribute and rely on the default calling convention being CallingConvention.StdCall
. 作为替代方案,您可以决定将C ++中的函数指针声明为__stdcall
,在这种情况下,您将删除UnmanagedFunctionPointer
属性并依赖于默认调用约定CallingConvention.StdCall
。
Implement it like this: 像这样实现它:
public static double MyFunc(double x)
{
return Math.Sqrt(x);
}
In order to keep the unmanaged function pointer alive (guarding against GC), you need to hold an instance of the delegate in a variable. 为了使非托管函数指针保持活动状态(防止GC),您需要在变量中保存委托的实例。
private static CallbackDelegate delegateInstance;
....
delegateInstance = MyFunc;
In the simple example that you have here, the C++ code does not use the unmanaged function pointer outside of TestDelegate
, but in a more complex example you may do so, in which case you must keep the unmanaged function pointer alive. 在这里的简单示例中,C ++代码不使用TestDelegate
之外的非托管函数指针,但在更复杂的示例中,您可以这样做,在这种情况下,您必须保持非托管函数指针处于活动状态。
The function that you import is declared like this: 您导入的函数声明如下:
[DllImport("DllName.dll")]
public extern static double TestDelegate(CallbackDelegate f);
You can then call it like this: 然后你可以像这样调用它:
double retval = TestDelegate(delegateInstance);
On the C++ side, I'd explicitly specify the calling convention for the callback, eg __stdcall
(you haven't done that in your code, and I think the default is __cdecl
): 在C ++方面,我明确指定了回调的调用约定 ,例如__stdcall
(你的代码中没有这样做,我认为默认是__cdecl
):
// Include the calling convention (__stdcall) for the Callback
typedef double (__stdcall * Callback)(double);
// Just use "Callback" here, instead of repeating
// the above function prototype
extern "C" __declspec(dllexport) __stdcall double TestDelegate(Callback func)
{
return func(25.0);
}
// BTW: Can export also using .DEF file to avoid __stdcall name mangling
On the C# side, you can try something like this: 在C#方面,您可以尝试这样的事情:
public delegate double CallbackDelegate(double x);
// PInvoke declaration for the native DLL exported function
[DllImport("YourDLL.dll", CallingConvention = CallingConvention.StdCall)]
public static extern double TestDelegate(CallbackDelegate func);
private double MyFunctionCallback(double x)
{
// ... Implement your C# callback code ...
}
CallbackDelegate managedDelegate = new CallbackDelegate(MyFunctionCallback);
// Call into the native DLL, passing the managed callback
TestDelegate(managedDelegate);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.