HOWTO: Programmatically Force a Logoff on Windows 95Last reviewed: May 20, 1997Article ID: Q168690 |
The information in this article applies to:
SUMMARYYou 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().
MORE INFORMATIONExitWindowsEx() with the EWX_LOGOFF flag alone does not force non- responsive applications to close. To achieve this, follow these steps:
NOTE: This method is not 100% successful. There are a few applications that cause ExitWindowsEx() to fail. This article will be updated when more information is available. NOTE: ExitWindowsEx does not work correctly if it is called from a Win32 Console application. This must be a Windows application to work properly.
Sample CodeThe following sample code demonstrates the technique:
#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 // we 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); // // bye-bye // TerminateProcess(hProcess, 0); } // // continue the enumeration // return TRUE; } |
Additional query words: 95 win95 ExitWindowsEx FORCE LOGOFF
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |