Possible Duplicate:
Get path of executable
I have an application on C++, how can I get my full directory (where .exe file of this application exists)?
Edit: OS - Windows
Windows:
WCHAR path[MAX_PATH];
GetModuleFileName(NULL, path, ARRAYSIZE(path));
Linux:
char buffer[MAX_PATH];
readlink("/proc/self/exe", buffer, MAX_PATH);
There's no good cross-platform solution for this, however, you can do this on all major platforms:
/proc/self/exe
special filechar buf[PATH_MAX];
ssize_t len;
if ((len = readlink("/proc/self/exe", buf, bufsize)) != -1) {
// buf contains the path
} else {
// error
}
_NSGetExecutablePath()
function (from man 3 dyld
)#include <stdint.h>
#include <limits.h>
uint32_t size = PATH_MAX;
char buf[PATH_MAX];
if (_NSGetExecutablePath(buf, &size) != -1) {
// buf now contains the path
} else {
// error
}
GetModuleFileName()
with NULL
as the handle.#include <windows.h>
char buffer[1024];
if (GetModuleFileName(NULL, buffer, sizeof(buffer)) != 0) {
// buffer contains the path
} else {
// error
}