The task is this - you need to get the address of the module I need in a foreign application from my application. My attempts:

const HANDLE process_controll::ProcessControll::getProcessHandle(const tstring & processName, const DWORD& accessFlag) { return OpenProcess(accessFlag, false, getID(processName)); } DWORD getModuleAdrEx(const TCHAR * processName, TCHAR* moduleName) { HANDLE procHandle = t_proc.getProcessHandle(processName); if (!procHandle) { msg_er_full; return 0; } HMODULE module{}; if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, moduleName, &module)) { msg_er_full; return 0; } MODULEINFO moduleInf{}; if (GetModuleInformation(procHandle, module, &moduleInf, sizeof(MODULEINFO)) == NULL) { return 0; } CloseHandle(procHandle); return moduleInf.lpBaseOfDll; } 

Actually, the function GetModuleInformation returns "failure", and, accordingly, in moduleInf.lpBaseOfDll - 0.

ps process ID 100% true always finds, the process handle is also not equal to 0, GetModuleHandleEx returns the "truth".

  • What about GetLastError after failing with GetModuleInformation? - Vladimir Martyanov
  • @Vladimir Martyanov, incorrect form, although I checked - and the process handle is not equal to 0 and the module returns non-zero - Duracell
  • And what is the code number? - Vladimir Martyanov
  • @Vladimir Martyanov, well, I’m deciphering what this code means is the wrong descriptor. - Duracell
  • Tell me the number, or the name ERROR_ - Vladimir Martyanov

1 answer 1

The answer turned out to be simple, who cares, I attach the code

 std::vector<MODULEENTRY32> process_controll::ProcessControll::getModuleListFromProcess(tstring processName) { std::vector<MODULEENTRY32> containerModule{}; MODULEENTRY32 MdlEntry{}; MdlEntry.dwSize = sizeof(MODULEENTRY32); HANDLE pMdlSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, getID(processName)); if (!pMdlSnap) { return std::vector<MODULEENTRY32>(); } while (Module32Next(pMdlSnap, &MdlEntry)) { containerModule.push_back(MdlEntry); } if (CloseHandle(pMdlSnap) == 0) { msg_er_full; } return containerModule; } 

Using:

 for (auto it : t_proc.getModuleListFromProcess(_T("processName"))) { console("%#x", it.modBaseAddr); } 
  • one
    IMHO, it would be good to describe the error verbally. - αλεχολυτ
  • What was the matter? - Vladimir Martyanov
  • one
    @Vladimir Martyanov - I didn’t understand what the error was of my first method - I just found an alternative working variant! - Duracell