C++ Code Sample: Get Path of Module
Lấy đường dẫn của file thực thi exe sau khi build. (C++ get path of executable)
Khi thực hiện code một project lập trình c++ trên visual studio.
Bạn build project thành công và file exe được tạo ra, có thể là mode debug/release với 2 nền tảng x86, x64
Vậy làm thế nào để chúng ta có thể lấy đường dẫn chứa file exe được build ra ở trên.
Hãy tham khảo mẫu code dưới đây.
std::string GetModulePathExe()
{
std::string exePath;
TCHAR chPath[MAX_PATH];
HMODULE hModule = GetModuleHandle(NULL);
if (hModule != NULL)
{
GetModuleFileName(hModule, chPath, (sizeof(chPath)));
std::wstring sTemp(&chPath[0]); //convert to wstring
exePath = std::string(sTemp.begin(), sTemp.end());
exePath = exePath.substr(0, exePath.find_last_of("\\/"));
}
return exePath;
}
END.
