问题
When I compile the following code (with Visual C++ 2010 Express) with
cl test.cpp
and run the .exe
, and then shutdown Windows 7, I get an error message like:
The instruction 0x00f....9 in explorer.exe cannot access memory at 0x00000000. Memory cannot be read.
I tried lots of modifications in the following code, but cannot solve this issue. Another remark: I'm really sure it's this program which is responsible for the crash: I don't run it, no shutdown error message, if I run it, there is one.
What could be the reason of the crash ?
Source file downloadable here.
#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;
}
来源:https://stackoverflow.com/questions/27856507/error-message-when-shutdown