简体   繁体   中英

Get full path from a process in Windows

there's something wrong with this api. i already check return function. no error. but output nothing.

HANDLE hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32 process;
process.dwSize = sizeof(PROCESSENTRY32);
Process32First(hSnapProcess, &process);
do
{
    if (process.th32ProcessID != 0)
    {
        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
        if (hProcess != NULL) 
        {
            wchar_t filePath[MAX_PATH];
            if (!GetModuleFileNameExW(hProcess, NULL, filePath, MAX_PATH))
            {
                std::wcout << filePath << std::endl;
            }
        }
        CloseHandle(hProcess);
    }

} while (Process32Next(hSnapProcess, &process));
CloseHandle(hSnapProcess);

Two clear mistakes can be seen in your code, both can be understood by reading the documentation.

Firstly, in GetModuleFileNameEx :

The handle must have the PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights.

Your handle only has PROCESS_QUERY_INFORMATION .

Secondly, again in GetModuleFileNameEx :

If the function succeeds, the return value specifies the length of the string copied to the buffer.

If the function fails, the return value is zero.

Your logic is back-to-front. Replace:

if (!GetModuleFileNameExW(hProcess, NULL, filePath, MAX_PATH))

with

if (GetModuleFileNameExW(hProcess, NULL, filePath, MAX_PATH))

Nothing is wrong with this API, the only thing that's wrong here is your code.

The documentation clearly states that the return value of GetModuleFileNameExW is the length of the string copied to the buffer.

If the return value is 0, the function has failed.

So you simply need to write this:

...
if (GetModuleFileNameExW(hProcess, NULL, filePath, MAX_PATH) != 0)
{
   // handle "success" case
}
...

BTW CloseHandle(hProcess); should be inside the if (hProcess != NULL) block.

Full working example with error checks

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

int main()
{
  HANDLE hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

  if (hSnapProcess != INVALID_HANDLE_VALUE)
  {
    PROCESSENTRY32 process;
    process.dwSize = sizeof(PROCESSENTRY32);
    Process32First(hSnapProcess, &process);
    do
    {
      if (process.th32ProcessID != 0)
      {
        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID);
        if (hProcess != NULL)
        {
          wchar_t filePath[MAX_PATH];
          if (GetModuleFileNameExW(hProcess, NULL, filePath, MAX_PATH))
          {
            std::wcout << filePath << std::endl;
          }
          else
          {
            std::wcout << L"GetModuleFileNameExW failed with error" << GetLastError() << std::endl;
          }

          CloseHandle(hProcess);
        }
      }

    } while (Process32Next(hSnapProcess, &process));

    CloseHandle(hSnapProcess);
  }
  else
  {
    std::wcout << L"CreateToolhelp32Snapshot failed with error" << GetLastError() << std::endl;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM