-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathEnumerateProcesses.cpp
More file actions
90 lines (77 loc) · 2.02 KB
/
EnumerateProcesses.cpp
File metadata and controls
90 lines (77 loc) · 2.02 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
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <filesystem>
#include "NativeCore.hpp"
enum class Platform
{
Unknown,
X86,
X64
};
Platform GetProcessPlatform(HANDLE process)
{
static USHORT processorArchitecture = PROCESSOR_ARCHITECTURE_UNKNOWN;
if (processorArchitecture == PROCESSOR_ARCHITECTURE_UNKNOWN)
{
SYSTEM_INFO info = {};
GetNativeSystemInfo(&info);
processorArchitecture = info.wProcessorArchitecture;
}
switch (processorArchitecture)
{
case PROCESSOR_ARCHITECTURE_INTEL:
return Platform::X86;
case PROCESSOR_ARCHITECTURE_AMD64:
auto isWow64 = FALSE;
if (IsWow64Process(process, &isWow64))
{
return isWow64 ? Platform::X86 : Platform::X64;
}
#ifdef RECLASSNET64
return Platform::X64;
#else
return Platform::X86;
#endif
}
return Platform::Unknown;
}
void RC_CallConv EnumerateProcesses(EnumerateProcessCallback callbackProcess)
{
if (callbackProcess == nullptr)
{
return;
}
const auto handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (handle != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32W pe32 = {};
pe32.dwSize = sizeof(PROCESSENTRY32W);
if (Process32FirstW(handle, &pe32))
{
do
{
const auto process = OpenRemoteProcess(reinterpret_cast<RC_Pointer>(static_cast<size_t>(pe32.th32ProcessID)), ProcessAccess::Read);
if (IsProcessValid(process))
{
const auto platform = GetProcessPlatform(process);
#ifdef RECLASSNET64
if (platform == Platform::X64)
#else
if (platform == Platform::X86)
#endif
{
EnumerateProcessData data = { };
data.Id = pe32.th32ProcessID;
GetModuleFileNameExW(process, nullptr, reinterpret_cast<LPWSTR>(data.Path), PATH_MAXIMUM_LENGTH);
const auto name = std::filesystem::path(data.Path).filename().u16string();
str16cpy(data.Name, name.c_str(), std::min<size_t>(name.length(), PATH_MAXIMUM_LENGTH - 1));
callbackProcess(&data);
}
}
CloseRemoteProcess(process);
} while (Process32NextW(handle, &pe32));
}
CloseHandle(handle);
}
}