ExitWindowsEx
The ExitWindowsEx function either logs off the current user, shuts down the system, or shuts down and restarts the system. It sends the WM_QUERYENDSESSION message to all applications to determine if they can be terminated.
BOOL ExitWindowsEx(
UINT uFlags,
DWORD dwReason
);
uFlags
[in] Shutdown type. This parameter must include one of the following values. 關機類型
EWX_LOGOFF
EWX_POWEROFF
EWX_REBOOT
EWX_SHUTDOWN
可選的參數
EWX_FORCE 強制進程終止 不發送WM_QUERYENDSESSION and WM_ENDSESSION訊息
EWX_FORCEIFHUNG 發送WM_QUERYENDSESSION and WM_ENDSESSION訊息,如果沒有響應強制終止進程。
dwReason
[in] Reason for initiating the shutdown.
If this parameter is zero, the SHTDN_REASON_FLAG_PLANNED reason code will not be set, and therefore the default action is an undefined shutdown that is logged as "No title for this reason could be found". By default, it is also an unplanned shutdown. Depending on how the system is configured, an unplanned shutdown triggers the creation of a file that contains the system state information, which can delay shutdown. Therefore, do not use zero for this parameter.
樣本:
The following example enables the SE_SHUTDOWN_NAME privilege and then shuts down the system.
BOOL MySystemShutdown()
...{
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;
// Shut down the system and force all applications to close.
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
return FALSE;
return TRUE;
}
注意,如果不實現設定許可權 會導致調用失敗。