areum
areum

Reputation: 103

C++ Windows - How to get process path from its PID (error)

I want to use pid to get the full path of the process.

#include <psapi.h>

HANDLE processHandle = NULL;
TCHAR filename[MAX_PATH];

processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, nPID);
if (processHandle != NULL) 
{
    if (GetModuleFileNameEx(processHandle, NULL, filename, MAX_PATH) == 0) 
    {
        //fail to get module file name
    } 
    else 
    {
        //module file name : filename
    }
    CloseHandle(processHandle);
} 
else 
{
    //fail to open process
}

This is the code that gets the path of the process using pid.

However, when I execute it, the following error occurs.

( I am using visual c ++ 6.0. )

Linking...
Process01Dlg.obj : error LNK2001: unresolved external symbol _GetModuleFileNameExA@16
Debug/Process01.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

All of the above methods have caused an error.

Is this a problem with the version?

Please answer. Thank you :)

Upvotes: 1

Views: 1799

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38991

It seems you forget to link your product with the psapi.lib. Add it to the project dependencies.

Not sure it would work in VC6.0.

However MSDN recommends other functions for retrieving process names:

To retrieve the name of the main executable module for a remote process, use the GetProcessImageFileName or QueryFullProcessImageName function. This is more efficient and more reliable than calling the GetModuleFileNameEx function with a NULL module handle.

Upvotes: 1

Related Questions