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
.
Trying to compile the following code gives the following errors (I know the code, even if run, would do nothing as-is. But this is a first step):
wstring
was not declared in this scope
szModName
was not declared in this scopeexpected
;
beforewstrModContain
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;
}