1

This is with the compiler that comes with Dev-C++ when run on Windows 7, "TDM-GCC 4.9.2 64-bit Release". The same thing happens with the 64-bit version as well. The "add the following flags when calling the linker" in "Compiler Options" is set to -static-libgcc -lws2_32, and "add the following flags when calling the compiler" is std=c++11.

Unfortunately, I can't get much more information than that because this is a very, very locked down computer.

Once I get home, I'll test it there and edit in any new information.

A test file:

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

int main() {
    HANDLE h = GetCurrentProcess();
    TCHAR name[100];
    GetProcessImageFileName(h, name, 100);
    std::cout << "Name is " << std::endl;
}

When compiled, it gives the following error:

C:\Users\[REDACTED]\AppData\Local\Temp\ccQz3L9P.o:test.cpp:(.text+0x2f): undefined reference to `GetProcessImageFileNameA'

As best as I can tell, it's calling GetProcessImageFileNameA instead of GetProcessImageFileName because of a #define in psapi.h:

#define GetProcessImageFileName __MINGW_NAME_AW(GetProcessImageFileName)

__MINGW_NAME_AW(func) is #defined in _mingw_unicode.h as func##A, which if I understand it correctly is where the new method name is coming from.

Back in psapi.h, DWORD WINAPI GetProcessImageFileNameA(HANDLE, LPSTR, DWORD); is declared, but I haven't found its definition anywhere, and Dev-C++ can't either. I'm fairly certain that's the problem.

How can I fix it? I'm open to anything, up to and including switching compilers or IDEs, as long as I can put whatever I use on a flash drive and carry it around with as little annoyance as possible, and that the end result doesn't require admin privileges to run.

Nic
  • 6,211
  • 10
  • 46
  • 69
  • It's "definition" is found in the import library you're using to link (as all other kernel Windows API functions are found) In `kernel32.lib`. https://msdn.microsoft.com/en-us/library/windows/desktop/ms683217%28v=vs.85%29.aspx. So where is this import library wrt Dev-C++? (which BTW is *not* a compiler, it is an IDE). – PaulMcKenzie Oct 15 '15 at 17:30

2 Answers2

2

GetProcessImageFileName is a windows API function and you need to link in the correct library to get to it.

It is defined as a macro to be either GetProcessImageFileNameA or GetProcessImageFileNameW depending whether you are compiling "wide" or "narrow".

The Win32 API has Unicode and ascii versions of each function (at least the functions that take strings). In general, the ascii version converts the characters to Unicode and calls the other implantation internally.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • I thought that `-lws2_32` (since it's GCC) would link the appropriate library. If that's not the case, how can I find out which to link? Is it `-lpsapi`? – Nic Oct 15 '15 at 18:21
  • MSDN references the correct .lib and .h for each function. A google search should land you and the correct page for any given Windows API function. In this case it is https://msdn.microsoft.com/en-us/library/windows/desktop/ms683217(v=vs.85).aspx – Niall Oct 15 '15 at 18:24
1

you need to link Psapi.lib or Kernel32.lib into your project. I am not sure about your compiler but in VC++ you add #pragma comment(lib, "Psapi.Lib") to your code or add Psapi.lib in additional libraries of project properties.

Sam

Sam
  • 2,473
  • 3
  • 18
  • 29
  • "_This is with the compiler that comes with Dev-C++ when run on Windows 7, "TDM-GCC 4.9.2 64-bit Release"._" -- the first sentence of the question. :P Aside from that, is there a way to do that through GCC command-line arguments? – Nic Oct 15 '15 at 18:22
  • @QPaysTaxes yes, just add `-lpsapi` to the end of the command – Jonathan Wakely Oct 15 '15 at 18:33