Added getExecutablePath(). Still need to check if the Windows version works, but who cares?

This commit is contained in:
Patrick 2025-01-19 21:24:38 +01:00
parent 75398b89d5
commit 461d3ec694
2 changed files with 32 additions and 1 deletions

View File

@ -8,7 +8,9 @@
#include <dlfcn.h>
#include <pthread.h>
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
// TODO
#include <array>
#include <windows.h>
#include "../util/winundef.hpp"
#endif
namespace fs = std::filesystem;
@ -97,6 +99,34 @@ void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT
#endif
}
std::string getExecutablePath() MIJIN_NOEXCEPT
{
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX
return fs::canonical("/proc/self/exe").string();
#elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS
// TODO: this is completely untested
std::array<char, 1024> buffer;
if (FAILED(GetModuleFileNameA(nullptr, buffer.data(), buffer.size())))
{
std::vector<char> buffer2;
buffer2.resize(1024);
do
{
if (buffer2.size() >= 10240)
{
MIJIN_ERROR("Something is wrong.");
return "";
}
buffer2.resize(buffer2.size() + 1024);
}
while (FAILED(GetModuleFileNameA(nullptr, buffer.data(), buffer.size())));
return buffer2.data();
}
return buffer.data();
#else
#endif
}
[[nodiscard]] std::string makeLibraryFilename(std::string_view libraryName) MIJIN_NOEXCEPT
{
#if MIJIN_TARGET_OS == MIJIN_OS_LINUX

View File

@ -77,6 +77,7 @@ public:
bool closeSharedLibrary(const LibraryHandle library) MIJIN_NOEXCEPT;
[[nodiscard]] void* loadSymbolFromLibrary(const LibraryHandle library, const char* symbolName) MIJIN_NOEXCEPT;
void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT;
[[nodiscard]] std::string getExecutablePath() MIJIN_NOEXCEPT;
[[nodiscard]] std::string makeLibraryFilename(std::string_view libraryName) MIJIN_NOEXCEPT;