Reputation: 282865
I'm trying to wrap this function defined by SDL2.
It's signature is
void SDL_AddEventWatch(SDL_EventFilter filter, void* userdata)
Where SDL_EventFilter
is
typedef int (SDLCALL * SDL_EventFilter) (void *userdata, SDL_Event * event);
Thus, I've defined my wrapper like so:
public delegate int EventFilter(IntPtr userData, IntPtr type);
[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_AddEventWatch")]
public static extern void AddEventWatch(EventFilter filter, IntPtr userData);
And I'm testing it like so:
SDL.AddEventWatch((data, e) =>
{
return 0;
}, IntPtr.Zero);
When I run my program it actually enters the lambda function, but then immediately crashes as soon as it exits the function ("vshost32.exe has stopped working").
What might be causing the crash?
Upvotes: 2
Views: 662
Reputation: 941455
#define SDLCALL __cdecl
You have a calling convention mismatch. Your native code requires a __cdecl function but your C# code declares a delegate that will be mapped to a an __stdcall callback. The default for unmanaged code interop. You must declare it like this:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int EventFilter(IntPtr userData, IntPtr type);
Upvotes: 3