-
Notifications
You must be signed in to change notification settings - Fork 3
/
proc.cpp
101 lines (81 loc) · 1.96 KB
/
proc.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
#include "framework.h"
#include "proc.h"
DWORD Proc::GetProcId(const wchar_t* procName)
{
procId = 0;
// Get Process snapshot
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE)
{
// Struct that contains process information
PROCESSENTRY32 procEntry;
procEntry.dwSize = sizeof(PROCESSENTRY32);
if (!Process32First(hSnap, &procEntry))
{
return procId;
}
while (Process32Next(hSnap, &procEntry))
{
if (!_wcsicmp(procEntry.szExeFile, procName))
{
procId = procEntry.th32ProcessID;
break;
}
}
}
CloseHandle(hSnap);
return procId;
}
DWORD_PTR Proc::GetModuleBaseAddress64(DWORD procId)
{
DWORD_PTR baseAddress = 0;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procId);
HMODULE* moduleArray;
LPBYTE moduleArrayBytes;
DWORD bytesRequired;
if (hProc)
{
if (EnumProcessModules(hProc, NULL, 0, &bytesRequired))
{
if (bytesRequired)
{
moduleArrayBytes = (LPBYTE)LocalAlloc(LPTR, bytesRequired);
if (moduleArrayBytes)
{
unsigned int moduleCount;
moduleCount = bytesRequired / sizeof(HMODULE);
moduleArray = (HMODULE*)moduleArrayBytes;
if (EnumProcessModules(hProc, moduleArray, bytesRequired, &bytesRequired))
{
baseAddress = (DWORD_PTR)moduleArray[0];
}
LocalFree(moduleArrayBytes);
}
}
}
CloseHandle(hProc);
}
return baseAddress;
}
DWORD_PTR Proc::GetModuleBase(const wchar_t* module, DWORD procId)
{
HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, procId);
if (hSnap == INVALID_HANDLE_VALUE)
{
return 0;
}
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(MODULEENTRY32);
if (!Module32First(hSnap, &modEntry))
{
return 0;
}
if (!_wcsicmp(modEntry.szModule, module))
return (DWORD_PTR)modEntry.modBaseAddr;
while (Module32Next(hSnap, &modEntry))
{
if (!_wcsicmp(modEntry.szModule, module))
return (DWORD_PTR)modEntry.modBaseAddr;
}
}
Proc* proc = new Proc();