1 C++获取WindowsLinux系统当前可执行程序的绝对路径

在程序中获取当前程序在系统中的绝对路径是频繁使用的功能,本文总结了如何在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)