如何使用用g ++编译的代码读取进程的内存?

I want to read the process memory of notepad.exe and find the string Hello World! inside it (it's typed inside a Notepad window).

I want to use g++.exe, not cl.exe, because it's too much of a hassle to try to figure out how to set the proper environment variables to be able to use it from the command line instead of from inside Visual Studio. There are other reasons as well, but the thing is I need to use g++.exe.

尝试编译以下代码会产生以下错误(我知道该代码即使运行也不会做任何事情。但这是第一步):

wstring was not declared in this scope

szModName was not declared in this scope

expected ; before wstrModContain

wstrModContain was not declared in this scope

string has not been declared

#include <windows.h>
#include <psapi.h>

HMODULE GetModule();

int main() {
    return 0;
}

HMODULE GetModule() {
    HMODULE hMods[1024];
    HWND hWnd = FindWindowA(0, "Untitled - Notepad");
    DWORD pID;
    GetWindowThreadProcessId(hWnd, &pID);
    HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, pID);
    DWORD cbNeeded;
    unsigned int i;
    if (EnumProcessModules(pHandle, hMods, sizeof(hMods), &cbNeeded)) {
        for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) {
            wstring szModName[MAX_PATH];
            if (GetModuleFileNameEx(pHandle, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) {
                TCHAR* wstrModName = szModName;
                wstring wstrModContain = "notepad.exe";
                if (wstrModName.find(wstrModContain) != string::npos) {
                    CloseHandle(pHandle);
                    return hMods[i];
                }
            }
        }
    }
    return nullptr;
}
评论