如何将函数指针从C#传递给C++ Dll?

All*_*ria 5 c# c++ dll pinvoke delegates

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!
}
Run Code Online (Sandbox Code Playgroud)

我试着用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;
        }

    }
Run Code Online (Sandbox Code Playgroud)

它编译时没有错误.但是当它运行时,VS2012会显示"访问冲突异常"错误.

我已经搜索并尝试了很多方法,例如传递委托而不是IntPtr,但所有这些方法都失败了.

那么,在包含函数指针的dll中使用API​​函数的正确方法是什么?或者如何实现"My_Real_purpose"函数?

Dav*_*nan 7

您的委托使用cdecl调用约定.因此,在C#中,您将声明代理如下:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate double CallbackDelegate(double x);
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您可以决定在C++中声明函数指针__stdcall,在这种情况下,您将删除该UnmanagedFunctionPointer属性并依赖于默认的调用约定CallingConvention.StdCall.

像这样实现它:

public static double MyFunc(double x)
{
    return Math.Sqrt(x);
}
Run Code Online (Sandbox Code Playgroud)

为了使非托管函数指针保持活动状态(防止GC),您需要在变量中保存委托的实例.

private static CallbackDelegate delegateInstance;
....
delegateInstance = MyFunc;
Run Code Online (Sandbox Code Playgroud)

在这里的简单示例中,C++代码不使用外部的非托管函数指针TestDelegate,但在更复杂的示例中,您可以这样做,在这种情况下,您必须保持非托管函数指针处于活动状态.

您导入的函数声明如下:

[DllImport("DllName.dll")]
public extern static double TestDelegate(CallbackDelegate f);
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样调用它:

double retval = TestDelegate(delegateInstance);
Run Code Online (Sandbox Code Playgroud)