4

I am trying to retrieve the name of remote processes using "GetProcessImageFileName". However, the GCC (MinGW) linker fails with the following error(s):

<source_file>.c: In function '<function_name>':
 warning: implicit declaration of function 'GetProcessImageFileName' [-Wimplicit-function-declaration]
 GetProcessImageFileName(hProcess, szProcessName, MAX_PATH);
 ^
undefined reference to `GetProcessImageFileName'
collect2.exe: error: ld returned 1 exit status

I've tried compiling with "-lPsapi" and "-lKernel32" but I get the same results. "GetProcessImageFileName" is declared in "Psapi.h".

I'm using a machine with Windows 7 Professional 64-bit and GCC 4.8.1. Any ideas what's going on?

XerZetZip
  • 45
  • 5
  • Only thing I can think of, without actually trying anything, is you're passing a "narrow" string into the Unicode version of the function. Do you have `UNICODE` defined, and what is the type of `szProcessName`? I'm guessing "yes" and `char*` or `std::string`. Some source would be nice. – icabod Nov 06 '14 at 10:57
  • The warning is from the compiler, not the linker, and it means that `GetProcessImageFileName` hasn't been declared. So you need to include the correct header. If the function is not in the header you may need a more recent version of mingw, or mingw itself may need updating. – arx Nov 06 '14 at 11:57
  • @arx Vanilla MinGW lacks pretty much anything new in XP, such as `GetProcessImageFileName()`. If that's what XerZetZip is using, he'll need to switch to mingw-w64. It's a shame that we still today have this issue with people using the wrong MinGW... – andlabs Nov 06 '14 at 14:45
  • @andlabs Cheers, I've expanded that into an answer. – arx Nov 06 '14 at 15:27
  • @icabod szProcessName is an array of `char`s – XerZetZip Nov 07 '14 at 01:41
  • @andlabs I included the correct header file `psapi.h` and `-lpsapi` and `-lKernel32` in the command line. I try to keep MinGW up to data but it appears there's no new developments. I also tried to get MinGW-w64 but had extensive issue getting it to work properly. – XerZetZip Nov 07 '14 at 01:43

1 Answers1

2

Make sure you define the macro _WIN32_WINNT to be larger or equal 0x0501 (Windows XP, minimum system supports GetProcessImageFileName). Either use the switch -D_WIN32_WINNT=0x0501 or before you include the headers:

#define _WIN32_WINNT 0x0501
#include <Windows.h>
#include <Psapi.h>