Answered by:
c# Callback from unmanaged DLL: InvalidFunctionPointerInDelegate was detected

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; }; [UnmanagedFunctionPointer(CallingConvention.Cdecl)] 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
Answers
-
Hi Mark,
Have you gone through below links:
http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
Regards, http://shwetamannjain.blogspot.com
- Proposed as answer by Shweta Jain (Lodha) Friday, March 16, 2012 4:34 AM
- Marked as answer by mark_555 Friday, March 16, 2012 11:43 PM
- Unmarked as answer by mark_555 Friday, March 16, 2012 11:43 PM
- Marked as answer by mark_555 Friday, March 16, 2012 11:44 PM
Friday, March 16, 2012 4:34 AM -
Hi Shweta and thanks for your reply, but unfortunately the links didn't helped with my problem.
After playing a little with MyStruct definition I discovered that data returned from callback was not correctly marshalled. I changed the layout from sequential into explicit and I've added FieldOffsetAttribute for each field and nothing more. After this minor changes I was able to get valid handles from functions without removing the ref keyword (required to mantain a valid function pointer).
Thanks
- Marked as answer by Mike Feng Saturday, March 17, 2012 1:46 AM
Friday, March 16, 2012 11:44 PM
All replies
-
Hi Mark,
Have you gone through below links:
http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
Regards, http://shwetamannjain.blogspot.com
- Proposed as answer by Shweta Jain (Lodha) Friday, March 16, 2012 4:34 AM
- Marked as answer by mark_555 Friday, March 16, 2012 11:43 PM
- Unmarked as answer by mark_555 Friday, March 16, 2012 11:43 PM
- Marked as answer by mark_555 Friday, March 16, 2012 11:44 PM
Friday, March 16, 2012 4:34 AM -
Hi Shweta and thanks for your reply, but unfortunately the links didn't helped with my problem.
After playing a little with MyStruct definition I discovered that data returned from callback was not correctly marshalled. I changed the layout from sequential into explicit and I've added FieldOffsetAttribute for each field and nothing more. After this minor changes I was able to get valid handles from functions without removing the ref keyword (required to mantain a valid function pointer).
Thanks
- Marked as answer by Mike Feng Saturday, March 17, 2012 1:46 AM
Friday, March 16, 2012 11:44 PM