From 0cb49374a6d86100cd6f3bf838e79bdbac242a8e Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Mon, 4 Nov 2019 08:43:31 -0800 Subject: Starting to clean up... --- Source/Memory.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'Source/Memory.cpp') 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 @@ #undef PROCESSENTRY32 #undef Process32Next -Memory::Memory(const std::string& processName) { +Memory::Memory() { +} + +[[nodiscard]] +bool Memory::Initialize(const std::wstring& processName) { // First, get the handle of the process - PROCESSENTRY32 entry; + PROCESSENTRY32W entry; entry.dwSize = sizeof(entry); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - while (Process32Next(snapshot, &entry)) { + while (Process32NextW(snapshot, &entry)) { if (processName == entry.szExeFile) { _handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); break; @@ -19,7 +23,7 @@ Memory::Memory(const std::string& processName) { } if (!_handle) { std::cerr << "Couldn't find " << processName.c_str() << ", is it open?" << std::endl; - throw std::exception("Unable to find process!"); + return false; } // Next, get the process base address @@ -27,9 +31,9 @@ Memory::Memory(const std::string& processName) { std::vector moduleList(1024); EnumProcessModulesEx(_handle, &moduleList[0], static_cast(moduleList.size()), &numModules, 3); - std::string name(64, '\0'); + std::wstring name(64, '\0'); for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { - int length = GetModuleBaseNameA(_handle, moduleList[i], &name[0], static_cast(name.size())); + int length = GetModuleBaseNameW(_handle, moduleList[i], &name[0], static_cast(name.size())); name.resize(length); if (processName == name) { _baseAddress = (uintptr_t)moduleList[i]; @@ -37,12 +41,16 @@ Memory::Memory(const std::string& processName) { } } if (_baseAddress == 0) { - throw std::exception("Couldn't find the base process address!"); + std::cerr << "Couldn't locate base address" << std::endl; + return false; } + return true; } Memory::~Memory() { - CloseHandle(_handle); + if (_handle != nullptr) { + CloseHandle(_handle); + } } int Memory::GetCurrentFrame() -- cgit 1.4.1