答案 0 :(得分:1)
视窗:
WCHAR path[MAX_PATH];
GetModuleFileName(NULL, path, ARRAYSIZE(path));
Linux的:
char buffer[MAX_PATH];
readlink("/proc/self/exe", buffer, MAX_PATH);
答案 1 :(得分:0)
没有良好的跨平台解决方案,但是,您可以在所有主要平台上执行此操作:
/proc/self/exe
特殊文件char buf[PATH_MAX];
ssize_t len;
if ((len = readlink("/proc/self/exe", buf, bufsize)) != -1) {
// buf contains the path
} else {
// error
}
_NSGetExecutablePath()
函数(来自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()
作为句柄调用NULL
。#include <windows.h>
char buffer[1024];
if (GetModuleFileName(NULL, buffer, sizeof(buffer)) != 0) {
// buffer contains the path
} else {
// error
}