J
Jef Driesen
I have a C DLL that I want to use from a C# project. The C header file
contains these declarations:
typedef void (*callback_t) (const unsigned char *data, unsigned int
size, void *userdata);
void myfunction (callback_t callback, void *userdata);
How do I translate this to C#?
I tried with:
delegate void callback_t (Byte[] data, UInt32 size, IntPtr userdata);
[DllImport("mydll.dll")]
static extern void myfunction (callback_t callback, IntPtr userdata);
When calling with myfunction (null, IntPtr.Zero) everything works as
expected. But once I start passing a callback function, the application
crashes with "Unhandled Exception: System.AccessViolationException:
Attempted to read or write protected memory. This is often an indication
that other memory is corrupt."
void test (Byte[] data, UInt32 size, IntPtr userdata)
{
// Nothing here
}
callback_t callback = new callback_t (test);
myfunction (callback, IntPtr.Zero);
I tried changing the data parameter from a byte array to an IntPtr, but
that seems to make no difference. What am I doing wrong? All other
functions (without a callback function parameter) work perfect.
contains these declarations:
typedef void (*callback_t) (const unsigned char *data, unsigned int
size, void *userdata);
void myfunction (callback_t callback, void *userdata);
How do I translate this to C#?
I tried with:
delegate void callback_t (Byte[] data, UInt32 size, IntPtr userdata);
[DllImport("mydll.dll")]
static extern void myfunction (callback_t callback, IntPtr userdata);
When calling with myfunction (null, IntPtr.Zero) everything works as
expected. But once I start passing a callback function, the application
crashes with "Unhandled Exception: System.AccessViolationException:
Attempted to read or write protected memory. This is often an indication
that other memory is corrupt."
void test (Byte[] data, UInt32 size, IntPtr userdata)
{
// Nothing here
}
callback_t callback = new callback_t (test);
myfunction (callback, IntPtr.Zero);
I tried changing the data parameter from a byte array to an IntPtr, but
that seems to make no difference. What am I doing wrong? All other
functions (without a callback function parameter) work perfect.