I want to get the full path of the current process.
I use _getcwd
to get the current working directory. But it not includes file name.
How can I get file name like: filename.exe
?
argv[0]
of your main function is your filename.
A simple code snippet:
#include<stdio.h>
int main(int argc, char** argv)
{
//access argv[0] here
}
If you cannot access/change code in main(), you can do something like this:
std::string executable_name()
{
#if defined(PLATFORM_POSIX) || defined(__linux__) //check defines for your setup
std::string sp;
std::ifstream("/proc/self/comm") >> sp;
return sp;
#elif defined(_WIN32)
char buf[MAX_PATH];
GetModuleFileNameA(nullptr, buf, MAX_PATH);
return buf;
#else
static_assert(false, "unrecognized platform");
#endif
}
On windows you can use:
TCHAR szExeFileName[MAX_PATH];
GetModuleFileName(NULL, szExeFileName, MAX_PATH);
szExeFileName will contain full path + executable name
[edit]
For more portable solution use argv[0]
or some other platform specific code. You can find such aproach here: https://github.com/mirror/boost/blob/master/libs/log/src/process_name.cpp.
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