remove the need for the dll and just use the normal GetModuleFileName function

This commit is contained in:
Jeffery Myers 2022-01-14 10:58:48 -08:00
parent 80bd9d7777
commit 7ce38ed161

View File

@ -166,29 +166,13 @@
#ifndef MAX_PATH #ifndef MAX_PATH
#define MAX_PATH 1025 #define MAX_PATH 1025
#endif #endif
void *LoadLibraryA(void *lpLibFileName); unsigned int GetModuleFileNameA( void* hModule, const char* lpFilename, unsigned int nSize);
void *LoadLibraryW(void *lpLibFileName); unsigned int GetModuleFileNameW( void* hModule, const unsigned short* lpFilename, unsigned int nSize);
#ifdef UNICODE
#define LoadLibrary LoadLibraryW
#else
#define LoadLibrary LoadLibraryA
#endif // !UNICODE
void *GetProcAddress(void *hModule, void *lpProcName);
void *GetCurrentProcess(void);
bool FreeLibrary(void *hLibModule);
int WideCharToMultiByte(unsigned int cp, unsigned long flags, const unsigned short *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); int WideCharToMultiByte(unsigned int cp, unsigned long flags, const unsigned short *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
const char pathDelim = '\\';
#elif defined(__linux__) #elif defined(__linux__)
#include <unistd.h> #include <unistd.h>
const char pathDelim = '/';
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include <sys/syslimits.h> #include <sys/syslimits.h>
const char pathDelim = '/';
#endif // OSs #endif // OSs
#endif // PLATFORM_DESKTOP #endif // PLATFORM_DESKTOP
@ -3007,38 +2991,13 @@ const char *GetApplicationDirectory(void)
memset(appDir, 0, MAX_FILEPATH_LENGTH); memset(appDir, 0, MAX_FILEPATH_LENGTH);
#if defined(_WIN32) #if defined(_WIN32)
typedef unsigned long(*GetModuleFileNameFunc)(void*, void*, void*, unsigned long);
GetModuleFileNameFunc getModuleFileNameExWPtr = NULL;
void *lib = LoadLibrary(L"psapi.dll");
if (lib == NULL)
{
appDir[0] = '.';
appDir[1] = '\\';
}
else
{
#if defined (UNICODE)
getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExW");
#else
getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExA");
#endif
if (getModuleFileNameExWPtr == NULL)
{
appDir[0] = '.';
appDir[1] = '\\';
}
else
{
int len = 0; int len = 0;
#if defined (UNICODE) #if defined (UNICODE)
unsigned short widePath[MAX_PATH]; unsigned short widePath[MAX_PATH];
len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, widePath, MAX_PATH); len = GetModuleFileNameW(NULL, widePath, MAX_PATH);
len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL); len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL);
#else #else
len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, appDir, MAX_PATH); len = GetModuleFileNameA(NULL, appDir, MAX_PATH);
#endif #endif
if (len > 0) if (len > 0)
{ {
@ -3056,10 +3015,7 @@ const char *GetApplicationDirectory(void)
appDir[0] = '.'; appDir[0] = '.';
appDir[1] = '\\'; appDir[1] = '\\';
} }
}
FreeLibrary(lib);
}
#elif defined(__linux__) #elif defined(__linux__)
unsigned int size = sizeof(appDir); unsigned int size = sizeof(appDir);
ssize_t len = readlink("/proc/self/exe", appDir, size); ssize_t len = readlink("/proc/self/exe", appDir, size);