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
You can do this via the GetModuleFileName function.
TCHAR szFileName[MAX_PATH];
GetModuleFileName(NULL, szFileName, MAX_PATH)
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.
See GetModuleFileName()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With