How do I find the location of the executable in C?
To summarize:
On Unixes with
/proc
really straight and realiable way is to:readlink("/proc/self/exe", buf, bufsize)
(Linux)readlink("/proc/curproc/file", buf, bufsize)
(FreeBSD)readlink("/proc/self/path/a.out", buf, bufsize)
(Solaris)
On Unixes without
/proc
(i.e. if above fails):If argv[0] starts with "/" (absolute path) this is the path.
Otherwise if argv[0] contains "/" (relative path) append it to cwd
(assuming it hasn't been changed yet).Otherwise search directories in
$PATH
for executableargv[0]
.
Afterwards it may be reasonable to check whether the executable isn't actually a symlink.
If it is resolve it relative to the symlink directory.This step is not necessary in /proc method (at least for Linux).
There the proc symlink points directly to executable.Note that it is up to the calling process to set
argv[0]
correctly.
It is right most of the times however there are occasions when the calling process cannot be trusted (ex. setuid executable).On Windows: use
GetModuleFileName(NULL, buf, bufsize)
how to get path to executable in C running on Windows?
Use GetModuleFileName() and pass NULL as the first argument:
DWORD last_error;
DWORD result;
DWORD path_size = 1024;
char* path = malloc(1024);
for (;;)
{
memset(path, 0, path_size);
result = GetModuleFileName(0, path, path_size - 1);
last_error = GetLastError();
if (0 == result)
{
free(path);
path = 0;
break;
}
else if (result == path_size - 1)
{
free(path);
/* May need to also check for ERROR_SUCCESS here if XP/2K */
if (ERROR_INSUFFICIENT_BUFFER != last_error)
{
path = 0;
break;
}
path_size = path_size * 2;
path = malloc(path_size);
}
else
{
break;
}
}
if (!path)
{
fprintf(stderr, "Failure: %d\n", last_error);
}
else
{
printf("path=%s\n", path);
}
Getting the absolute path of the executable, using C#?
MSDN has an article that says to use System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
; if you need the directory, use System.IO.Path.GetDirectoryName
on that result.
Or, there's the shorter Application.ExecutablePath
which "Gets the path for the executable file that started the application, including the executable name" so that might mean it's slightly less reliable depending on how the application was launched.
C++ [Windows] Path to the folder where the executable is located
Use GetModuleFileName to find out where your exe is running from.
WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);
Then strip the exe name from path.
how to find the location of the executable in C in OS X
Mac OS X implements the functions:
int _NSGetExecutablePath(char * buf, uint32_t * length);
int * _NSGetArgc(void);
char *** _NSGetArgv(void);
The first of these is probably what you want here. The other two are interesting as well, though!
Get path of executable
There is no cross platform way that I know.
For Linux: pass "/proc/self/exe"
to std::filesystem::canonical
or readlink
.
Windows: pass NULL as the module handle to GetModuleFileName
.
Related Topics
Can Someone Explain This Template Code That Gives Me the Size of an Array
Returning Unique_Ptr from Functions
Separating Class Code into a Header and Cpp File
How to Access Private Members from Outside the Class Without Using Friends
Does C++ Support Variable Length Arrays
C++ Standard Library: How to Write Wrappers For Cout, Cerr, Cin and Endl
How to Implement Aba Counter With C++11 Cas
Why Is My Program Slow When Looping Over Exactly 8192 Elements
Std::Vector::Resize() Vs. Std::Vector::Reserve()
How to Terminate a Thread in C++11
C++: What Is the Size of an Object of an Empty Class
Function Passed as Template Argument
How to Detect Whether There Is a Specific Member Variable in Class
Evaluating Arithmetic Expressions from String in C++
Opengl - How to Create Order Independent Transparency
Why Does Printf Not Print Out Just One Byte When Printing Hex