#include "./operation_windows.h" Operation_Windows::Operation_Windows() { } int Operation_Windows::ScreenOperation(bool commond) { #ifdef _WIN32 if(commond==true) { ::PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2); } else { ::PostMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)-1); } #else Q_UNUSED(commond); #endif return 0; } bool Operation_Windows::envinit() { #ifdef _WIN32 HANDLE hToken; TOKEN_PRIVILEGES tkp; //获取进程标志 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return false; //获取关机特权的LUID 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; #endif return true; } int Operation_Windows::PoweroffOperation(int commond) { #ifdef _WIN32 if(envinit() !=0) return -1; /*****************************************************************/ if(commond == 1) { // 强制关闭计算机 if ( !ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) return -1; } else if(commond == 2) { SetSystemPowerState(false,TRUE); } else if(commond == 3) { ExitWindowsEx(EWX_REBOOT,0); //重启计算机 } else { } #else Q_UNUSED(commond); #endif return 0; }