C++ – 跨平台在Windows、Linux系统上获取当前可执行程序路径
1 C++跨平台在Windows、Linux系统上获取当前可执行程序路径
跨平台获取当前可执行程序路径是C++跨平台项目中会经常使用的功能,我将这个功能简单的封装成了一个PathUtils
工具类,在该类中通过GetCurrentProgramDirectory
静态函数获取当前可执行程序路径,下面贴出了功能实现代码。
path_utils.h
#ifndef _PATH_UTILS_H_
#define _PATH_UTILS_H_
#include <string>
class PathUtils
{
public:
// 得到当前程序执行路径
static std::string GetCurrentProgramDirectory();
};
#endif // !_PATH_UTILS_H_
path_utils.cpp
#include "path_utils.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#include<Windows.h>
#elif defined(linux) || defined(__linux)
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
#endif // WINDOWS
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
static HMODULE GetSelfModuleHandle()
{
MEMORY_BASIC_INFORMATION mbi;
return (
(::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0)
? (HMODULE)mbi.AllocationBase : NULL
);
}
std::string PathUtils::GetCurrentProgramDirectory()
{
std::string strCurrentPath = "";
char curDirector[260] = { 0 };
GetModuleFileName(GetSelfModuleHandle(), curDirector, 260);
strCurrentPath = curDirector;
size_t nameStart = strCurrentPath.rfind("\\");
strCurrentPath = strCurrentPath.substr(0, nameStart + 1);
return strCurrentPath;
}
#elif defined(linux) || defined(__linux)
std::string PathUtils::GetCurrentProgramDirectory()
{
std::string strCurrentPath = "";
char szCurWorkPath[256];
memset(szCurWorkPath, '\0', 256);
int nRet = readlink("/proc/self/exe", szCurWorkPath, 256);
if (nRet > 256 || nRet < 0)
{
return strCurrentPath;
}
for (int i = nRet; i > 0; i--)
{
if (szCurWorkPath[i] == '/' || szCurWorkPath[i] == '\\')
{
szCurWorkPath[i] = '\0';
break;
}
}
strCurrentPath = szCurWorkPath;
return strCurrentPath;
}
#endif
测试代码
#include <iostream>
#include "path_utils.h"
int main()
{
std::cout << "Path : " << PathUtils::GetCurrentProgramDirectory() << std::endl;
return 0;
}
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 跨平台在Windows、Linux系统上获取当前可执行程序路径
原文链接:https://www.stubbornhuang.com/2790/
发布于:2023年09月07日 11:45:54
修改于:2023年09月07日 11:45:54
当前分类随机文章推荐
- C++ - std::unordered_map中使用结构体或者vector等复杂的数据结构作为Key值 阅读1249次,点赞0次
- C++ - 常用的C++命令行参数解析第三方库 阅读4261次,点赞3次
- C++11 - std::shared_ptr初始化的几种方式 阅读7494次,点赞2次
- C++ - 智能指针的正确使用方式 阅读822次,点赞0次
- C++ - C++实现Python numpy的矩阵维度转置算法,例如(N,H,W,C)转换为(N,C,H,W) 阅读5060次,点赞4次
- C++ - sleep睡眠函数总结 阅读1895次,点赞0次
- Windows编译Spout2以及配置Spout2开发环境 阅读228次,点赞0次
- C++ - 在某一天某个时间点定时执行任务,比如2022年9月19日晚上9点准点执行发送邮件函数 阅读813次,点赞0次
- C++ - 使用正则判断字符串是否全是中文 阅读1674次,点赞0次
- C++ - 跨平台在Windows、Linux系统上获取当前可执行程序路径 阅读61次,点赞0次
全站随机文章推荐
- 资源分享 - Mathematics for Computer Graphics , Third Edition 英文高清PDF下载 阅读1597次,点赞0次
- 资源分享 - PHP与MySQL程序设计(第3版) 中文 PDF下载 阅读2413次,点赞0次
- 资源分享 - An Introduction to Computational Fluid Dynamics - The Finite Volume Method (Second Edition)英文高清PDF下载 阅读769次,点赞0次
- Python - ModuleNotFoundError: No module named 'skimage' 阅读566次,点赞0次
- Duilib - Label控件文本换行 阅读2644次,点赞1次
- C++ - 主线程如何捕获子线程抛出的异常 阅读424次,点赞0次
- C++ - C++实现Python numpy的矩阵维度转置算法,例如(N,H,W,C)转换为(N,C,H,W) 阅读5060次,点赞4次
- WordPress - 优化Google Adsense广告js的加载速度 阅读163次,点赞0次
- Google Adsense - 使用招商银行电汇收款 阅读1467次,点赞2次
- C++ - std::map正向遍历与反向遍历的几种方式 阅读5171次,点赞3次
评论
169