Note that there are some explanatory texts on larger screens.

plurals
  1. POGracefully closing a windowless app in WinAPI
    primarykey
    data
    text
    <p>I'm writing a little utility app to control the system master volume via hotkeys for my GF, whose laptop is for some reason deprived of such function keys. I whipped up the code pretty much instantly and I've got the main functionality working perfectly; however, since I'm not creating any windows (just a message loop handling the WM_HOTKEY message), I can't terminate the app in a more elegant manner than just gracelessly terminating the process (also, when the system is shutting down, it shows the "should I wait for the process to end or kill it now" window with some rubbish in the place where the window title usually is).</p> <p>Is there any way of doing this which doesn't involve creating a fake window just for the sake of intercepting WM_CLOSE messages?</p> <p>Here's the code (I left out the mixer control functions intentionally, they're irrelevant to the question):</p> <pre><code>int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { MSG msg; int step; MixerInfo_t mi; HANDLE mutex; mutex = CreateMutex(NULL, TRUE, "volhotkey"); if (mutex == NULL) return 1; if (GetLastError() == ERROR_ALREADY_EXISTS) return 0; RegisterHotKey(NULL, 1, MOD_ALT | MOD_CONTROL, VK_F5); RegisterHotKey(NULL, 2, MOD_ALT | MOD_CONTROL, VK_F6); RegisterHotKey(NULL, 3, MOD_ALT | MOD_CONTROL, VK_F7); mi = GetMixerControls(); step = (mi.maxVolume - mi.minVolume) / 20; while (GetMessage(&amp;msg, NULL, 0, 0)) { switch (msg.message) { case WM_HOTKEY: switch (msg.wParam) { case 1: AdjustVolume(&amp;mi, -step); break; case 2: AdjustVolume(&amp;mi, step); break; case 3: SetMute(&amp;mi, !IsMuted(&amp;mi)); break; } MessageBeep(MB_ICONASTERISK); break; case WM_DESTROY: PostQuitMessage(0); break; default: break; } } UnregisterHotKey(NULL, 1); UnregisterHotKey(NULL, 2); return msg.wParam; } </code></pre> <p>Thanks in advance!</p> <p>Oh, and for the record, WM_DESTROY is also never posted.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload