-1

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

ShadowRanger
  • 124,179
  • 11
  • 158
  • 228
  • Oops, I thought my tag edits would disable my dupehammer, but I guess since I didn't add `c++`, it was still active. I think that should answer your question, but if it doesn't, let me know, I'll unlock. – ShadowRanger Jun 01 '22 at 23:37
  • *"program2 is located each time at a different address"* - Based on what evidence did you arrive at that conclusion? `FindWindow(NULL, "window")` - Is there actually a window with that window name? If so, are you sure it's the one you are looking for? `"window"` sounds like a rather generic choice. – IInspectable Jun 02 '22 at 04:36
  • "window" is just a pseudo name, I write full path to exe there and I'm sure it works fine because it finds pID correctly – Влад Бурцевич Jun 03 '22 at 10:58

0 Answers0