当我使用
编译以下代码(使用Visual C ++ 2010 Express)时cl test.cpp
并运行.exe
,然后关闭Windows 7,我收到如下错误消息:
The instruction 0x00f....9 in explorer.exe cannot access memory at 0x00000000. Memory cannot be read.
我在以下代码中尝试了很多修改,但无法解决此问题。 另一句话:我确定这个程序导致崩溃:我没有运行它,没有关机错误消息,如果我运行它,就有一个。
崩溃的原因是什么?
#pragma comment(lib, "user32.lib")
#define UNICODE
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwndMain = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_LAYERED, wc.lpszClassName, 0, WS_POPUP | WS_VISIBLE | WS_SYSMENU, 0, 0, 300, 300, 0, 0, 0, 0);
SetLayeredWindowAttributes(hwndMain, 0, 192, LWA_ALPHA);
ShowWindow(hwndMain, nCmdShow);
// without the next 2 lines, no crash at shutdown
// but these 2 next lines are really important to make the main
// window part of the windows' desktop
// see comments on http://stackoverflow.com/a/27787003/1422096
HWND hwndOwner = GetWindow(GetWindow(GetTopWindow(0), GW_HWNDLAST), GW_CHILD);
SetWindowLong(hwndMain, GWL_HWNDPARENT, (LONG) hwndOwner);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}