相关疑难解决方法(0)

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

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); …
Run Code Online (Sandbox Code Playgroud)

c# c++ dll pinvoke delegates

5
推荐指数
1
解决办法
4233
查看次数

标签 统计

c# ×1

c++ ×1

delegates ×1

dll ×1

pinvoke ×1