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

9

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!
}

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

    }

编译没有错误,但运行时VS2012显示“访问冲突异常”的错误。

我已经搜索并尝试了很多方法,比如传递委托而不是IntPtr,但它们都失败了。

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


可能是[将C++中的函数指针传递给C#以调用 - 函数参数包括宽字符字符串(LPCWSTR)]的重复问题(https://dev59.com/q1jUa4cB1Zd3GeqPNgpF)。 - Waseem Anwar
我移除了COM标签,因为这个问题与COM无关;PInvoke标签更加合适。 - Mr.C64
1
委托声明是错误的,但这还不足以解释AVE。确保您知道如何调试C++代码,以便您可以诊断此崩溃。显示您获得的堆栈跟踪。 - Hans Passant
2个回答

17

你的代理使用cdecl调用约定。在C#中,您应该这样声明代理:

你的代理使用cdecl调用约定。在C#中,因此您应该像这样声明代理:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate double CallbackDelegate(double x);

作为另一种选择,您可以决定将C ++中的函数指针声明为__stdcall,这样您将删除UnmanagedFunctionPointer属性,并依赖于默认调用约定为CallingConvention.StdCall

按照以下方式实现:

public static double MyFunc(double x)
{
    return Math.Sqrt(x);
}
为了保持未管理的函数指针的生存(以防止垃圾回收),您需要在变量中保存委托实例。
private static CallbackDelegate delegateInstance;
....
delegateInstance = MyFunc;
在这里给出的简单示例中,C++ 代码没有在 TestDelegate 之外使用非托管函数指针,但在更复杂的示例中,您可能会这样做,在这种情况下,您必须保持非托管函数指针处于活动状态。
导入的函数声明如下:
[DllImport("DllName.dll")]
public extern static double TestDelegate(CallbackDelegate f);
你可以这样调用它:
double retval = TestDelegate(delegateInstance);

3
在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

在 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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接