-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.cpp
More file actions
135 lines (118 loc) · 4.08 KB
/
Copy pathprocess.cpp
File metadata and controls
135 lines (118 loc) · 4.08 KB
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
#include "process.h"
#include "log.h"
#include <Windows.h>
#include <TlHelp32.h>
#include <Psapi.h>
#include <algorithm>
#include <cwctype>
#include <chrono>
#include <thread>
#include <cstdio>
#include <cwchar>
namespace {
std::wstring ToLower(std::wstring s) {
std::transform(s.begin(), s.end(), s.begin(),
[](wchar_t c) { return (wchar_t)std::towlower(c); });
return s;
}
bool IEqual(const std::wstring& a, const std::wstring& b) {
return ToLower(a) == ToLower(b);
}
bool IContains(const std::wstring& hay, const std::wstring& needle) {
if (needle.empty()) return true;
return ToLower(hay).find(ToLower(needle)) != std::wstring::npos;
}
} // anon
std::vector<ProcInfo> EnumerateProcesses(const std::wstring& filter) {
std::vector<ProcInfo> out;
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snap == INVALID_HANDLE_VALUE) return out;
PROCESSENTRY32W pe{ sizeof(pe) };
if (Process32FirstW(snap, &pe)) {
do {
if (filter.empty() || IContains(pe.szExeFile, filter)) {
out.push_back({ pe.th32ProcessID, pe.szExeFile });
}
} while (Process32NextW(snap, &pe));
}
CloseHandle(snap);
return out;
}
DWORD FindPidByName(const std::wstring& exe) {
auto all = FindAllPidsByName(exe);
return all.empty() ? 0 : all.front();
}
std::vector<DWORD> FindAllPidsByName(const std::wstring& exe) {
std::vector<DWORD> out;
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snap == INVALID_HANDLE_VALUE) return out;
PROCESSENTRY32W pe{ sizeof(pe) };
if (Process32FirstW(snap, &pe)) {
do {
if (IEqual(pe.szExeFile, exe)) out.push_back(pe.th32ProcessID);
} while (Process32NextW(snap, &pe));
}
CloseHandle(snap);
return out;
}
Arch GetProcessArch(HANDLE process) {
USHORT processMachine = 0;
USHORT nativeMachine = 0;
if (!IsWow64Process2(process, &processMachine, &nativeMachine)) {
return Arch::Unknown;
}
// processMachine == IMAGE_FILE_MACHINE_UNKNOWN means "not WoW64" → native arch
USHORT m = (processMachine == IMAGE_FILE_MACHINE_UNKNOWN) ? nativeMachine : processMachine;
switch (m) {
case IMAGE_FILE_MACHINE_I386: return Arch::X86;
case IMAGE_FILE_MACHINE_AMD64: return Arch::X64;
case IMAGE_FILE_MACHINE_ARM64: return Arch::ARM64;
default: return Arch::Unknown;
}
}
bool IsModuleLoaded(HANDLE process, const std::wstring& name, HMODULE* outHandle) {
HMODULE mods[1024];
DWORD needed = 0;
if (!EnumProcessModulesEx(process, mods, sizeof(mods), &needed, LIST_MODULES_ALL)) {
return false;
}
DWORD count = needed / sizeof(HMODULE);
if (count > 1024) count = 1024;
std::wstring target = ToLower(name);
for (DWORD i = 0; i < count; ++i) {
wchar_t buf[MAX_PATH];
if (GetModuleBaseNameW(process, mods[i], buf, MAX_PATH)) {
if (ToLower(buf) == target) {
if (outHandle) *outHandle = mods[i];
return true;
}
}
}
return false;
}
DWORD WaitForProcess(const std::wstring& exe, DWORD timeoutSec) {
using clock = std::chrono::steady_clock;
auto start = clock::now();
for (;;) {
DWORD pid = FindPidByName(exe);
if (pid) return pid;
if (timeoutSec) {
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(clock::now() - start).count();
if ((DWORD)elapsed >= timeoutSec) return 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
}
bool IsProcessAlive(HANDLE process) {
DWORD code = 0;
if (!GetExitCodeProcess(process, &code)) return false;
return code == STILL_ACTIVE;
}
size_t PrintProcessList(const std::vector<ProcInfo>& procs) {
std::fwprintf(stdout, L"%-8s %s\n", L"PID", L"Name");
std::fwprintf(stdout, L"%-8s %s\n", L"------", L"-----------------------------");
for (const auto& p : procs) {
std::fwprintf(stdout, L"%-8lu %ls\n", p.pid, p.name.c_str());
}
return procs.size();
}