在WIN2000以上执行关机、注销、重启代码需要调整权限:
//调整权限调用API关机函数
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // 设置一个权限
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);
ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0); //关机
ExitWindowsEx(EWX_LOGOFF,0); //注销
ExitWindowsEx(EWX_FORCE | EWX_REBOOT,0); //重启