Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a callback function with parameters when using P/Invoke
    primarykey
    data
    text
    <p>I'm calling an external C library from a C# wrapper that I'm writing. One of the functions I'm calling is to register a callback function. I get no errors when I call the <code>setCallback</code> function but when the callback function is later called I get errors, unfortunately unspecified by the C library.</p> <p>Here's the C method (from the header file)</p> <pre><code>DLLExport int setCallbacks(Client handle, void* context, connectionLost* cl, messageArrived* ma, deliveryComplete* dc); </code></pre> <p>Here's my C# declaration of the method</p> <pre><code>[DllImport("some.dll", CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] private static extern int setCallbacks(IntPtr client, IntPtr context, MulticastDelegate connectionLost, MulticastDelegate messageArrived, MulticastDelegate messageDelivered); </code></pre> <p>These are my delegates</p> <pre><code>private delegate int ConnectionLostDelegate(IntPtr context, [MarshalAs(UnmanagedType.LPStr)] String cause); private delegate int MessageArrivedDelegate(IntPtr context, [MarshalAs(UnmanagedType.LPStr)] String title, Int32 titleLength, MessageObject message); private delegate int MessageDeliveredDelegate(IntPtr context, int deliveryToken); </code></pre> <p>This is how I call the method</p> <pre><code>ConnectionLostDelegate conLost = new ConnectionLostDelegate(ConnectionLost); MessageArrivedDelegate mesArr = new MessageArrivedDelegate(MessageArrived); MessageDeliveredDelegate mesDel = new MessageDeliveredDelegate(MessageDelivered); result = setCallbacks(client, IntPtr.Zero, conLost, mesArr, mesDel); </code></pre> <p>Here's the struct that is used by one of the callbacks</p> <pre><code>[StructLayout(LayoutKind.Sequential)] public class MessageObject { [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.U1)] public byte[] struct_id; Int32 struct_version; Int32 payloadlen; IntPtr payload; Int32 qos; Int32 retained; Int32 dup; Int32 msgid; public MessageObject() { struct_id = Encoding.ASCII.GetBytes("WXYZ"); struct_version = 0; payload = IntPtr.Zero; } } </code></pre> <p>C Headers for callback functions as follows..</p> <pre><code>typedef int messageArrived(void* context, char* title, int titleLen, MessageObject* message); typedef void deliveryComplete(void* context, int token); typedef void connectionLost(void* context, char* cause); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload