// WinShutdown.cpp : 定义应用程序的类行为。 // #include "stdafx.h" #include "WinShutdown.h" #ifdef _DEBUG #define new DEBUG_NEW #endif CWinShutdown::CWinShutdown(void) { } CWinShutdown::~CWinShutdown(void) { } /************************************************************************/ /* 函数:[11/28/2017 Home]; /* 描述:; /* 参数:; /* [IN] IsRestart:TRUE表示关机并重启, 否则只关机; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ BOOL CWinShutdown::SystemShutdown(IN BOOL IsReboot /* = FALSE */) { HANDLE hToken; TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return( FALSE ); // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (GetLastError() != ERROR_SUCCESS) return FALSE; // 关机类型; UINT uFlags = EWX_SHUTDOWN | EWX_FORCE; if ( IsReboot ) uFlags = EWX_RESTARTAPPS | EWX_FORCE; // 关机原因; DWORD dwReason = SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED; // Shut down the system and force all applications to close. if (!ExitWindowsEx(uFlags, dwReason)) return FALSE; //shutdown was successful return TRUE; } /************************************************************************/ /* 函数:[11/28/2017 Home]; /* 描述:; /* 参数:; /* [IN] lpMsgToUser:; /* [IN] dwTimeout:; /* [IN] bForceAppsClosed:true强制关闭应用程序, false提示用户保存变更; /* [IN] bRebootAfterShutdown:true 关机后重启; /* 返回:void; /* 注意:; /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ BOOL CWinShutdown::SystemShutdownBox(IN LPTSTR lpMsgToUser, IN DWORD dwTimeout /* = 30 */, IN BOOL bForceAppsClosed /* = FALSE */, IN BOOL bRebootAfterShutdown /* = FALSE */) { HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure BOOL fResult; // system shutdown flag // Get the current process token handle so we can get shutdown privilege. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE; // Get the LUID for shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) return FALSE; // 关机原因; DWORD dwReason = SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED; // Display the shutdown dialog box and start the countdown. fResult = InitiateSystemShutdownEx( NULL, // shut down local computer lpMsgToUser, // message for user dwTimeout, // time-out period, in seconds bForceAppsClosed, // ask user to close apps bRebootAfterShutdown, // reboot after shutdown dwReason); if (!fResult) return FALSE; // Disable shutdown privilege. tkp.Privileges[0].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); return TRUE; }