Advertisement
Guest User

Untitled

a guest
Oct 12th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <tlhelp32.h>
  4.  
  5. #include "memory.hpp"
  6.  
  7. Memory::~Memory() {
  8.     if (!CloseHandle(m_hProcess))
  9.         std::cout << "Cannot close the handle.\nGetLastError: " << GetLastError() << std::endl;
  10. }
  11.  
  12. bool Memory::Attach(const char* ProcessName, DWORD dwDesiredAccess) {
  13.     DWORD PID = 0;
  14.  
  15.     PROCESSENTRY32 entry;
  16.     entry.dwFlags = sizeof(PROCESSENTRY32);
  17.     HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
  18.     if (Process32First(snapshot, &entry) == TRUE) {
  19.         while (Process32Next(snapshot, &entry) == TRUE) {
  20.             if (strcmp(entry.szExeFile, ProcessName) == 0)
  21.                 PID = entry.th32ProcessID;
  22.             else
  23.                 return false;
  24.         }
  25.     } else
  26.         return false;
  27.  
  28.     if (!(Memory::m_hProcess = OpenProcess(dwDesiredAccess, FALSE, PID))) {
  29.         std::cout << "Cannot open handle to process with PID: " << PID << "\nGetLastError: " << GetLastError() << std::endl;
  30.         return false;
  31.     }
  32.  
  33.     return true;
  34. }
  35.  
  36. bool Memory::Attach(DWORD ProcessID, DWORD dwDesiredAccess) {
  37.     if (!(Memory::m_hProcess = OpenProcess(dwDesiredAccess, FALSE, ProcessID))) {
  38.         std::cout << "Cannot open handle to process with PID: " << ProcessID << "\nGetLastError: " << GetLastError() << std::endl;
  39.         return false;
  40.     }
  41.     return true;
  42. }
  43.  
  44. template <class T>
  45. T Memory::Read(DWORD64 Address) {
  46.     T buffer;
  47.     if (!ReadProcessMemory(Memory::m_hProcess, (LPCVOID)Address, buffer, sizeof(T), NULL)) {
  48.         std::cout << "Cannot read from address: " << Address << "\nGetLastError: " << GetLastError() << std::endl;
  49.         return 0;
  50.     }
  51.     return buffer;
  52. }
  53.  
  54. template <class T>
  55. bool Memory::Write(DWORD64 Address, T dataBuffer) {
  56.     if (!WriteProcessMemory(Memory::m_hProcess, Address, dataBuffer, sizeof(T), NULL)) {
  57.         std::cout << "Cannot write to address: " << Address << "\nGetLastError: " << GetLastError() << std::endl;
  58.         return false;
  59.     }
  60.     return true;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement