-
Notifications
You must be signed in to change notification settings - Fork 1
/
kprocread.cpp
148 lines (122 loc) · 5.02 KB
/
kprocread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "kprocread.h"
SYSTEM_INFO sysInfo;
std::wstring KPROC::widen(const std::string& str)
{
std::wostringstream wstm;
const std::ctype<wchar_t>& ctfacet = std::use_facet<std::ctype<wchar_t>>(wstm.getloc());
for (size_t i = 0; i < str.size(); ++i)
wstm << ctfacet.widen(str[i]);
return wstm.str();
}
bool KPROC::checkProcess(std::string cstr)
{
const std::wstring widestring = KPROC::widen(cstr);
const wchar_t* wcstr = widestring.c_str();
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (_tcsicmp(entry.szExeFile, wcstr) == 0)
{
return 1;
}
}
}
return -0;
}
float KPROC::getCpuUsage(std::string cstr)
{
const std::wstring widestring = KPROC::widen(cstr);
const wchar_t* wcstr = widestring.c_str();
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (_tcsicmp(entry.szExeFile, wcstr) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
static ULARGE_INTEGER lastCPU, lastSysCPU, lastUserCPU;
static int numProcessors;
static HANDLE self;
double percent;
numProcessors = sysInfo.dwNumberOfProcessors;
//Getting system times information
FILETIME SysidleTime;
FILETIME SyskernelTime;
FILETIME SysuserTime;
ULARGE_INTEGER SyskernelTimeInt, SysuserTimeInt;
GetSystemTimes(&SysidleTime, &SyskernelTime, &SysuserTime);
memcpy(&SyskernelTimeInt, &SyskernelTime, sizeof(FILETIME));
memcpy(&SysuserTimeInt, &SysuserTime, sizeof(FILETIME));
__int64 denomenator = SysuserTimeInt.QuadPart + SyskernelTimeInt.QuadPart;
//Getting process times information
FILETIME ProccreationTime, ProcexitTime, ProcKernelTime, ProcUserTime;
ULARGE_INTEGER ProccreationTimeInt, ProcexitTimeInt, ProcKernelTimeInt, ProcUserTimeInt;
GetProcessTimes(hProcess, &ProccreationTime, &ProcexitTime, &ProcKernelTime, &ProcUserTime);
memcpy(&ProcKernelTimeInt, &ProcKernelTime, sizeof(FILETIME));
memcpy(&ProcUserTimeInt, &ProcUserTime, sizeof(FILETIME));
__int64 numerator = ProcUserTimeInt.QuadPart + ProcKernelTimeInt.QuadPart;
//QuadPart represents a 64-bit signed integer (ULARGE_INTEGER)
percent = 100 * long float(((long float(numerator)) / (long float(denomenator))));
return percent;
CloseHandle(hProcess);
}
}
}
CloseHandle(snapshot);
return -1;
}
float KPROC::getPhyMem(std::string cstr)
{
const std::wstring widestring = KPROC::widen(cstr);
const wchar_t* wcstr = widestring.c_str();
PROCESS_MEMORY_COUNTERS_EX pmc;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (_tcsicmp(entry.szExeFile, wcstr) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
GetProcessMemoryInfo(hProcess, (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
return float(pmc.WorkingSetSize / 1024)/1000;
CloseHandle(hProcess);
}
}
}
CloseHandle(snapshot);
return -1;
}
float KPROC::getVirMem(std::string cstr)
{
const std::wstring widestring = KPROC::widen(cstr);
const wchar_t* wcstr = widestring.c_str();
PROCESS_MEMORY_COUNTERS_EX pmc;
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) == TRUE)
{
while (Process32Next(snapshot, &entry) == TRUE)
{
if (_wcsicmp(entry.szExeFile, wcstr) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
GetProcessMemoryInfo(hProcess, (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
return float(pmc.PrivateUsage / 1024)/1000;
CloseHandle(hProcess);
}
}
}
CloseHandle(snapshot);
return -1;
}