I have an c dll that I need to call from my c# application.
I have it communicating with the dll, but I am having issues with callbacks.
The c looks like this.
typedef void (CDECLCALL_CONV *ConnectedCB) (DPHandle hdl);
typedef struct
{
unsigned int size;
ConnectedCB connectedCB;
.. more callbacks
} DPcbs;
BPS_DP_API unsigned long CDECLCALL_CONV DPSetCallBacks(DPHandle hdl, DPcbs *DPcbs);
What I have tried:
I have created a interface class that wraps the unmanaged code.
It looks like this
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)]
public delegate void ConnectedCB(ref DocProcHandle hdl);
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct DPcbs
{
public uint size;
public ConnectedCB connectedCB;
... more callbacks
}
[System.Runtime.InteropServices.DllImportAttribute("DocProc.dll", EntryPoint = "DPSetCallBacks", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern uint DPSetCallBacks(ref DocProcHandle hdl, ref DPcbs DPcbs);
Finally My c# calling code
{
...
m_events = new DPcbs();
m_events.connectedCB = connected;
m_events.size = Convert.ToUInt32(Marshal.SizeOf<DPcbs>());
wrapper.DPSetCallBacks(ref m_handle, ref m_events);
...
}
private void connected(ref DocProcHandle hdl)
{
}
The application "stopped working" - no error just a crash.
But it happens AFTER the connected method is called, and before anything else.
If I do not set the connected callback it does not crash. (it seems to crash if it tries to call any of my callbacks)
I have tried everything i can think of to make sure the method does not go through the GC.
The class is static, I have made the method static. I have Alloc'ed both the method and the event.
Is there anything I am missing or doing wrong?
Any suggestions on what to try?