HOWTO: Programmatically Force a Logoff on Windows 95 |
Q168690
You can use the Win32 API ExitWindowsEx() on Windows NT to force a logoff.
You accomplish this by combining the EWX_LOGOFF and EWX_FORCE flags.
Unfortunately, this combination is not supported on Windows 95.
You can duplicate this capability on Windows 95 by taking some additional
steps before calling ExitWindowsEx().
ExitWindowsEx() with the EWX_LOGOFF flag alone does not force non-responsive applications to close. To achieve this, follow these steps:
#include <windows.h>
BOOL CALLBACK EnumWindowsProc(
HWND hwnd,
DWORD lParam
);
//
// EnumWindowsProc must be called from a Windows
// application on Windows 95.
//
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//
// Close all open applications.
//
EnumWindows(EnumWindowsProc, 0);
// Now do a regular logoff.
ExitWindowsEx(EWX_LOGOFF , 0);
}
BOOL CALLBACK EnumWindowsProc(
HWND hwnd,
DWORD lParam
)
{
DWORD pid = 0;
LRESULT lResult;
HANDLE hProcess;
DWORD dwResult;
lResult = SendMessageTimeout(
hwnd,
WM_QUERYENDSESSION,
0,
ENDSESSION_LOGOFF,
SMTO_ABORTIFHUNG,
2000,
&dwResult);
if( lResult )
{
//
// Application will terminate nicely, so let it.
//
lResult = SendMessageTimeout(
hwnd,
WM_ENDSESSION,
TRUE,
ENDSESSION_LOGOFF,
SMTO_ABORTIFHUNG,
2000,
&dwResult);
}
else // You have to take more forceful measures.
{
//
// Get the ProcessId for this window.
//
GetWindowThreadProcessId( hwnd, &pid );
//
// Open the process with all access.
//
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
//
// Terminate the process.
//
TerminateProcess(hProcess, 0);
}
//
// Continue the enumeration.
//
return TRUE;
}
Additional query words: win95 ExitWindowsEx FORCE LOGOFF
Keywords : kbnokeyword kbKernBase kbThread kbGrpDSKernBase
Issue type : kbhowto
Technology : kbAudDeveloper kbWin32sSearch kbWin32API
|
Last Reviewed: December 16, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |