An application can carry out any system command at any time by passing a WM_SYSCOMMAND message to DefWindowProc.
If I don't have any windows, which window can I post/send WM_SYSCOMMAND to carry out system commands?
First, GetDesktopWindow() doesn't work.
Second, while GetForegroundWindow() works most of the time, it won't work if the foreground window decides to intercept WM_SYSCOMMAND or has a higher integrity level.
And HWND_BROADCAST is the worst option.
So what about GetShellWindow()?
Or, can I direct call DefWindowProcW(RANDOM_WINDOW, WM_SYSCOMMAND, ...)? It seems that this will work as long as RANDOM_WINDOW is any valid HWND, and the integrity level is no higher than caller.
( I'm raising this question because there are some terrible answers about turning off the monitor, such as SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2). While some have warned against this in the comments, and Raymond Chen has even written an article about it, even Raymond Chen's solution involves creating a custom window. To fix(edit) these terrible answers, I want to provide an equally simple alternative solution. Because I can't just say "use any your own HWND, if you don't have any window, then create one". For those who unfamiliar with Win32, they won't want to create a custom window specifically for this, and they're unaware of the flaws in these solutions. )
Edit: It seems everyone thinks I don't know how to create a window or correctly turn off the monitor.
void TurnOffMonitor() {
auto TurnOffMonitorWndproc = [](HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) ->LRESULT {
if (uMsg == WM_NCCREATE) {
DefWindowProcW(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
return FALSE;
}
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
};
WNDCLASSW wc{};
wc.lpfnWndProc = TurnOffMonitorWndproc;
wc.lpszClassName = L"TOMWC";
ATOM classATOM = RegisterClassW(&wc);
CreateWindowExW(0, MAKEINTATOM(classATOM), L"", 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
UnregisterClassW(MAKEINTATOM(classATOM), 0);
}
Although I must admit, I don't know how to do this in a language other than C/C++.
DefWindowProc()directly. I would break down the preconceptions and create a window for this. It doesn't have to be visible.