Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current instance's executable file name from native win32 C++ app? [duplicate]

Tags:

c++

winapi

Possible Duplicate:
How to get the application executable name in Windows (C++ Win32 or C++/CLI)?

How can I get the current instance's file name & path from within my native win32 C++ application?

For example; if my application was c:\projects\testapps\getapppath.exe it would be able to tell the path is c:\projects\testapps\getapppath.exe

like image 852
John MacIntyre Avatar asked Oct 22 '10 20:10

John MacIntyre


4 Answers

You can do this via the GetModuleFileName function.

TCHAR szFileName[MAX_PATH];

GetModuleFileName(NULL, szFileName, MAX_PATH)
like image 83
Garett Avatar answered Nov 16 '22 10:11

Garett


GetCurrentProcess, then QueryFullProcessImageName

Other answers are better for your own process - this is preferred for remote ones. Per the docs:

To retrieve the module name of the current process, use the GetModuleFileName function with a NULL module handle. This is more efficient than calling the GetProcessImageFileName function with a handle to the current process.

To retrieve the name of the main executable module for a remote process in win32 path format, use the QueryFullProcessImageName function.

like image 41
Steve Townsend Avatar answered Nov 16 '22 10:11

Steve Townsend


See GetModuleFileName()

like image 3
Thanatos Avatar answered Nov 16 '22 11:11

Thanatos


UPDATE: Works only for console applications!

The program's path is passed as first argument, It's stored in argv[0] in the main(argc, argv[]) function.

like image 2
Ed.C Avatar answered Nov 16 '22 11:11

Ed.C