The ExitWindows function logs off the current user. It sends the WM_QUERYENDSESSION message to determine if they can be terminated.
To shut down the system, or shut down and restart the system, use the ExitWindowsEx function.
if (FForce) // Forces processes to terminate
FFlag |= EWX_FORCE;
// Get version info to determine operation
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
//The GetVersionEx function obtains extended information about the version of the operating system that is currently running.
if (GetVersionEx(&osvi) == 0)
return false;
// Determine the platform
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
// Windows NT 3.51, Windows NT 4.0, Windows 2000,
// Windows XP, or Windows .NET Server
//The OpenProcessToken function opens the access token associated with a process.
//If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError.
//打开跟当前进程相关联的access token,输出值是第三个参数hToken
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;
//参数1:lpSystemName [in] Pointer to a null-terminated string specifying the name of the system on which the privilege name is looked up. If a null string is specified, the function attempts to find the privilege name on the local system.
//参数2:#define SE_SHUTDOWN_NAME TEXT("SeShutdownPrivilege")
// #define SE_SECURITY_NAME TEXT("SeSecurityPrivilege")
//参数3:lpLuid [out] Pointer to a variable that receives the locally unique identifier by which the privilege is known on the system specified by the lpSystemName parameter.
//The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name.
//获得the shutdown privilege's locally unique identifier on the local system,
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
//PrivilegeCount Specifies the number of entries in the Privileges array.
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;//激活privilege
//The AdjustTokenPrivileges function enables or disables privileges in the specified access token. Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
}
ExitWindowsEx(FFlag, 0);
return TRUE;
}