Passing C # function pointers to dlls C ++ / CLI

I am trying to pass a function pointer from C # to C ++ / CLI and get a Windows compiler error stating that the ManagedTakeCallback function ManagedTakeCallback not supported by this language (C #). I define a ManagedTakeCallback in a 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(); } 

Then the C ++ / CLI interaction layer calls the C DLL. I can call the interop function ManagedAdd ; however, if ManagedTakeCallback added, a Windows compiler error occurs. I suspect that the C # application is not marching correctly in the function pointer through the ManagedTakeCallback function ManagedTakeCallback or that the signature does not match the C ++ / CLI side? I would really appreciate your understanding.

+4
source share
1 answer

Here is a comment from the site:

"But C # does not support a pointer to a C ++ function, so we cannot refer to a pointer to a C ++ function. C # has only Delegate objects, and we need to convert the function pointer to Delegate by Marshal.GetDelegateForFunctionPointer. It is declared in System .Runtime. InteropServices is as follows:

 public static Delegate GetDelegateForFunctionPointer (
       IntPtr ptr,
       Type t)

for a complete answer, see the link: http://www.codeproject.com/Articles/27298/Dynamic-Invoke-C-DLL-function-in-C

0
source

Source: https://habr.com/ru/post/1382751/


All Articles