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
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.
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.
Tested:
int _tmain(int argc, _TCHAR *argv[])
{
_tprintf(L"%s", argv[0]);
return 0;
}
Prints full path.