【发布时间】:2012-09-17 22:46:16
【问题描述】:
我有一个包含许多不同操作系统的二进制文件的程序
我需要获取二进制文件的位置(想想自动更新器)
如何在“你的”操作系统中这样做?
(把它想象成一个挑战,有点:))
编辑注意:该程序可以是可移植的(运行闪存驱动器等)或安装(例如,最初为 .deb 格式)
EDIT2:这是我已经拥有的:
/**
* Get the location of the executable
* @return the location of the executable, as a string.
*/
const char *GetExecutableLocation()
{
const char *path;
char buf[1024];
#if defined (WIN32) || defined (WIN64)
GetModuleFileName(path, &size);
#elif defined (__APPLE__)
_NSGetExecutablePath(path, &size);
#elif defined(UNIX)
if (readlink("/proc/self/exe", buf, sizeof(buf)) == -1) path = buf;
#elif defined(__FreeBSD__)
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
sysctl(mib, 4, buf, sizeof(buf), NULL, 0);
path = buf;
#elif defined(SUNOS)
path = getexecname();
#endif
return path;
}
(请注意,我只测试了unix部分,我不知道其他任何内容)
【问题讨论】:
-
可能是
argv[0]之类的?
标签: c++ path executable