c# Callback from unmanaged DLL: InvalidFunctionPointerInDelegate was detected RRS feed

  • Question

  • [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    internal struct MyStruct
           [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
           public byte[] data1;			
           public wanted_callback callback;
           public IntPtr user_info;			
           [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
           public byte[] data2;			
           public int data3;
           public byte data4;
    public delegate void wanted_callback(ref MyStruct cb);
    [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
    private extern static int RegisterCallback(wanted_callback MyVoid);
    [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
    public extern static IntPtr Function1(MyStruct str);
    [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl)]
    public extern static IntPtr Function2(MyStruct str);
    private static wanted_callback wanted_callback_Handler;
    static void Main(string[] args)
       wanted_callback_Handler = new wanted_callback(My_Call_Back);
       int succ = RegisterCallback(wanted_callback_Handler);
    private static void My_Call_Back(ref MyStruct str)
       IntPtr result1 = Function1(str);        
       IntPtr result2 = Function2(str);

    Hi to all,
    I'm having problems with the above code. From my C# application I need to receive a callback from an unmanaged DLL (closed source). The callback return "MyStruct" filled with internal data. This way, the filled struct can be used to call other functions like "Function1" and "Function2".

    The main problem is that If I leave the "ref" keyword in "wanted_callback" delegate and also in "My_Call_Back" void, when I call "Function1" and "Function2", the returned IntPtr is null (IntPtr.zero).

    If I remove the "ref" keyword, the above functions returns valid handles and I'm able to correctly process all the data, but, at the same time, as soon as I register the callback with "RegisterCallback(wanted_callback_Handler);", Visual Studio 2010 immediately show the following message:

    InvalidFunctionPointerInDelegate was detected
    Message: Invalid function pointer 0x1 was passed into the runtime to be converted to a delegate. Passing in invalid function pointers to be converted to delegates can cause crashes, corruption or data loss.

    Should be an easy fix, but after one day of researches I still didn't found a solution and opening this thread is my last chance.

    Any help is really appreciated.

    Thursday, March 15, 2012 3:57 AM


All replies