Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.4 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: cmexitwin.cpp
  4. //
  5. // Module: Common Code
  6. //
  7. // Synopsis: Implements the function MyExitWindowsEx.
  8. //
  9. // Copyright (c) 1998-1999 Microsoft Corporation
  10. //
  11. // Author: quintinb Created Heaser 08/19/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include <windows.h>
  15. BOOL MyExitWindowsEx(UINT uFlags,
  16. DWORD dwRsvd)
  17. {
  18. BOOL bRes;
  19. //
  20. // If platform is NT, we will have to adjust privileges before rebooting
  21. //
  22. if (OS_NT)
  23. {
  24. HANDLE hToken; // handle to process token
  25. TOKEN_PRIVILEGES tkp; // ptr. to token structure
  26. //
  27. // Get the current process token handle
  28. // so we can get shutdown privilege.
  29. //
  30. if (!OpenProcessToken(GetCurrentProcess(),
  31. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  32. &hToken))
  33. {
  34. CMTRACE1(TEXT("MyExitWindowsEx() OpenThreadToken() failed, GLE=%u."), GetLastError());
  35. return FALSE;
  36. }
  37. //
  38. // Get the LUID for shutdown privilege
  39. //
  40. bRes = LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  41. &tkp.Privileges[0].Luid);
  42. #ifdef DEBUG
  43. if (!bRes)
  44. {
  45. CMTRACE1(TEXT("MyExitWindowsEx() LookupPrivilegeValue() failed, GLE=%u."), GetLastError());
  46. }
  47. #endif
  48. tkp.PrivilegeCount = 1;
  49. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  50. //
  51. // Get shutdown privilege for this process
  52. //
  53. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  54. (PTOKEN_PRIVILEGES) NULL, 0);
  55. //
  56. // Cannot reliably test the return value of AdjustTokenPrivileges
  57. //
  58. if (GetLastError() != ERROR_SUCCESS)
  59. {
  60. CMTRACE1(TEXT("MyExitWindowsEx() AdjustTokenPrivileges() failed, GLE=%u."), GetLastError());
  61. CloseHandle(hToken);
  62. return FALSE;
  63. }
  64. CloseHandle(hToken);
  65. }
  66. bRes = ExitWindowsEx(uFlags,dwRsvd);
  67. #ifdef DEBUG
  68. if (!bRes)
  69. {
  70. CMTRACE1(TEXT("MyExitWindowsEx() ExitWindowsEx() failed, GLE=%u."), GetLastError());
  71. }
  72. #endif
  73. return (bRes);
  74. }