2

On Windows I get WM_CLOSE message which is handled by WndProc, which notifies application is being quit either through -

  1. User clicked cross(X) button
  2. User pressed Alt+F4 Keyboard shortcut
  3. User right click on application icon in taskbar and clicked "Close window"/"Close all windows"
  4. User went to Task Manager and from Applications tab, clicked on End Task

Before even WM_CLOSE, windows sends following message and we can use it to detect the above:

message=WM_SYSCOMMAND (WndProc) and wParam=SC_CLOSE and lParam != 0 implies "1." i.e. Cross(X) button

message=WM_SYSCOMMAND (WndProc) and wParam=SC_CLOSE and lParam = 0 implies "2.", "3." or "4."

Please note "2." can be detected by listening WM_SYSKEYDOWN and wParam = VK_F4 (received even before WM_SYSCOMMAND)

Now any idea how do I distinguish between "3." and "4." as well?

Ultimately I need to do something when my application is killed from applications tab in Task Manager.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • 1
    call [`InSendMessage()`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-insendmessage) when you process `WM_CLOSE` in case 3 it return *false* and in case 4 (from task manager) *true* – RbMm May 28 '20 at 19:48
  • Note that this means the the close message came via `SendMessage`, will also apply to other external ways of ending a program, such as taskkill.exe. It's not exclusively the "applications tab in Task Manager". – Eryk Sun May 30 '20 at 13:53

0 Answers0