-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemInfo.cpp
231 lines (197 loc) · 6.13 KB
/
SystemInfo.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include"SystemInfo.hpp"
#include<algorithm>
#include<iostream>
Xertz::SystemInfo::SystemInfo()
{
CoInitialize(nullptr);
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), reinterpret_cast<LPVOID*>(&_deviceEnumerator));
if (_deviceEnumerator != nullptr)
{
_deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &_defaultDevice);
_deviceEnumerator->Release();
_deviceEnumerator = NULL;
_defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<LPVOID*>(&_endpointVolume));
_defaultDevice->Release();
_defaultDevice = NULL;
}
_ntdll = GetModuleHandleW(L"ntdll.dll");
}
Xertz::SystemInfo::~SystemInfo()
{
if (_endpointVolume != nullptr)
{
_endpointVolume->Release();
CoUninitialize();
}
}
bool Xertz::SystemInfo::RefreshProcessInfoList()
{
GetInstance().s_processInfoList.clear();
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32W entry;
entry.dwSize = sizeof(entry);
if (Process32FirstW(snap, &entry))
{
do
{
GetInstance().s_processInfoList.emplace_back(entry.th32ProcessID, std::wstring(entry.szExeFile));
} while (Process32NextW(snap, &entry));
return true;
}
return false;
}
void Xertz::SystemInfo::obtainHWNDs()
{
_windows.clear();
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&_windows));
}
WINDOW_HANDLE_LIST& Xertz::SystemInfo::GetWindowHandleList()
{
GetInstance().obtainHWNDs();
return GetInstance()._windows;
}
HMODULE Xertz::SystemInfo::GetNtDllHandle()
{
return GetInstance()._ntdll;
}
bool Xertz::SystemInfo::RefreshApplicationProcessInfoList()
{
GetInstance().s_applicationProcessInfoList.clear();
GetInstance().obtainHWNDs();
DWORD previousPid = 0;
std::vector<std::wstring> foundApps;
for (HWND window : GetInstance()._windows)
{
DWORD anotherpid;
DWORD pid = GetWindowThreadProcessId(window, &anotherpid);
if (anotherpid < 1)
continue;
if (anotherpid == previousPid)
continue;
for (ProcessInfo& process : GetInstance().s_processInfoList)
{
bool found = false;
if (process.GetPID() != anotherpid)
continue;
for (std::wstring& processName : foundApps)
if (process.GetProcessNameW().compare(processName) == 0)
found = true;
if (found)
continue;
GetInstance().s_applicationProcessInfoList.push_back(process);
foundApps.push_back(process.GetProcessNameW());
previousPid = anotherpid;
break;
}
}
return GetInstance().s_applicationProcessInfoList.size();
}
PROCESS_INFO_LIST& Xertz::SystemInfo::GetProcessInfoList()
{
return GetInstance().s_processInfoList;
}
PROCESS_INFO_LIST& Xertz::SystemInfo::GetApplicationProcessInfoList()
{
return GetInstance().s_applicationProcessInfoList;
}
PROCESS_INFO Xertz::SystemInfo::GetProcessInfo(const int pid)
{
for (int i = 0; i < GetInstance().s_processInfoList.size(); ++i)
{
if (GetInstance().s_processInfoList[i].GetPID() == pid)
{
return GetInstance().s_processInfoList[i];
}
}
return ProcessInfo(-1, L"Process not found");
}
PROCESS_INFO Xertz::SystemInfo::GetProcessInfo(std::wstring processName, const bool substring, const bool caseSensitive)
{
if (GetInstance().RefreshProcessInfoList())
{
for (int i = 0; i < GetInstance().s_processInfoList.size(); ++i)
{
if (caseSensitive)
{
if ((substring && GetInstance().s_processInfoList[i].GetProcessNameW().find(processName) != -1) || /*case sensitive and contains*/
(!substring && GetInstance().s_processInfoList[i].GetProcessNameW().compare(processName) == 0)) /*case sensitive and same*/
{
return GetInstance().s_processInfoList[i];
}
}
else
{
std::wstring lProcessName = GetInstance().s_processInfoList[i].GetProcessNameW();
lProcessName = GetInstance().WString2Lower(lProcessName);
processName = GetInstance().WString2Lower(processName);
if ((substring && lProcessName.find(processName) != -1) || /*case insensitive and contains*/
(!substring && lProcessName.compare(processName) == 0)) /*case insensitive and same*/
{
return GetInstance().s_processInfoList[i];
}
}
}
}
return ProcessInfo(-1, L"Process not found");
}
bool Xertz::SystemInfo::KillProcess(const int pid)
{
HANDLE h = OpenProcess(PROCESS_TERMINATE, false, pid);
TerminateProcess(h, 1);
return CloseHandle(h);
}
bool Xertz::SystemInfo::KillProcess(const std::wstring& processName, const bool substring, const bool caseSensitive)
{
const int pid = GetInstance().GetProcessInfo(processName, substring, caseSensitive).GetPID();
return GetInstance().KillProcess(pid);
}
std::wstring Xertz::SystemInfo::WString2Lower(const std::wstring& str) const
{
std::wstring result = str;
std::transform(result.begin(), result.end(), result.begin(), tolower);
return result;
}
bool Xertz::SystemInfo::PGetMasterVolume()
{
bool success = _endpointVolume->GetMasterVolumeLevel(&_masterVolumeDecibel) != 0;
_masterVolumeScalar = _masterVolumeDecibel;
success = _endpointVolume->GetMasterVolumeLevelScalar(&_masterVolumeScalar) != 0;
return !success;
}
bool Xertz::SystemInfo::GetMasterVolume(float* out, const bool scalar)
{
const bool success = GetInstance().PGetMasterVolume();
scalar ? *out = GetInstance()._masterVolumeScalar * GetInstance()._percentageFactor : *out = GetInstance()._masterVolumeDecibel;
return success;
}
bool Xertz::SystemInfo::SetMasterVolume(const float in, const bool scalar)
{
bool success;
if (scalar)
{
success = GetInstance()._endpointVolume->SetMasterVolumeLevelScalar(in / GetInstance()._percentageFactor, NULL) != 0;
}
else
{
success = GetInstance()._endpointVolume->SetMasterVolumeLevel(in, NULL) != 0;
}
GetInstance().PGetMasterVolume();
return success;
}
bool Xertz::SystemInfo::SetMasterMute(const bool mute)
{
bool success;
if (mute)
{
GetInstance().PGetMasterVolume();
const float volScal = GetInstance()._masterVolumeScalar, volDec = GetInstance()._masterVolumeDecibel;
success = SetMasterVolume(0.0f, true);
GetInstance()._masterVolumeScalar = volScal;
GetInstance()._masterVolumeDecibel = volDec;
}
else
{
success = SetMasterVolume(GetInstance()._masterVolumeScalar * GetInstance()._percentageFactor, true);
}
return !success;
}