WinShutdown.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // WinShutdown.cpp : 定义应用程序的类行为。
  2. //
  3. #include "stdafx.h"
  4. #include "WinShutdown.h"
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #endif
  8. CWinShutdown::CWinShutdown(void)
  9. {
  10. }
  11. CWinShutdown::~CWinShutdown(void)
  12. {
  13. }
  14. /************************************************************************/
  15. /* 函数:[11/28/2017 Home];
  16. /* 描述:;
  17. /* 参数:;
  18. /* [IN] IsRestart:TRUE表示关机并重启, 否则只关机;
  19. /* [OUT] :;
  20. /* [IN/OUT] :;
  21. /* 返回:void;
  22. /* 注意:;
  23. /* 示例:;
  24. /*
  25. /* 修改:;
  26. /* 日期:;
  27. /* 内容:;
  28. /************************************************************************/
  29. BOOL CWinShutdown::SystemShutdown(IN BOOL IsReboot /* = FALSE */)
  30. {
  31. HANDLE hToken;
  32. TOKEN_PRIVILEGES tkp;
  33. // Get a token for this process.
  34. if (!OpenProcessToken(GetCurrentProcess(),
  35. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  36. return( FALSE );
  37. // Get the LUID for the shutdown privilege.
  38. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
  39. tkp.PrivilegeCount = 1; // one privilege to set
  40. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  41. // Get the shutdown privilege for this process.
  42. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  43. if (GetLastError() != ERROR_SUCCESS)
  44. return FALSE;
  45. // 关机类型;
  46. UINT uFlags = EWX_SHUTDOWN | EWX_FORCE;
  47. if ( IsReboot )
  48. uFlags = EWX_RESTARTAPPS | EWX_FORCE;
  49. // 关机原因;
  50. DWORD dwReason = SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED;
  51. // Shut down the system and force all applications to close.
  52. if (!ExitWindowsEx(uFlags, dwReason))
  53. return FALSE;
  54. //shutdown was successful
  55. return TRUE;
  56. }
  57. /************************************************************************/
  58. /* 函数:[11/28/2017 Home];
  59. /* 描述:;
  60. /* 参数:;
  61. /* [IN] lpMsgToUser:;
  62. /* [IN] dwTimeout:;
  63. /* [IN] bForceAppsClosed:true强制关闭应用程序, false提示用户保存变更;
  64. /* [IN] bRebootAfterShutdown:true 关机后重启;
  65. /* 返回:void;
  66. /* 注意:;
  67. /* 示例:;
  68. /*
  69. /* 修改:;
  70. /* 日期:;
  71. /* 内容:;
  72. /************************************************************************/
  73. BOOL CWinShutdown::SystemShutdownBox(IN LPTSTR lpMsgToUser, IN DWORD dwTimeout /* = 30 */, IN BOOL bForceAppsClosed /* = FALSE */, IN BOOL bRebootAfterShutdown /* = FALSE */)
  74. {
  75. HANDLE hToken; // handle to process token
  76. TOKEN_PRIVILEGES tkp; // pointer to token structure
  77. BOOL fResult; // system shutdown flag
  78. // Get the current process token handle so we can get shutdown privilege.
  79. if (!OpenProcessToken(GetCurrentProcess(),
  80. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  81. return FALSE;
  82. // Get the LUID for shutdown privilege.
  83. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
  84. tkp.PrivilegeCount = 1; // one privilege to set
  85. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  86. // Get shutdown privilege for this process.
  87. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
  88. // Cannot test the return value of AdjustTokenPrivileges.
  89. if (GetLastError() != ERROR_SUCCESS)
  90. return FALSE;
  91. // 关机原因;
  92. DWORD dwReason = SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED;
  93. // Display the shutdown dialog box and start the countdown.
  94. fResult = InitiateSystemShutdownEx(
  95. NULL, // shut down local computer
  96. lpMsgToUser, // message for user
  97. dwTimeout, // time-out period, in seconds
  98. bForceAppsClosed, // ask user to close apps
  99. bRebootAfterShutdown, // reboot after shutdown
  100. dwReason);
  101. if (!fResult)
  102. return FALSE;
  103. // Disable shutdown privilege.
  104. tkp.Privileges[0].Attributes = 0;
  105. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  106. (PTOKEN_PRIVILEGES) NULL, 0);
  107. return TRUE;
  108. }