Mkv47

C++ code that reads the last memory address for pointer [base addresses and offsets] from a window

May 18th, 2023 (edited)
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <Windows.h>
  5. #include <TlHelp32.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <chrono>
  9. #include <thread>
  10. #include <tchar.h>
  11.  
  12. DWORD processID;
  13. // Method to get process ID from an active window if you can get it in another way you can remove it.
  14. void GetProcessIDFromWindow(LPCSTR windowName) {
  15.     HWND hwnd = NULL;
  16.     while (hwnd == NULL)
  17.     {
  18.         hwnd = FindWindowA(NULL, (windowName));
  19.         GetWindowThreadProcessId(hwnd, &processID);
  20.         if(hwnd == NULL){std::cout << "No program found please make sure that the program has started..." << std::endl;
  21.             std::chrono::seconds delay(2);
  22.             std::this_thread::sleep_for(delay);
  23.         }
  24.     }
  25. }
  26. //Method to get Module Base Address and the process handle
  27. uintptr_t GetModuleBaseAddress(DWORD processId, const char* moduleName)
  28. {
  29.     HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processId);
  30.     if (processHandle == NULL)
  31.     {
  32.         // Failed to open the process
  33.         return 0;
  34.     }
  35.  
  36.     HMODULE moduleHandle = NULL;
  37.     MODULEENTRY32 moduleEntry;
  38.     moduleEntry.dwSize = sizeof(MODULEENTRY32);
  39.  
  40.     // Iterate through the loaded modules in the process to find the target module
  41.     HANDLE moduleSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, processId);
  42.     if (moduleSnapshot != INVALID_HANDLE_VALUE)
  43.     {
  44.         if (Module32First(moduleSnapshot, &moduleEntry))
  45.         {
  46.             do
  47.             {
  48.                 if (_stricmp(moduleEntry.szModule, moduleName) == 0) //there is an error here you can try to fix it but it doesn't effect this code. note* i hate every function that starts with "_".
  49.                 {
  50.                     moduleHandle = moduleEntry.hModule;
  51.                     break;
  52.                 }
  53.             } while (Module32Next(moduleSnapshot, &moduleEntry));
  54.         }
  55.         CloseHandle(moduleSnapshot);
  56.     }
  57.     if (moduleHandle == NULL)
  58.     {
  59.         // Module not found
  60.         CloseHandle(processHandle);
  61.         return 0;
  62.     }
  63.     uintptr_t baseAddress = reinterpret_cast<uintptr_t>(moduleHandle);
  64.     CloseHandle(processHandle);
  65.     return baseAddress;
  66. }
  67. int main() {
  68.     LPCSTR winName = "Program Window Name"; //Put the window name that you want to read from
  69.     GetProcessIDFromWindow(winName);
  70.     std::fstream pIDFile;
  71.     pIDFile.open("pID.txt", std::ios::out);
  72.     pIDFile << processID;
  73.     pIDFile.close();
  74.         uintptr_t baseAddress = NULL;
  75.     HANDLE processHandle = NULL;
  76.     processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
  77.     if (processHandle == INVALID_HANDLE_VALUE || processHandle == NULL) {
  78.         // error handling
  79.         std::cout << "Failed to open process" << std::endl;
  80.         return 0;
  81.     }
  82.     const char* moduleName = "Program.exe";  // Replace with the name of your module, you can find it with the base address if you use cheat engine
  83.     baseAddress = GetModuleBaseAddress(processID, moduleName);
  84.     DWORD gameBaseAddress= baseAddress;
  85.     DWORD offsetGameToBaseAdress = 0x00000000;
  86.  
  87.     std::vector<DWORD> pointsOffsets{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //Place your offsets here you can add single digit numbers with or without an extra zero but make sure that the hex identfier is included "0x" before the number
  88.     //Get value at gamebase+offset -> store it in baseAddress
  89.     ReadProcessMemory(processHandle, (LPVOID)(gameBaseAddress+ offsetGameToBaseAdress), &baseAddress, sizeof(baseAddress), NULL);
  90.  
  91.     DWORD pointsAddress = baseAddress; //The Adress we need -> change now while going through offsets
  92.  
  93.     for (int i = 0; i < pointsOffsets.size() - 1; i++) // -1 because we dont want the value at the last offset
  94.     {
  95.         ReadProcessMemory(processHandle, (LPVOID)(pointsAddress + pointsOffsets.at(i)), &pointsAddress, sizeof(pointsAddress), NULL);
  96.     }
  97.  
  98.     pointsAddress += pointsOffsets.at(pointsOffsets.size()-1); //Add Last offset -> done!
  99.     std::cout<< "0x" << std::hex <<pointsAddress <<std::endl;
  100.     std::cout << "test" << std::endl;
  101.     std::chrono::seconds delay(20);
  102.     std::this_thread::sleep_for(delay);
  103. }
  104. /** This code was made by me but using other people code snippets and my own
  105.  * Thank to CasualGamer youtube channel */
Advertisement
Add Comment
Please, Sign In to add comment