#ifndef _PATH_UTILS_H_ #define _PATH_UTILS_H_ #include #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) #include #elif defined(linux) || defined(__linux) #include #include #include #include #include #endif // WINDOWS namespace helpers { namespace PathUtils { #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 ); } static std::string GetCurrentProgramDir() { 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) static std::string GetCurrentProgramDir() { 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; } static std::string GetCmdDir() { char cwd[PATH_MAX]; if (getcwd(cwd, sizeof(cwd)) != nullptr) { std::cout << "当前工作目录是: " << cwd << std::endl; } else { std::cerr << "错误: 无法获取当前工作目录。" << std::endl; } return cwd; } #endif } } #endif // !_PATH_UTILS_H_