Dll Callback and shared memory [modified]
-
I've a third party DLL that exports this function:
typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);
__declspec(dllimport) void SetCallback(RESPONSE_FUNC pfResponse, UCHAR* responseBuffer);
I must allocate the buffer, call the SetCallback function, and read the modified data when the callback function is called. I tried with this code:
static byte[] responseBuffer = new byte[10];
delegate bool RESPONSE_FUNC(byte eventID);
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern void ANT_SetCallback (RESPONSE_FUNC callback, byte[] responseBuffer);public void test
{
ANT_SetCallback (EventCallback, responseBuffer);
}static bool EventCallback(byte eventID)
{
return true;
}When the dll doesn't try to modify the buffer then the Callback function is called without errors(the first event doesn't access the buffer). But when the DLL tries to modify the data in the buffer, the program crashes with a 0xc0000409 exception. I'm stuck on this, any suggestion ?
modified on Sunday, November 9, 2008 1:01 PM
-
I've a third party DLL that exports this function:
typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);
__declspec(dllimport) void SetCallback(RESPONSE_FUNC pfResponse, UCHAR* responseBuffer);
I must allocate the buffer, call the SetCallback function, and read the modified data when the callback function is called. I tried with this code:
static byte[] responseBuffer = new byte[10];
delegate bool RESPONSE_FUNC(byte eventID);
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern void ANT_SetCallback (RESPONSE_FUNC callback, byte[] responseBuffer);public void test
{
ANT_SetCallback (EventCallback, responseBuffer);
}static bool EventCallback(byte eventID)
{
return true;
}When the dll doesn't try to modify the buffer then the Callback function is called without errors(the first event doesn't access the buffer). But when the DLL tries to modify the data in the buffer, the program crashes with a 0xc0000409 exception. I'm stuck on this, any suggestion ?
modified on Sunday, November 9, 2008 1:01 PM
This
typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);
should be
typedef UCHAR (
__stdcall
*RESPONSE_FUNC)(UCHAR eventID);Also, make sure you pass a buffer large enough to accomodate whatever writing the DLL is doing in it :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
This
typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID);
should be
typedef UCHAR (
__stdcall
*RESPONSE_FUNC)(UCHAR eventID);Also, make sure you pass a buffer large enough to accomodate whatever writing the DLL is doing in it :) Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
This typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID); should be typedef UCHAR (__stdcall *RESPONSE_FUNC)(UCHAR eventID);
It's a third party library, I'm not able to change it :( And yes, I'm sure the buffer is bigh enough... it should be 3 bytes, I'm passing 10 ;)
-
Mark Salsbery wrote:
This typedef UCHAR (__cdecl *RESPONSE_FUNC)(UCHAR eventID); should be typedef UCHAR (__stdcall *RESPONSE_FUNC)(UCHAR eventID);
It's a third party library, I'm not able to change it :( And yes, I'm sure the buffer is bigh enough... it should be 3 bytes, I'm passing 10 ;)
Paolo Vernazza wrote:
I'm not able to change it
Oops, sorry. On the C# side...
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate bool RESPONSE_FUNC(byte eventID);Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Paolo Vernazza wrote:
I'm not able to change it
Oops, sorry. On the C# side...
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate bool RESPONSE_FUNC(byte eventID);Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Great, it worked!