home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / APPLAUNC.PAK / EXITWIN.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  2KB  |  54 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991, 1993 by Borland International
  3. //
  4. // ExitWin will exit Windows.  If "prompt" is given on the command-line then
  5. // the user will be prompted to confirm the exit.
  6. //
  7. //----------------------------------------------------------------------------
  8. #include <owl/owlpch.h>
  9. #include <osl/inlines.h>
  10. #include <string.h>
  11.  
  12. int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR clpLine, int)
  13. {
  14.   if (strstr(clpLine, "prompt") != 0)
  15.     if (::MessageBox(0, "Confirm!", "Exit Windows", MB_OKCANCEL) == IDCANCEL)
  16.       return 0;
  17.  
  18. #if defined(BI_PLAT_WIN16)
  19.   return ExitWindows(0,0);
  20.  
  21. #elif defined(BI_PLAT_WIN32)
  22.  
  23.   // Check for Win32s and use the old Win16 API version if so
  24.   //
  25.   if ((::GetVersion()&0x80000000) && (::GetVersion()&0xFF) < 4)
  26.     return ExitWindows(0,0);
  27.  
  28.   // Must be WinNT or Win 4.X. If NT, adjust privaleges first
  29.   //
  30.   if ((::GetVersion()&0xFF) < 4) {
  31.     HANDLE            hToken;
  32.     LUID              takeOwnershipValue;
  33.     TOKEN_PRIVILEGES  tkp;
  34.   
  35.     if (!OpenProcessToken(GetCurrentProcess(),
  36.                           TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  37.       return !::MessageBox(0, "Exit Error", "OpenProcessToken() failed", MB_OK);
  38.     if (!LookupPrivilegeValue(0, SE_SHUTDOWN_NAME, &takeOwnershipValue))
  39.       return !::MessageBox(0, "Exit Error", "LookupPrivilegeValue() failed", MB_OK);
  40.   
  41.     tkp.PrivilegeCount = 1;
  42.     tkp.Privileges[0].Luid = takeOwnershipValue;
  43.     tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  44.     AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), 0, 0);
  45.     if (GetLastError())
  46.       return !::MessageBox(0, "Exit Error", "AdjustTokenPrivileges() failed", MB_OK);
  47.   }
  48.  
  49.   // Now call the Win32 API to force windows to shutdown.
  50.   //
  51.   return ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN,0);
  52. #endif
  53. }
  54.