Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// MemoryManager.h
- // All-inclusive packet to read-write memory easily.
- // Use with caution
- /// CODED BY DOUBLE V
- /// Credits: TeamGamerFood
- /// Oh, I forgot, this is not documented because I'm a lazy ass.
- /// But you should understand how to use it...
- #pragma once
- #include <Windows.h>
- #include <TlHelp32.h>
- class MemoryManager
- {
- private:
- DWORD dwPID;
- HANDLE hProcess;
- public:
- MODULEENTRY32 Client, Engine;
- DWORD ClientBase, EngineBase;
- bool AttachProcess(char* ProcessName)
- {
- HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
- if (hPID == INVALID_HANDLE_VALUE) return false;
- PROCESSENTRY32 procEntry;
- procEntry.dwSize = sizeof(procEntry);
- const WCHAR* procNameChar;
- int nChars = MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, NULL, 0);
- procNameChar = new WCHAR[nChars];
- MultiByteToWideChar(CP_ACP, 0, ProcessName, -1, (LPWSTR)procNameChar, nChars);
- do
- if (!wcscmp(procEntry.szExeFile, procNameChar))
- {
- this->dwPID = procEntry.th32ProcessID;
- CloseHandle(hPID);
- this->hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, this->dwPID);
- return true;
- }
- while (Process32Next(hPID, &procEntry));
- CloseHandle(hPID);
- return false;
- }
- MODULEENTRY32 GetModule(char* ModuleName)
- {
- HANDLE hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
- MODULEENTRY32 mEntry;
- if (hModule == INVALID_HANDLE_VALUE)
- {
- mEntry.modBaseAddr = 0x0;
- return mEntry;
- }
- mEntry.dwSize = sizeof(mEntry);
- const WCHAR *modNameChar;
- int nChars = MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, NULL, 0);
- modNameChar = new WCHAR[nChars];
- MultiByteToWideChar(CP_ACP, 0, ModuleName, -1, (LPWSTR)modNameChar, nChars);
- do
- if (!wcscmp(mEntry.szModule, modNameChar))
- {
- CloseHandle(hModule);
- return mEntry;
- }
- while (Module32Next(hModule, &mEntry));
- CloseHandle(hModule);
- mEntry.modBaseAddr = 0x0;
- return mEntry;
- }
- bool Init()
- {
- dwPID = NULL;
- hProcess = NULL;
- if (!AttachProcess("csgo.exe")) return false;
- Client = GetModule("client.dll");
- Engine = GetModule("engine.dll");
- ClientBase = (DWORD)Client.modBaseAddr;
- EngineBase = (DWORD)Engine.modBaseAddr;
- if (ClientBase == 0x0 || EngineBase == 0x0) return false;
- }
- bool Clear()
- {
- CloseHandle(hProcess);
- }
- MemoryManager()
- {
- Init();
- }
- ~MemoryManager()
- {
- Clear();
- }
- template <class T>
- T Read(DWORD dwAddress)
- {
- T value;
- if (!ReadProcessMemory(hProcess, (LPVOID)dwAddress, &value, sizeof(T), NULL)) value = NULL;
- return value;
- }
- template <class T>
- T ReadSize(DWORD dwAddress, size_t Size)
- {
- T value;
- if (!ReadProcessMemory(hProcess, (LPVOID)dwAddress, &value, Size, NULL)) value = NULL;
- return value;
- }
- template <class T>
- T* ReadArray(DWORD dwAddress, size_t ArraySize)
- {
- T* arr;
- if (!ReadProcessMemory(hProcess, (LPVOID)dwAddress, arr, sizeof(T)*ArraySize, NULL)) arr = nullptr;
- return arr;
- }
- template <class T>
- bool Write(DWORD dwAddress, T ValueToWrite)
- {
- return WriteProcessMemory(hProcess, (LPVOID)dwAddress, &ValueToWrite, sizeof(T), NULL) == TRUE;
- }
- template <class T>
- bool WriteSize(DWORD dwAddress, T ValueToWrite, size_t Size)
- {
- return WriteProcessMemory(hProcess, (LPVOID)dwAddress, &ValueToWrite, Size, NULL) == TRUE;
- }
- template <class T>
- bool WriteArray(DWORD dwAddress, T* ArrayToWrite, size_t ArraySize)
- {
- return WriteProcessMemory(hProcess, (LPVOID)dwAddress, ArrayToWrite, sizeof(T)*ArraySize, NULL) == TRUE;
- }
- template <class T>
- bool WriteProtected(DWORD dwAddress, T ValueToWrite)
- {
- DWORD_PTR oldprotect;
- if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), PAGE_EXECUTE_READWRITE, &oldprotect)) return false;
- if (!Write(dwAddress, ValueToWrite)) return false;
- if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), oldprotect, NULL)) return false;
- return true;
- }
- template <class T>
- bool WriteProtectedSize(DWORD dwAddress, T ValueToWrite, size_t Size)
- {
- DWORD_PTR oldprotect;
- if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), PAGE_EXECUTE_READWRITE, &oldprotect)) return false;
- if (!WriteSize(dwAddress, ValueToWrite, Size)) return false;
- if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), oldprotect, NULL)) return false;
- return true;
- }
- template <class T>
- bool WriteArrayProtected(DWORD dwAddress, T* ArrayToWrite, size_t ArraySize)
- {
- DWORD_PTR oldprotect;
- if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), PAGE_EXECUTE_READWRITE, &oldprotect)) return false;
- if (!WriteArray(dwAddress, ArrayToWrite, ArraySize)) return false;
- if (!VirtualProtectEx(hProcess, (LPVOID)dwAddress, sizeof(T), oldprotect, NULL)) return false;
- return true;
- }
- DWORD GetProcID() { return dwPID; }
- HANDLE GetProcHandle() { return hProcess; }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement