Microsoft KB Archive/168690: Difference between revisions

From BetaArchive Wiki
m (Text replacement - """ to """)
m (Text replacement - "&" to "&")
 
Line 95: Line 95:
           SMTO_ABORTIFHUNG,
           SMTO_ABORTIFHUNG,
           2000,
           2000,
           &dwResult);
           &dwResult);


       if( lResult )
       if( lResult )
Line 109: Line 109:
               SMTO_ABORTIFHUNG,
               SMTO_ABORTIFHUNG,
               2000,
               2000,
               &dwResult);
               &dwResult);


       }
       }
Line 117: Line 117:
           // Get the ProcessId for this window.
           // Get the ProcessId for this window.
           //  
           //  
           GetWindowThreadProcessId( hwnd, &pid );
           GetWindowThreadProcessId( hwnd, &pid );
           //  
           //  
           // Open the process with all access.
           // Open the process with all access.

Latest revision as of 11:29, 21 July 2020

HOWTO: Programmatically Force a Logoff on Windows 95

Q168690



The information in this article applies to:


  • Microsoft Win32 Application Programming Interface (API), included with:
    • Microsoft Windows 95





SUMMARY

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().



MORE INFORMATION

ExitWindowsEx() with the EWX_LOGOFF flag alone does not force non-responsive applications to close. To achieve this, follow these steps:

  1. Enumerate top-level windows with the EnumWindows() API.
  2. In the EnumWindowsProc, ask each application to shut down by sending it the WM_QUERYENDSESSION message.
  3. If the application responds positively, then send the WM_ENDSESSION message to direct the application to terminate.
  4. If the application responds negatively, terminate the application with the TerminateProcess() API.
  5. Once all applications with top-level windows have been shut down in this manner, you can call ExitWindowsEx(EWX_LOGOFF) with a reasonable assurance that the logoff operation will complete.

IMPORTANT: Using the TerminateProcess() API is not a "nice" way to shut down an application and could result in the loss of data. This technique should only be used if a machine must be guaranteed to perform the logoff operation while unattended.

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 Code

The following sample code demonstrates this 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  // 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.