-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathprocess.cc
More file actions
111 lines (89 loc) · 3.31 KB
/
process.cc
File metadata and controls
111 lines (89 loc) · 3.31 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
#include <node.h>
#include <windows.h>
#include <TlHelp32.h>
#include <vector>
#include "process.h"
#include "memoryjs.h"
process::process() {}
process::~process() {}
using v8::Exception;
using v8::Isolate;
using v8::String;
process::Pair process::openProcess(const char* processName, char** errorMessage){
PROCESSENTRY32A process;
HANDLE handle = NULL;
// A list of processes (PROCESSENTRY32A)
std::vector<PROCESSENTRY32A> processes = getProcesses(errorMessage);
for (std::vector<PROCESSENTRY32A>::size_type i = 0; i != processes.size(); i++) {
// Check to see if this is the process we want.
if (!strcmp(processes[i].szExeFile, processName)) {
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processes[i].th32ProcessID);
process = processes[i];
break;
}
}
if (handle == NULL) {
*errorMessage = "unable to find process";
}
return {
handle,
process,
};
}
process::Pair process::openProcess(DWORD processId, char** errorMessage) {
PROCESSENTRY32A process;
HANDLE handle = NULL;
// A list of processes (PROCESSENTRY32A)
std::vector<PROCESSENTRY32A> processes = getProcesses(errorMessage);
for (std::vector<PROCESSENTRY32A>::size_type i = 0; i != processes.size(); i++) {
// Check to see if this is the process we want.
if (processId == processes[i].th32ProcessID) {
handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processes[i].th32ProcessID);
process = processes[i];
break;
}
}
if (handle == NULL) {
*errorMessage = "unable to find process";
}
return {
handle,
process,
};
}
std::vector<PROCESSENTRY32A> process::getProcesses(char** errorMessage) {
// Take a snapshot of all processes.
HANDLE hProcessSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32W pEntry;
if (hProcessSnapshot == INVALID_HANDLE_VALUE) {
*errorMessage = "method failed to take snapshot of the process";
}
// Before use, set the structure size.
pEntry.dwSize = sizeof(pEntry);
// Exit if unable to find the first process.
if (!Process32FirstW(hProcessSnapshot, &pEntry)) {
CloseHandle(hProcessSnapshot);
*errorMessage = "method failed to retrieve the first process";
}
std::vector<PROCESSENTRY32A> processes;
// Loop through processes.
do {
// Add the process to the vector
PROCESSENTRY32A pEntryReal;
pEntryReal.dwSize = sizeof(pEntryReal);
pEntryReal.cntUsage = pEntry.cntUsage;
pEntryReal.th32ProcessID = pEntry.th32ProcessID;
pEntryReal.th32DefaultHeapID = pEntry.th32DefaultHeapID;
pEntryReal.th32ModuleID = pEntry.th32ModuleID;
pEntryReal.cntThreads = pEntry.cntThreads;
pEntryReal.th32ParentProcessID = pEntry.th32ParentProcessID;
pEntryReal.pcPriClassBase = pEntry.pcPriClassBase;
pEntryReal.dwFlags = pEntry.dwFlags;
// We use UTF-8 strings everywhere else in the program, but because the Windows API only supports Unicode through UTF-16,
// we have to convert the UTF-16 strings that Windows gives us to UTF-8 for use in the rest of the program
WideCharToMultiByte(CP_UTF8, 0, pEntry.szExeFile, -1, pEntryReal.szExeFile, sizeof(pEntryReal.szExeFile), NULL, NULL);
processes.push_back(pEntryReal);
} while (Process32NextW(hProcessSnapshot, &pEntry));
CloseHandle(hProcessSnapshot);
return processes;
}