[英]Why does my program crash immediately after calling delegate?
I'm trying to wrap this function defined by SDL2. 我正在尝试包装SDL2定义的此函数 。
It's signature is 它的签名是
void SDL_AddEventWatch(SDL_EventFilter filter, void* userdata)
Where SDL_EventFilter
is SDL_EventFilter
在哪里
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"). 当我运行程序时,它实际上进入了lambda函数,但是一旦退出该函数,便立即崩溃(“ vshost32.exe已停止工作”)。
What might be causing the crash? 是什么原因导致坠机?
#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.
您的本机代码需要__cdecl函数,但是您的C#代码声明了一个将映射到__stdcall回调的委托。 The default for unmanaged code interop.
非托管代码互操作的默认值。 You must declare it like this:
您必须这样声明:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int EventFilter(IntPtr userData, IntPtr type);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.