I have the following code in Native C dll.
typedef void CallbackType( INT32 param1, INT32 param2 ); NATIVE_API void RegisterEventCallBack(CallbackType *callBackFunction);
I need to call this method from my C # code. After doing some of the solutions available on the stack, I tried this:
Delegate Declaration:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void CallbackType(Int32 param1, Int32 param2);
Method import declaration:
[DllImport("EtIPAdapter.dll")] public static extern void RegisterEventCallBack([MarshalAs(UnmanagedType.FunctionPtr)]CallbackType callbackFunc);
Call:
RegisterEventCallBack(ReceivedData); private static void ReceivedData(Int32 param1, Int32 param2) {
But this does not work, and I get the following error:
The PInvoke function call "RegisterEventCallBack" has an unbalanced stack. This is likely due to the fact that the PInvoke managed signature does not match the unmanaged target signature. Verify that the calling agreement and PInvoke signature settings match the target unmanaged signature.
I also tried passing the function pointer that I received using GetFunctionPointerFromDelegate(ReceivedDataDelegate) . But this also leads to the same error.
The error indicates a signature mismatch, but I do not see any obvious signature mismatch. Please, help.
source share