操作系统得到可执行路径

Posted qianbo_insist

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了操作系统得到可执行路径相关的知识,希望对你有一定的参考价值。

操作系统得到可执行路径

下面这个函数是从bullet里面摘出来的,其他的方式还有一个就是使用whereami,不过bullet比较简单,适用于苹果,linux和windows, whereami的函数比较全面,可以自动检测系统获取,下面是代码

int getExePath(char* path, int maxPathLenInBytes)

	int numBytes = 0;

#if __APPLE__
	uint32_t bufsize = uint32_t(maxPathLenInBytes);

	if (_NSGetExecutablePath(path, &bufsize) != 0)
	
		b3Warning("Cannot find executable path\\n");
		return false;
	
	else
	
		numBytes = strlen(path);
	
#else
#ifdef _WIN32
	//https://msdn.microsoft.com/en-us/library/windows/desktop/ms683197(v=vs.85).aspx

	HMODULE hModule = GetModuleHandle(NULL);
	numBytes = GetModuleFileNameA(hModule, path, maxPathLenInBytes);

#else
	///http://stackoverflow.com/questions/933850/how-to-find-the-location-of-the-executable-in-c
	numBytes = (int)readlink("/proc/self/exe", path, maxPathLenInBytes - 1);
	if (numBytes > 0)
	
		path[numBytes] = 0;
	
	else
	
		b3Warning("Cannot find executable path\\n");
	
#endif  //_WIN32
#endif  //__APPLE__

	return numBytes;

以上是关于操作系统得到可执行路径的主要内容,如果未能解决你的问题,请参考以下文章

QT如何修改编译后产生的可执行文件的路径》

得到本地应用程序的EXE的路径

从 C++ 中的可执行路径(或从 hWnd,或从 pid)获取程序名称

Symfony2 Assetic:无法解析节点可执行文件的路径

r星找不到游戏可执行文件的路径

如何解决错误“可执行路径不是绝对的,忽略:”