Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <tlhelp32.h>
- #include "memory.hpp"
- Memory::~Memory() {
- if (!CloseHandle(m_hProcess))
- std::cout << "Cannot close the handle.\nGetLastError: " << GetLastError() << std::endl;
- }
- bool Memory::Attach(const char* ProcessName, DWORD dwDesiredAccess) {
- DWORD PID = 0;
- PROCESSENTRY32 entry;
- entry.dwFlags = sizeof(PROCESSENTRY32);
- HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
- if (Process32First(snapshot, &entry) == TRUE) {
- while (Process32Next(snapshot, &entry) == TRUE) {
- if (strcmp(entry.szExeFile, ProcessName) == 0)
- PID = entry.th32ProcessID;
- else
- return false;
- }
- } else
- return false;
- if (!(Memory::m_hProcess = OpenProcess(dwDesiredAccess, FALSE, PID))) {
- std::cout << "Cannot open handle to process with PID: " << PID << "\nGetLastError: " << GetLastError() << std::endl;
- return false;
- }
- return true;
- }
- bool Memory::Attach(DWORD ProcessID, DWORD dwDesiredAccess) {
- if (!(Memory::m_hProcess = OpenProcess(dwDesiredAccess, FALSE, ProcessID))) {
- std::cout << "Cannot open handle to process with PID: " << ProcessID << "\nGetLastError: " << GetLastError() << std::endl;
- return false;
- }
- return true;
- }
- template <class T>
- T Memory::Read(DWORD64 Address) {
- T buffer;
- if (!ReadProcessMemory(Memory::m_hProcess, (LPCVOID)Address, buffer, sizeof(T), NULL)) {
- std::cout << "Cannot read from address: " << Address << "\nGetLastError: " << GetLastError() << std::endl;
- return 0;
- }
- return buffer;
- }
- template <class T>
- bool Memory::Write(DWORD64 Address, T dataBuffer) {
- if (!WriteProcessMemory(Memory::m_hProcess, Address, dataBuffer, sizeof(T), NULL)) {
- std::cout << "Cannot write to address: " << Address << "\nGetLastError: " << GetLastError() << std::endl;
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement