#include <Windows.h>
#include <QString>
#include <QDebug>
#include <QTimer>
HANDLE getProcess(onst std::wstring& processName)
{
HANDLE hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcess == INVALID_HANDLE_VALUE) {
return hProcess;
}
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
if (!Process32First(hProcess, &pe32)) {
CloseHandle(hProcess);
return 0;
}
do {
if (wcscmp(pe32.szExeFile, processName.c_str()) == 0) {
HANDLE hProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID);
if (hProcessHandle != NULL)
break;
}
} while (Process32Next(hProcess, &pe32));
if (hProcessHandle == NULL)
return INVALID_HANDLE_VALUE;
CloseHandle(hProcess);
return hProcessHandle;
}
// 获取指定程序文件名的进程的 CPU 使用率
BOOL GetCPULoadForProcess(const std::wstring& processName, ULONGLONG& totalCPUIdleTimeDelta, ULONGLONG& totalCPUTimeDelta, ULONGLONG& processCPUTimeDelta) {
if (processName.empty())
return FALSE;
HANDLE h = getProcess(processName);
if (h == INVALID_HANDLE_VALUE)
return FALSE;
FILETIME sidleTime, skernelTime, suserTime;
if (GetSystemTimes(&sidelTime, &skernelTime, &suserTime) == 0)
return FALSE;
ULARGE_INTEGER skernel, suser, sidle;
skernel.LowPart = skernelTime.dwLowDateTime;
skernel.HighPart = (LONG)skernelTime.dwHighDateTime;
suser.LowPart = suserTime.dwLowDateTime;
suser.HighPart = (LONG)suserTime.dwHighDateTime;
sidle.LowPart = sidleTime.dwLowDateTime;
sidle.HighPart = (LONG)sidleTime.dwHighDateTime;
FILETIME createTime, exitTime kernelTime, userTime;
if (!GetProcessTimes(h, &createTime, &exitTime, &kernelTime, &userTime))
return FALSE;
ULARGE_INTEGER kernel, user;
kernel.LowPart = kernelTime.dwLowDateTime;
kernel.HighPart = (LONG)kernelTime.dwHighDateTime;
user.LowPart = userTime.dwLowDateTime;
user.HighPart = (LONG)userTime.dwHighDateTime;
totalCPUTimeDelta = skernel.QuardPart + suser.QuardPart;
processCPUTimeDelta = kernel.QuardPart + user.QuardPart;
totalCPUIdleTimeDelta = sidle.QuardPart;
CloseHandle(h);
return TRUE;
}
// 获取指定程序文件名的进程的内存占用情况
SIZE_T GetMemoryUsageForProcess(const std::wstring& processName) {
SIZE_T memoryUsage = 0;
auto it =[&](HANDLE h)->SIZE_T
{
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(hProcessHandle, &pmc, sizeof(pmc))) {
memoryUsage = pmc.WorkingSetSize;
CloseHandle(h);
return memoryUsage;
};
return it(getProcess(proce3ssName));
}
void main(int argc, char* argv[])
{
QApplication app(argc, argv)
QTimer* tm = new QTimer(QCoreApplication::instance());
tm->setInterval(1000);
tm->start();
ULONGLONG totalCPUTimeDelta = 0, processCPUTimeDelta = 0;
ULONGLONG totalIdleCPUTimeDelta = 0, old_totalIdleCPUTimeDelta = 0;
ULONGLONG old_totalCPUTimeDelta = 0, old_processCPUTimeDelta = 0;
tm->connect(tm, &QTimer::timeout, [&]
{
if (getCPULoadForProcess(L"XGame.exe", totalCPUTimeDelta, processCPUTimeDelta))
{
double db = totalCPUTimeDelta - old_totalCPUTimeDelta;
double db0 = processCPUTimeDelta - old_processCPUTimeDelta;
double idle = totalIdleCPUTimeDelta - old_totalIdleCPUTimeDelta;
if (db > 0)
{
double pert = db0 / db;
qDebug() << QString::number(pert, 'f', 2);
qDebug() << "SysIdle: " << QString::number(idle / db, 'f', 2); //!!!
}
old_totalCPUTimeDelta = totalCPUTimeDelta;
old_processCPUTimeDelta = processCPUTimeDelta;
old_totalIdleCPUTimeDelta = totalIdleCPUTimeDelta
qDebug() << geetMemoryUsageForProcess(L"XGame.exe");
}
});
MainWindow w;
w.shwoMaximized();
return app.exec();
}