The OS notifies the console programs of various events through "control signals". Call SetConsoleCtrlHandler to configure the function to call the OS to deliver the signals. The signal for the closed window is CTRL_CLOSE_EVENT .
function ConsoleEventProc(CtrlType: DWORD): BOOL; stdcall; begin if (CtrlType = CTRL_CLOSE_EVENT) then begin // optionally run own code here // ... end; Result := True; end; ... begin SetConsoleCtrlHandler(@ConsoleEventProc, True); // my application code here // ... end.
source share