Every time I try to run the program1 and get base address of program2 I get the same answer, despite the fact that my program2 is located each time at a different address. Here is the code where I define the address
#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <psapi.h>
#include <tchar.h> // _tcscmp
#include <vector>
DWORD_PTR GetProcessBaseAddress( DWORD processID, HANDLE processHandle )
{
DWORD_PTR baseAddress = 0;
HMODULE *moduleArray;
LPBYTE moduleArrayBytes;
DWORD bytesRequired;
if ( processHandle )
{
if ( EnumProcessModules( processHandle, NULL, 0, &bytesRequired ) )
{
if ( bytesRequired )
{
moduleArrayBytes = (LPBYTE)LocalAlloc( LPTR, bytesRequired );
if ( moduleArrayBytes )
{
unsigned int moduleCount;
moduleCount = bytesRequired / sizeof( HMODULE );
moduleArray = (HMODULE *)moduleArrayBytes;
if ( EnumProcessModules( processHandle, moduleArray, bytesRequired, &bytesRequired ) )
{
baseAddress = (DWORD_PTR)moduleArray[0];
}
LocalFree( moduleArrayBytes );
}
}
}
CloseHandle( processHandle );
}
return baseAddress;
}
int main() {
HWND window = FindWindow(NULL, "window");
if (window == NULL) {
std::cout << "Start the process!" << std::endl;
return 0;
}
DWORD pID = NULL; // ID of our Game
GetWindowThreadProcessId(window, &pID);
HANDLE processHandle = NULL;
processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pID);
if (processHandle == INVALID_HANDLE_VALUE || processHandle == NULL) { // error handling
std::cout << "Failed to open process" << std::endl;
return 0;
}
DWORD_PTR baseAddr = GetProcessBaseAddress(pID, processHandle); // bad
char t[100];
ReadProcessMemory(processHandle, (void*)baseAddr, &t, 100, nullptr);
std::cout << t << std::endl;
return 0;
}
This is the code of the program from which I am trying to read the memory
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char t[100];
cout << "Addr: " << &t << endl;
while(cin >> t) {
if (!strcmp(t, "q")) {
break;
}
cout << "I have this text : " << t << endl;
}
char c;
cin >> c;
return 0;
}
I feel like I'm missing something obvious, but I don't see what