diff --git a/OperationManager/operation_windows.cpp b/OperationManager/operation_windows.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33710473eaa90bb5d8af31af3acf9c5ecae3c88f --- /dev/null +++ b/OperationManager/operation_windows.cpp @@ -0,0 +1,71 @@ +#include "./operation_windows.h" + +Operation_Windows::Operation_Windows() +{ + +} +int Operation_Windows::ScreenOperation(bool commond) +{ +#if (defined(Q_CC_MSVC) || defined(Q_CC_MINGW)) + 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() +{ +#if (defined(Q_CC_MSVC) || defined(Q_CC_MINGW)) + 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) +{ +#if (defined(Q_CC_MSVC) || defined(Q_CC_MINGW)) + 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; +}