【发布时间】:2016-10-30 00:58:55
【问题描述】:
我正在 .NET 核心上的 C# 控制台应用程序中通过 P/Invoke Win32 API 创建一个窗口。以下是核心代码。
class WindowContext
{
public IWindow MainLoop(Action guiMethod)// this is called somewhere else
{
MSG msg = new MSG();
while (msg.message != 0x12/*WM_QUIT*/)
{
if (PeekMessage(ref msg, IntPtr.Zero, 0, 0, 0x0001/*PM_REMOVE*/))
{
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}
}
}
private IntPtr WindowProc(IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam)
{
//....
}
public IWindow CreateWindow(Point position, Size size)// this is called to create a window
{
IntPtr hInstance = processHandle.DangerousGetHandle();
string szAppName = "ImGuiApplication~";
WNDCLASS wndclass;
wndclass.style = 0x0002 /*CS_HREDRAW*/ | 0x0001/*CS_VREDRAW*/;
wndclass.lpfnWndProc = WindowProc;
// RegisterClass(ref wndclass);
// CreateWindowEx(...)
// ...
}
}
但是一旦我将鼠标移到窗口上,程序就会不断崩溃。
程序“[18996] dotnet.exe”已退出,代码为 -1073740771 (0xc000041d)。
最后我发现调用 PeekMessage 时发生了崩溃。但我不知道为什么。
【问题讨论】: