Last active
September 13, 2018 16:15
-
-
Save subnomo/38800124e54db0a48025e2d6f474bf26 to your computer and use it in GitHub Desktop.
SimpleMemory - A C++ class to simplify reading and writing to the memory of programs (e.g. games)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SimpleMemory.h" | |
SimpleMemory::SimpleMemory(std::wstring windowName) { | |
hwnd = FindWindowW(nullptr, windowName.c_str()); | |
DWORD pid; | |
GetWindowThreadProcessId(hwnd, &pid); | |
hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, false, pid); | |
} | |
SimpleMemory::~SimpleMemory() { | |
CloseHandle(hProcess); | |
} | |
DWORD SimpleMemory::read(DWORD address) const { | |
LPCVOID addr = reinterpret_cast<LPCVOID>(address); | |
DWORD result; | |
ReadProcessMemory(hProcess, addr, &result, sizeof(result), nullptr); | |
return result; | |
} | |
BOOL SimpleMemory::write(DWORD address, DWORD value) const { | |
LPVOID addr = reinterpret_cast<LPVOID>(address); | |
return WriteProcessMemory(hProcess, addr, &value, sizeof(value), nullptr); | |
} | |
DWORD SimpleMemory::addOffsets(DWORD initial, std::vector<DWORD> offsets) const { | |
for (std::vector<int>::size_type i = 0; i != offsets.size(); i++) { | |
DWORD offset = offsets.at(i); | |
if (i != offsets.size() - 1) { | |
initial = read(initial + offset); | |
} else { | |
initial += offset; | |
} | |
} | |
return initial; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#define WIN32_LEAN_AND_MEAN | |
#include <Windows.h> | |
#include <string> | |
#include <vector> | |
class SimpleMemory { | |
HWND hwnd; | |
HANDLE hProcess; | |
public: | |
SimpleMemory(std::wstring windowName); | |
~SimpleMemory(); | |
DWORD read(DWORD address) const; | |
BOOL write(DWORD address, DWORD value) const; | |
/** | |
* Adds a list of offsets to an initial address, each time reading from | |
* memory the new address. Directly adds the final offset to address. | |
*/ | |
DWORD addOffsets(DWORD initial, std::vector<DWORD> offsets) const; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment