I'm working on a win32 C++ program that has a fairly simple interface that isn't particularly dynamic. When I was looking around, I found the option of graphically creating a dialog in the resource editor and just using that as my main window. This worked well, was easy to deal with, and was doing everything it should. Until...
One of the things I'm trying to implement is for the program to prevent the computer from turning off when it's doing certain operations in order to avoid problems. I read through the Microsoft docs, which were straightforward enough, and implemented it, but nothing seemed to work. Then I stumbled upon this page, which pointed me towards what the problem could be:
Note that the system does not allow console applications or applications without a visible window to cancel shutdown.
Which got me wondering if a dialog doesn't count for some reason. So I tried to temporarily switch to an empty window as the main window, and registering it (something you can't do like that with a dialog) - and sure enough, it is suddenly able to properly intercept the shutdown attempt.
So how do I properly intercept the shutdown attempt when my main window is a dialog?
My code looks somewhat like this:
INT_PTR CALLBACK DialogProc(HWND dialogHandle, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_QUERYENDSESSION:
return FALSE;
case WM_ENDSESSION:
return FALSE;
// code for handling other window/dialog events
}
return false;
}
I tried to return DefWindowProc
as a default, which actually works to block the shutdown, but causes problems when I open another dialog. Also, according to this documentation, it shouldn't be used.
So how should I go about this?
A dialog box must specify the return value by setting DWLP_MSGRESULT
with SetWindowLongPtr
and then returning true.
switch(uMsg)
{
case WM_QUERYENDSESSION:
SetWindowLongPtr(hwndDialog, DWLP_MSGRESULT, 0);
return TRUE;
}
return FALSE;