简体   繁体   中英

Passing C# function pointers into C++/CLI interop dll

I'm trying to pass a function pointer from C# into C++/CLI and getting a windows compiler error stating that the ManagedTakeCallback function is not supported by this language (C#)--I define the ManagedTakeCallback in the C++/CLI interop. My code looks like

C# application:

namespace ManagedConsoleApplication

{

class Callback
    {
        public delegate double DelegateAdd(double value1, double value2);

        public static double CallbackAdd(double value1, double value2)
        {
            return value1 + value2;
        }

        public static DelegateAdd delegateCallback = new DelegateAdd(Callback.CallbackAdd); //declare as static to prevent GC

    }

    class Program
    {
       // [DllImport("InteropDLL.dll", CallingConvention = CallingConvention.StdCall)]
       // public static extern void ManagedCallback(IntPtr pCallback);
        static void Main(string[] args)
        {
            InteropDLL io = new InteropDLL(); 
            Console.WriteLine("The Add return = {0}", io.ManagedAdd(3, 2));
            Console.WriteLine("Pass CallBack to Unmanaged code");
            Callback cb = new Callback();
            IntPtr intptr_delegate = Marshal.GetFunctionPointerForDelegate(Callback.delegateCallback); //convert delegate to function pointer which can be used by unmanaged code
            Console.WriteLine("The callback return is {0}", io.ManagedTakeCallback(intptr_delegate)); 
            Console.WriteLine("Please hit Enter to exit");
            String value = Console.In.ReadLine();
            //Console.WriteLine("End of program ", value);
        }
    }
}

and,

C++/CLI interop dll h and cpp file:

//HEADER

namespace Interop
{

typedef double (__stdcall *PCallback)(double value1, double value2);

    public ref class InteropDLL
    {
    public:
        double ManagedAdd(double value1, double value2);

    public:
        double ManagedTakeCallback(PCallback pCallback);
    };
}

//CPP

double Interop::InteropDLL::ManagedAdd(double value1, double value2)
{
    return NativeAdd(value1, value2);
}

double Interop::InteropDLL::ManagedTakeCallback(PCallback pCallback)
{
    return NativeTakeCallback();
}

The C++/CLI interop layer then calls a C DLL. I'm able to call ManagedAdd interop function; however, if ManagedTakeCallback is added, there is a windows compiler error. I suspect that the C# application is not marshaling in the function pointer correctly via ManagedTakeCallback function or that the signature is not correct on the C++/CLI side? I would greatly appreciate any insight.

Here is comment from on site:

"But C# doesn't suport C++ function pointer, so we cannot invoke a C++ function pointer here. C# only has Delegate objects and we have to convert the function pointer to Delegate by Marshal.GetDelegateForFunctionPointer. It is declared in System.Runtime.InteropServices as follows:

public static Delegate GetDelegateForFunctionPointer (
      IntPtr ptr,
      Type t)

for full answer look at link: http://www.codeproject.com/Articles/27298/Dynamic-Invoke-C-DLL-function-in-C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM