C++ – Windows和Linux系统下获取当前可执行程序的绝对路径
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – Windows和Linux系统下获取当前可执行程序的绝对路径
原文链接:https://www.stubbornhuang.com/2051/
发布于:2022年03月22日 16:52:27
修改于:2022年06月01日 13:47:44

1 C++获取Windows和Linux系统当前可执行程序的绝对路径
在程序中获取当前程序在系统中的绝对路径是频繁使用的功能,本文总结了如何在Windows和Linux系统获取当前可执行程序绝对路径的方式,并封装成可跨平台编译的工具类PathUtils。
1.1 在Windows系统上获取当前可执行程序的绝对路径
通过调用Windows API GetModuleFileName函数获取当前可执行程序的绝对路径。
#include<Windows.h>
static HMODULE GetSelfModuleHandle()
{
MEMORY_BASIC_INFORMATION mbi;
return (
(::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0)
? (HMODULE)mbi.AllocationBase : NULL
);
}
std::string PathUtils::GetCurrentExeDirectory()
{
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;
}
1.2 在Linux系统上获取当前可执行程序的绝对路径
通过readlink函数获取当前可执行程序的绝对路径。
#include <string.h>
#include <unistd.h>
#include <dlfcn.h>
std::string PathUtils::GetCurrentExeDirectory()
{
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;
}
1.3 PathUtils工具类
PathUtils.h
#ifndef PATH_UTILS_H
#define PATH_UTILS_H
#include <string>
class PathUtils
{
public:
// 得到当前的Exe的路径
static std::string GetCurrentExeDirectory();
};
#endif // !PATH_UTILS_H
PathUtils.cpp
#include "PathUtils.h"
#ifdef 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
#ifdef 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::GetCurrentExeDirectory()
{
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::GetCurrentExeDirectory()
{
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
在linux系统上编译时需要链接dl选项,
target_link_libraries(main dl rt)
当前分类随机文章推荐
- C++11 - std::function简要介绍以及可包装函数的几种形式总结 阅读3044次,点赞0次
- C++ - 获取std::vector中的最小值、最大值以及对应的索引 阅读15次,点赞0次
- C++11/std::thread - 线程的基本用法 阅读3174次,点赞0次
- C++ - 判断两个字符串是否相等方法总结 阅读285次,点赞0次
- C++ - GBK编码下的全角字符转半角字符 阅读1481次,点赞0次
- C++ - 只有在Debug模式下才使用std::cout输出调试日志,Release发布版本不输出调试日志 阅读4148次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与short的相互转换 阅读1219次,点赞0次
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读2964次,点赞0次
- C++STL容器 - std::map查找元素与判断键值是否存在方法总结 count,find,contains,equal_range,lower_bound,upper_bound 阅读922次,点赞0次
- C++ - queue存储动态指针时正确释放内存 阅读5223次,点赞2次
全站随机文章推荐
- 工具软件 - 解决从Onenote复制文字到QQ变成图片的问题,2023年最新解决方案 阅读79次,点赞0次
- 深度学习 - 以一个极简单的中英文翻译Demo彻底理解Transformer 阅读604次,点赞0次
- 客户端开发GUI框架对比与技术选型总结 阅读2977次,点赞0次
- 简单粗暴:使用pycharm安装对应的Python版本第三方包 阅读3536次,点赞0次
- 书籍翻译 – Fundamentals of Computer Graphics, Fourth Edition,第12章 Data Structures for Graphics中文翻译 阅读1112次,点赞3次
- 资源分享 – OpenGL SuperBible – Comprehensive Tutorial and Reference (Seventh Edition) OpenGL蓝宝书第7版英文高清PDF下载 阅读2329次,点赞2次
- 资源分享 - Game AI Pro 360 - Guide to Architecture 英文高清PDF下载 阅读2112次,点赞0次
- 资源分享 - Virtual Clothing - Theory and Practice 英文PDF下载 阅读991次,点赞0次
- OpenCV - 创建新图像以及遍历图片像素值和设置像素值 阅读2996次,点赞0次
- C++ - 使用C++标准库过滤Windows文件名中的非法字符 阅读4384次,点赞1次
评论
167