BOOL EnableShutdownPrivilege() { HANDLE hProcess = NULL; HANDLE hToken = NULL; LUID uID = {0}; TOKEN_PRIVILEGES stToken_Privileges = {0}; hProcess = ::GetCurrentProcess(); //獲取當前應用程序進程句柄 if(!::OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken)) //打開當前進程的訪問令牌句柄(OpenProcessToken函數調用失敗返回值爲零) { return FALSE; } if(!::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&uID)) //獲取權限名稱爲"SeShutdownPrivilege"的LUID(LookupPrivilegeValue函數調用失敗返回值爲零) { return FALSE; } stToken_Privileges.PrivilegeCount = 1; //欲調整的權限個數 stToken_Privileges.Privileges[0].Luid = uID; //權限的LUID stToken_Privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; //權限的屬性,SE_PRIVILEGE_ENABLED爲使能該權限 if(!::AdjustTokenPrivileges(hToken,FALSE,&stToken_Privileges,sizeof stToken_Privileges,NULL,NULL)) //調整訪問令牌裏的指定權限(AdjustTokenPrivileges函數調用失敗返回值爲零) { return FALSE; } if(::GetLastError() != ERROR_SUCCESS) //查看權限是否調整成功 { return FALSE; } ::CloseHandle(hToken); return TRUE; }