您当前的位置: 首页 >  操作系统

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

操作系统得到可执行路径

qianbo_insist 发布时间:2022-09-12 09:16:31 ,浏览量:0

操作系统得到可执行路径

下面这个函数是从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;
}
关注
打赏
1663161521
查看更多评论
立即登录/注册

微信扫码登录

0.0385s