实现“注销/重启/关机”功能代码: HANDLE hToken; TOKEN_PRIVILEGES tkp;if (FForce) // Forces processes to terminate FFlag |= EWX_FORCE;// Get version info to determine operation OSVERSIONINFO osvi; osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 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 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return false; LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); }ExitWindowsEx(FFlag, 0);
ExitWindowsExWindows NT/2000: To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, see Privileges. Windows 95/98: ExitWindowEx does not work from a console application, as it does on Windows NT/Windows 2000.
HANDLE hToken;
TOKEN_PRIVILEGES tkp;if (FForce) // Forces processes to terminate
FFlag |= EWX_FORCE;// Get version info to determine operation
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
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
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false; LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
}ExitWindowsEx(FFlag, 0);