forked from Rob--/memoryjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.cc
66 lines (50 loc) · 1.68 KB
/
module.cc
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
#include <node.h>
#include <windows.h>
#include <TlHelp32.h>
#include <vector>
#include "module.h"
#include "process.h"
#include "memoryjs.h"
module::module() {}
module::~module() {}
using v8::Exception;
using v8::Isolate;
using v8::String;
MODULEENTRY32 module::findModule(const char* moduleName, DWORD processId, char** errorMessage) {
MODULEENTRY32 module;
// A list of modules (MODULEENTRY32)
std::vector<MODULEENTRY32> modules = getModules(processId, errorMessage);
// Loop over every module
for (std::vector<MODULEENTRY32>::size_type i = 0; i != modules.size(); i++) {
// Check to see if this is the module we want.
if (!strcmp(modules[i].szModule, moduleName)) {
// module is returned and moduleEntry is used internally for reading/writing to memory
module = moduleEntry = modules[i];
break;
}
}
return module;
}
std::vector<MODULEENTRY32> module::getModules(DWORD processId, char** errorMessage) {
// Take a snapshot of all modules inside a given process.
HANDLE hModuleSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processId);
MODULEENTRY32 mEntry;
if (hModuleSnapshot == INVALID_HANDLE_VALUE) {
*errorMessage = "method failed to take snapshot of the process";
}
// Before use, set the structure size.
mEntry.dwSize = sizeof(mEntry);
// Exit if unable to find the first module.
if (!Module32First(hModuleSnapshot, &mEntry)) {
CloseHandle(hModuleSnapshot);
*errorMessage = "method failed to retrieve the first module";
}
std::vector<MODULEENTRY32> modules;
// Loop through modules.
do {
// Add the module to the vector
modules.push_back(mEntry);
} while (Module32Next(hModuleSnapshot, &mEntry));
CloseHandle(hModuleSnapshot);
return modules;
}