You need to get the full name of the executable file (exe) knowing the process ID.

Based on the statement that the executable itself is the first (main) module of the application, wrote this code:

HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid); MODULEENTRY32 meModuleEntry; meModuleEntry.dwSize = sizeof(MODULEENTRY32); Module32First(hSnapshot, &meModuleEntry); if (INVALID_HANDLE_VALUE == hSnapshot) MessageBox(NULL, _T("ERROR"), _T("Information"), MB_ICONINFORMATION); else MessageBox(NULL, meModuleEntry.szExePath, _T("Information"), MB_ICONINFORMATION); 

Thus, if in pid transfer the identifier of the process, then everything works. But if you pass any other identifier (for example, explorer), then the hSnapshot variable takes the value INVALID_HANDLE_VALUE .

What am I doing wrong? Or is it not possible to view the modules of another process?

  • Resolved! The problem turned out to be that I compiled the application under x86, and ran it on a 64-bit system. WOW64 allows you to run 32-bit applications, but in this case CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid) for 64-bit processes returns INVALID_HANDLE_VALUE , and for 32-bit applications it works fine. - SHSERG
  • If possible, publish the solution found in the form of an answer to your question . - Nicolas Chabanovsky

2 answers 2

Why so difficult. This is easier:

 auto proccess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); if(NULL == process) { auto error = GetLastError(); // ругаемся } else { TCHAR * image_path = nullptr; DWORD image_path_lenght = 0; if(!QueryFullProcessImageName( process , 0 , &image_path , &image_path_lenght )) { // опять ругаемся } else { // готово } CloseHandle(process); process = NULL; } 
  • An interesting feature is QueryFullProcessImageName, it's a pity that only Vista is supported ... - Vladimir Martyanov
  • The fact is that not every process will allow itself to be opened through OpenProcess - SHSERG
  • Again, for OS processes, this code does not work (for example, for explorer.exe or notepad.exe) - SHSERG

Resolved! The problem turned out to be that I compiled the application under x86, and ran it on a 64-bit system. WOW64 allows you to run 32-bit applications, but in this case CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid) for 64-bit processes returns INVALID_HANDLE_VALUE , and for 32-bit applications it works fine.