diff options
author | jbzdarkid <jbzdarkid@gmail.com> | 2019-11-04 08:43:31 -0800 |
---|---|---|
committer | jbzdarkid <jbzdarkid@gmail.com> | 2019-11-04 08:43:31 -0800 |
commit | 0cb49374a6d86100cd6f3bf838e79bdbac242a8e (patch) | |
tree | cab3a985cd5a7c8a448d3c0a67ff7588faed9c24 /Source/Memory.cpp | |
parent | edef920a2d7d753f93cd716052d8f2104bfdfa62 (diff) | |
download | witness-tutorializer-0cb49374a6d86100cd6f3bf838e79bdbac242a8e.tar.gz witness-tutorializer-0cb49374a6d86100cd6f3bf838e79bdbac242a8e.tar.bz2 witness-tutorializer-0cb49374a6d86100cd6f3bf838e79bdbac242a8e.zip |
Starting to clean up...
Diffstat (limited to 'Source/Memory.cpp')
-rw-r--r-- | Source/Memory.cpp | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 7bc3b2f..1f1ae0a 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp | |||
@@ -6,12 +6,16 @@ | |||
6 | #undef PROCESSENTRY32 | 6 | #undef PROCESSENTRY32 |
7 | #undef Process32Next | 7 | #undef Process32Next |
8 | 8 | ||
9 | Memory::Memory(const std::string& processName) { | 9 | Memory::Memory() { |
10 | } | ||
11 | |||
12 | [[nodiscard]] | ||
13 | bool Memory::Initialize(const std::wstring& processName) { | ||
10 | // First, get the handle of the process | 14 | // First, get the handle of the process |
11 | PROCESSENTRY32 entry; | 15 | PROCESSENTRY32W entry; |
12 | entry.dwSize = sizeof(entry); | 16 | entry.dwSize = sizeof(entry); |
13 | HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | 17 | HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
14 | while (Process32Next(snapshot, &entry)) { | 18 | while (Process32NextW(snapshot, &entry)) { |
15 | if (processName == entry.szExeFile) { | 19 | if (processName == entry.szExeFile) { |
16 | _handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); | 20 | _handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); |
17 | break; | 21 | break; |
@@ -19,7 +23,7 @@ Memory::Memory(const std::string& processName) { | |||
19 | } | 23 | } |
20 | if (!_handle) { | 24 | if (!_handle) { |
21 | std::cerr << "Couldn't find " << processName.c_str() << ", is it open?" << std::endl; | 25 | std::cerr << "Couldn't find " << processName.c_str() << ", is it open?" << std::endl; |
22 | throw std::exception("Unable to find process!"); | 26 | return false; |
23 | } | 27 | } |
24 | 28 | ||
25 | // Next, get the process base address | 29 | // Next, get the process base address |
@@ -27,9 +31,9 @@ Memory::Memory(const std::string& processName) { | |||
27 | std::vector<HMODULE> moduleList(1024); | 31 | std::vector<HMODULE> moduleList(1024); |
28 | EnumProcessModulesEx(_handle, &moduleList[0], static_cast<DWORD>(moduleList.size()), &numModules, 3); | 32 | EnumProcessModulesEx(_handle, &moduleList[0], static_cast<DWORD>(moduleList.size()), &numModules, 3); |
29 | 33 | ||
30 | std::string name(64, '\0'); | 34 | std::wstring name(64, '\0'); |
31 | for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { | 35 | for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { |
32 | int length = GetModuleBaseNameA(_handle, moduleList[i], &name[0], static_cast<DWORD>(name.size())); | 36 | int length = GetModuleBaseNameW(_handle, moduleList[i], &name[0], static_cast<DWORD>(name.size())); |
33 | name.resize(length); | 37 | name.resize(length); |
34 | if (processName == name) { | 38 | if (processName == name) { |
35 | _baseAddress = (uintptr_t)moduleList[i]; | 39 | _baseAddress = (uintptr_t)moduleList[i]; |
@@ -37,12 +41,16 @@ Memory::Memory(const std::string& processName) { | |||
37 | } | 41 | } |
38 | } | 42 | } |
39 | if (_baseAddress == 0) { | 43 | if (_baseAddress == 0) { |
40 | throw std::exception("Couldn't find the base process address!"); | 44 | std::cerr << "Couldn't locate base address" << std::endl; |
45 | return false; | ||
41 | } | 46 | } |
47 | return true; | ||
42 | } | 48 | } |
43 | 49 | ||
44 | Memory::~Memory() { | 50 | Memory::~Memory() { |
45 | CloseHandle(_handle); | 51 | if (_handle != nullptr) { |
52 | CloseHandle(_handle); | ||
53 | } | ||
46 | } | 54 | } |
47 | 55 | ||
48 | int Memory::GetCurrentFrame() | 56 | int Memory::GetCurrentFrame() |