Here is my code example:
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdParam, int iCmdShow)
{
static char szAppName[] = "hidden window";
HWND hWnd = GetActiveWindow();
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_OWNDC;
wndclass.lpfnWndProc = WindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = NULL;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hWnd = CreateWindow(
szAppName,
"hidden window", // window caption
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
// Some code
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ofstream myfile;
myfile.open("log.txt", ios::app);
myfile << "I was called \n";
switch (uMsg)
{
case WM_QUERYENDSESSION:
{
myfile << "WM_QUERYENDSESSION CALLED\n";
return TRUE;
break;
}
}
myfile.close();
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
I register handler by wndclass.lpfnWndProc = WindowProc;
and WindowProc is triggered few times and writes "I was called \n" only when I start app, but when I shutdown system absolutely nothing writes to log.txt, therefore handler is not triggered. I tried different examples, but nothing works to me. Any ideas what's wrong here?