You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.9 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*!
*FileName: SystemTool.cpp
*Author: Pan Yingdong
*Email: bob.pan@hzleaper.com
*Created:2020/6/15 15:28
*Note:系统相关工具 指令
*/
#include "SystemTool.h"
#include <process.h>
#include <QString>
#pragma execution_character_set("utf-8")
bool SystemTool::SystemShutDown(bool bReset)
{
if (bReset == false)
system("shutdown -s -t 00");//关机
else
system("shutdown -r -t 00");//重启
return true;
}
std::string SystemTool::TCHAR2STRING(TCHAR *STR)
{
int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, NULL);
char* chRtn = new char[iLen * sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);
std::string str(chRtn);
return str;
}
bool SystemTool::isProcessRunning(const char* processName)
{
bool bFound = false;
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes && !bFound)
{
char* exename = UnicodeToAnsi(pEntry.szExeFile);
QString strExeName(exename);
if (strExeName.compare(processName, Qt::CaseInsensitive) == 0)
{
bFound = true;
}
delete exename;
hRes = Process32Next(hSnapShot, &pEntry);
}
return bFound;
}
void SystemTool::GetProcessPath(DWORD dwProcessID, TCHAR* buffer)
{
TCHAR Filename[MAX_PATH];
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessID);
if (hProcess == NULL)return;
HMODULE hModule;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hModule, sizeof(hModule), &cbNeeded))
{
if (GetModuleFileNameEx(hProcess, hModule, Filename, MAX_PATH)) {
RtlMoveMemory((void*)buffer, Filename, sizeof(TCHAR)*MAX_PATH);
}
}
else {
DWORD size = MAX_PATH;
if (QueryFullProcessImageName(hProcess, 0, Filename, &size)) {
RtlMoveMemory((void*)buffer, Filename, sizeof(TCHAR)*MAX_PATH);
}
}
CloseHandle(hProcess);
}
std::vector<DWORD> SystemTool::getPIDByName(const char* processName)
{
std::vector<DWORD> vecRet;
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
char* exename = SystemTool::UnicodeToAnsi(pEntry.szExeFile);
QString strExeName(exename);
if (strExeName.compare(processName, Qt::CaseInsensitive) == 0)
{
TCHAR appPath[MAX_PATH];
SystemTool::GetProcessPath((DWORD)pEntry.th32ProcessID, appPath);
std::string texts = SystemTool::TCHAR2STRING(appPath);
// QFileInfo appFile(texts.c_str());
// QString strAppPath = appFile.absoluteFilePath();
// if (strAppPath.compare(curAppPath, Qt::CaseInsensitive) == 0)
{
vecRet.push_back((DWORD)pEntry.th32ProcessID);
}
}
delete exename;
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
return vecRet;
}
void SystemTool::killZombieProcess(const char *curAppName)
{
std::vector<DWORD> PIDs = getPIDByName(curAppName);
for (int i = 0; i < PIDs.size(); ++i)
{
DWORD pid = PIDs.at(i);
// QString strCmd = QString("taskkill /PID %1 -t -f").arg(pid);
// system(strCmd.toStdString().c_str());
//if (pid != curAppPID)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
}
}
QString SystemTool::SecondTimeString(quint64 value)
{
QString strTime;
int seconds = value % 60;
int minutes = value / 60;
strTime = QString("%1分%2秒").arg(minutes).arg(seconds);
if (minutes >= 60) {
minutes = (value / 60) % 60;
int hours = (value / 60) / 60;
strTime = QString("%1时%2分%3秒").arg(hours).arg(minutes).arg(seconds);
if (hours >= 24) {
hours = ((value / 60) / 60) % 24;
int day = ((value / 60) / 60) / 24;
strTime = QString("%1天%2时%3分%4秒").arg(day).arg(hours).arg(minutes).arg(seconds);
}
}
return strTime;
}
void SystemTool::ReflashBarIcon()
{
// HWND hShellTrayWnd = FindWindow("Shell_TrayWnd", NULL);
// //任务栏右边托盘图标+时间区
// HWND hTrayNotifyWnd = FindWindowEx(hShellTrayWnd, 0, "TrayNotifyWnd", NULL);
// //不同系统可能有可能没有这层
// HWND hSysPager = FindWindowEx(hTrayNotifyWnd, 0, "SysPager", NULL);
// //托盘图标窗口
// HWND hToolbarWindow32;
// if (hSysPager)
// {
// hToolbarWindow32 = FindWindowEx(hSysPager, 0, "ToolbarWindow32", NULL);
// }
// else
// {
// hToolbarWindow32 = FindWindowEx(hTrayNotifyWnd, 0, "ToolbarWindow32", NULL);
// }
// if (hToolbarWindow32)
// {
// RECT r;
// ::GetWindowRect(hToolbarWindow32, &r);
// int width = r.right - r.left;
// int height = r.bottom - r.top;
// //从任务栏中间从左到右 MOUSEMOVE一遍所有图标状态会被更新
// for (int x = 1; x < width; x++)
// {
// ::SendMessage(hToolbarWindow32, WM_MOUSEMOVE, 0, MAKELPARAM(x, height / 2));
// }
// }
}