home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / APPLAUNC.PAK / EXITWIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  1.9 KB  |  58 lines

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