Win32 services that interact with the user should be prepared to handle logoff events. When a logoff event occurs, the Win32 service must close all handles to the user's window station and desktop.
This sample demonstrates how the message box in the interaction example code should be dismissed at logoff. The ConsoleCtrlHandler function in this example is a HandlerRoutine that was specified by a call to the SetConsoleCtrlHandler function.
BOOL CALLBACK EnumProc(
HWND hwnd,
LPARAM lParam)
{
/*
* Send a WM_CLOSE to destroy the window, because DestroyWindow does
* not work across threads.
*/
SendMessage(hwnd, WM_CLOSE, 0, 0);
return TRUE;
}
BOOL ConsoleCtrlHandler(
DWORD dwCtrlType)
{
if (dwCtrlType == CTRL_LOGOFF_EVENT && dwGuiThreadId != 0) {
SetThreadDesktop(GetThreadDesktop(dwGuiThreadId));
EnumThreadWindows(dwGuiThreadId, EnumProc, 0);
}
return FALSE;
}