VC 关机
win7中,若注销,可直接调用ExitWindowsEx(EWX_LOGOFF, 0)
若要关机,则需要获取系统权限,如下:
bool Shutdown() { HANDLE hToken; TOKEN_PRIVILEGES tkp; //打开进程访问令牌 if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return false; //修改权限 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); if(GetLastError() != ERROR_SUCCESS) return false; if(!ExitWindowsEx(EWX_SHUTDOWN, 0)) return false; return true; }