-3

Trying to get the full path of the executable from a pid. It fails to compile no matter what I tried. g++ (MinGW.org GCC-6.3.0-1) 6.3.0

#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <process.h>
#include <string>
#include <iostream>

using namespace std;
string getProcessName(unsigned long pid)
{
    string name = "";
    TCHAR processPath[MAX_PATH] = { 0 };
    DWORD len = MAX_PATH;
    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid );
    int rc = GetProcessImageFileName(hProcess, processPath, len);
    if(rc > 0)
    {
//      GET STRING HERE
    }
    CloseHandle(hProcess);
    return name;
}

output:

..\src\Test.cpp:15:61: error: 'GetProcessImageFileName' was not declared in this scope
wohlstad
  • 12,661
  • 10
  • 26
  • 39
joseph lake
  • 53
  • 1
  • 7
  • 1
    You shouldn't keep editing your question after you got answer(s). Your original question only mentioned your compilation error. And BTW I don't see how the same code (that failed to compile) even got to the linker stage (to produce linker errors). Please post a new question with all the relevant details for the linker issue. – wohlstad Jun 13 '23 at 07:46

2 Answers2

0

As you can see in the GetProcessImageFileName documentation,
It is declared in the header psapi.h.

Therefore you need to add:

#include <psapi.h>

Side notes:

  1. using namespace std; is not recommended. See: Why is "using namespace std;" considered bad practice?.
  2. The error in your question body is a compilation error. In your title you mentioned Undefined Reference which is usually a linker error.
  3. After you solve the compilation error, you will need to link with the library that contains the implementation for GetProcessImageFileName. On MSVC these are either Psapi.lib or Kernel32.lib depending on your OS. See the documentation link above. I am not sure what you should use for MinGW.

Edit:
As mentioned in the comment below: the equivalent for Psapi.lib for MinGW is libpsapi.a.
See: How to add Psapi.lib to TARGETLIBS in Eclipse with G++ MinGW.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • no effect compile error – joseph lake Jun 13 '23 at 07:42
  • 1
    Are you sure you still get the same compilation error after you add the missing #include ? Or is it a linker error now ? – wohlstad Jun 13 '23 at 07:52
  • yes I had to add someone's fix to add libs to eclipse from mingw in order to fix it. then it outputed an invalid path like \Device\HarddiskVolume3\Users\jredfox\Documents\dev\OpenTerminal-natives\Debug\ instead of an actual path – joseph lake Jun 13 '23 at 08:05
  • I fixed it by linking psapi from https://stackoverflow.com/a/48257640/8477015 – joseph lake Jun 13 '23 at 08:22
0

It's a linking error between eclipse and mingw . The awnser was found here https://stackoverflow.com/a/48257640/8477015

And I changed my code to a working one below

#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <psapi.h>

string getProcessName(unsigned long pid)
{
    string name = "";
    HANDLE phandle = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
    TCHAR filename[MAX_PATH];
    GetModuleFileNameEx(phandle, NULL, filename, MAX_PATH);
    CloseHandle(phandle);
    return string(filename);
}
joseph lake
  • 53
  • 1
  • 7