Get path of executable

前端 未结 23 1526
清歌不尽
清歌不尽 2020-11-22 07:04

I know this question has been asked before but I still haven\'t seen a satisfactory answer, or a definitive \"no, this cannot be done\", so I\'ll ask again!

All I wa

23条回答
  •  旧巷少年郎
    2020-11-22 07:41

    This is what I ended up with

    The header file looks like this:

    #pragma once
    
    #include 
    namespace MyPaths {
    
      std::string getExecutablePath();
      std::string getExecutableDir();
      std::string mergePaths(std::string pathA, std::string pathB);
      bool checkIfFileExists (const std::string& filePath);
    
    }
    
    

    Implementation

    
    #if defined(_WIN32)
        #include 
        #include 
        #include  
    
        #define access _access_s
    #endif
    
    #ifdef __APPLE__
        #include 
        #include 
        #include 
        #include 
    #endif
    
    #ifdef __linux__
        #include 
        #include 
        #include 
    
        #if defined(__sun)
            #define PROC_SELF_EXE "/proc/self/path/a.out"
        #else
            #define PROC_SELF_EXE "/proc/self/exe"
        #endif
    
    #endif
    
    namespace MyPaths {
    
    #if defined(_WIN32)
    
    std::string getExecutablePath() {
       char rawPathName[MAX_PATH];
       GetModuleFileNameA(NULL, rawPathName, MAX_PATH);
       return std::string(rawPathName);
    }
    
    std::string getExecutableDir() {
        std::string executablePath = getExecutablePath();
        char* exePath = new char[executablePath.length()];
        strcpy(exePath, executablePath.c_str());
        PathRemoveFileSpecA(exePath);
        std::string directory = std::string(exePath);
        delete[] exePath;
        return directory;
    }
    
    std::string mergePaths(std::string pathA, std::string pathB) {
      char combined[MAX_PATH];
      PathCombineA(combined, pathA.c_str(), pathB.c_str());
      std::string mergedPath(combined);
      return mergedPath;
    }
    
    #endif
    
    #ifdef __linux__
    
    std::string getExecutablePath() {
       char rawPathName[PATH_MAX];
       realpath(PROC_SELF_EXE, rawPathName);
       return  std::string(rawPathName);
    }
    
    std::string getExecutableDir() {
        std::string executablePath = getExecutablePath();
        char *executablePathStr = new char[executablePath.length() + 1];
        strcpy(executablePathStr, executablePath.c_str());
        char* executableDir = dirname(executablePathStr);
        delete [] executablePathStr;
        return std::string(executableDir);
    }
    
    std::string mergePaths(std::string pathA, std::string pathB) {
      return pathA+"/"+pathB;
    }
    
    #endif
    
    #ifdef __APPLE__
        std::string getExecutablePath() {
            char rawPathName[PATH_MAX];
            char realPathName[PATH_MAX];
            uint32_t rawPathSize = (uint32_t)sizeof(rawPathName);
    
            if(!_NSGetExecutablePath(rawPathName, &rawPathSize)) {
                realpath(rawPathName, realPathName);
            }
            return  std::string(realPathName);
        }
    
        std::string getExecutableDir() {
            std::string executablePath = getExecutablePath();
            char *executablePathStr = new char[executablePath.length() + 1];
            strcpy(executablePathStr, executablePath.c_str());
            char* executableDir = dirname(executablePathStr);
            delete [] executablePathStr;
            return std::string(executableDir);
        }
    
        std::string mergePaths(std::string pathA, std::string pathB) {
            return pathA+"/"+pathB;
        }
    #endif
    
    
    bool checkIfFileExists (const std::string& filePath) {
       return access( filePath.c_str(), 0 ) == 0;
    }
    
    }
    
    

提交回复
热议问题