0x6B6F77616C74
0x6B6F77616C74

Reputation: 2629

Extracting the current executable name

Firstly, I would like to say that I don't mean the full path, what
GetModuleFileName or argv[0] yield. Is there a smarter solution than dismissing everything before the last backslash?

Upvotes: 5

Views: 7437

Answers (3)

ergohack
ergohack

Reputation: 1358

In Windows C/C++ there is a global variable extern char * _pgmptr that can be read as well as an unsafe string copy from _get_pgmptr(char ** buffer).

Caveats

Only call _get_pgmptr() if your program has a narrow entry point, like main() or WinMain(). The _pgmptr global variable contains the full path to the executable associated with the process. For more information, see _pgmptr, _wpgmptr.

If a DLL is loaded in two processes, its file name in one process may differ in case from its file name in the other process.

The global variable _pgmptr is automatically initialized to the full path of the executable file, and can be used to retrieve the full path name of an executable file.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612954

First of all you want to get hold of the full path to the executable by calling GetModuleFileName passing NULL as the module handle. Then call PathFindFileName to pull out the file name component.

There is in fact a difference between GetModuleFileName and argv[0]. The latter is the name used to start the process. It could be missing the full path, but more importantly here, it could be missing the .exe extension. If you want to know the actual filename then you need to use GetModuleFileName.

Upvotes: 17

Luka Ramishvili
Luka Ramishvili

Reputation: 899

If you use .NET, then here's an answer. But internally it may be calling GetModuleFileName.

Upvotes: 0

Related Questions