1

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.
  • QueryFullProcessImageName
  • GetModuleFileName
  • GetModuleFileNameEx
  • GetProcessImageFileName

All of the above methods have caused an error.

Is this a problem with the version?

Please answer. Thank you :)

areum
  • 93
  • 3
  • 14
  • 1
    _"... I am using visual c ++ 6.0..."_ if you mean Visual Studio 6 than please update to a newer verson: https://en.wikipedia.org/wiki/Microsoft_Visual_Studio#6.0_(1998) MSVC 2017 is free for personal/small business use. – Richard Critten Feb 19 '18 at 01:08
  • I have reason to use that version... – areum Feb 19 '18 at 01:15
  • Problem is all the online documentation and also my memory have been retired. What do the docs than come with VC 6 say is the library required for that function? The online MSDN docs only go back as far as Windows XP. https://msdn.microsoft.com/en-us/library/windows/desktop/ms683198(v=vs.85).aspx – Richard Critten Feb 19 '18 at 01:18

1 Answers1

1

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.

273K
  • 29,503
  • 10
  • 41
  • 64
  • It was resolved by adding `#pragma comment (lib, "psapi.lib")` Thank you~ – areum Feb 19 '18 at 01:43
  • 1
    @areum, `QueryFullProcessImageName` should be used in Vista and later. It requires only limited access to the target process. Note that, as an unreliable implementation detail, I see that in Windows 10 `GetModuleFileNameEx` also only needs limited access when `hModule` is `NULL`. For this case it's implemented the same as `QueryFullProcessImageName`, by calling `NtQueryInformationProcess` to get the `ProcessImageFileNameWin32`. Of course, you should assume that `GetModuleFileNameEx` needs full access to read virtual memory in the target process. – Eryk Sun Feb 19 '18 at 03:10