diff options
Diffstat (limited to 'Source/Memory.h')
| -rw-r--r-- | Source/Memory.h | 51 |
1 files changed, 51 insertions, 0 deletions
| diff --git a/Source/Memory.h b/Source/Memory.h new file mode 100644 index 0000000..8e8bbc3 --- /dev/null +++ b/Source/Memory.h | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #pragma once | ||
| 2 | #include <vector> | ||
| 3 | #include <map> | ||
| 4 | #include <windows.h> | ||
| 5 | |||
| 6 | // https://github.com/erayarslan/WriteProcessMemory-Example | ||
| 7 | // http://stackoverflow.com/q/32798185 | ||
| 8 | // http://stackoverflow.com/q/36018838 | ||
| 9 | // http://stackoverflow.com/q/1387064 | ||
| 10 | class Memory | ||
| 11 | { | ||
| 12 | public: | ||
| 13 | Memory(const std::string& processName); | ||
| 14 | ~Memory(); | ||
| 15 | |||
| 16 | Memory(const Memory& memory) = delete; | ||
| 17 | Memory& operator=(const Memory& other) = delete; | ||
| 18 | |||
| 19 | template<class T> | ||
| 20 | std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) { | ||
| 21 | std::vector<T> data; | ||
| 22 | data.resize(numItems); | ||
| 23 | for (int i=0; i<5; i++) { | ||
| 24 | if (ReadProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * numItems, nullptr)) | ||
| 25 | { | ||
| 26 | return data; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | ThrowError(); | ||
| 30 | return {}; | ||
| 31 | } | ||
| 32 | |||
| 33 | template <class T> | ||
| 34 | void WriteData(const std::vector<int>& offsets, const std::vector<T>& data) { | ||
| 35 | for (int i=0; i<5; i++) { | ||
| 36 | if (WriteProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * data.size(), nullptr)) { | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | ThrowError(); | ||
| 41 | } | ||
| 42 | |||
| 43 | private: | ||
| 44 | void ThrowError(); | ||
| 45 | |||
| 46 | void* ComputeOffset(std::vector<int> offsets); | ||
| 47 | |||
| 48 | std::map<uintptr_t, uintptr_t> _computedAddresses; | ||
| 49 | uintptr_t _baseAddress = 0; | ||
| 50 | HANDLE _handle = nullptr; | ||
| 51 | }; \ No newline at end of file | ||
