about summary refs log tree commit diff stats
path: root/Source/Memory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Memory.cpp')
-rw-r--r--Source/Memory.cpp24
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
9Memory::Memory(const std::string& processName) { 9Memory::Memory() {
10}
11
12[[nodiscard]]
13bool 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
44Memory::~Memory() { 50Memory::~Memory() {
45 CloseHandle(_handle); 51 if (_handle != nullptr) {
52 CloseHandle(_handle);
53 }
46} 54}
47 55
48int Memory::GetCurrentFrame() 56int Memory::GetCurrentFrame()