[CORE] Bug and formating fixes for GetApplicatonDir (#2285)

* Fix formating problems with GetApplicationDir.
Don't ever return an empty string

* always return a valid path even if it's ./

* remove the need for the dll and just use the normal GetModuleFileName function
This commit is contained in:
Jeffery Myers 2022-01-14 11:16:25 -08:00 committed by GitHub
parent f57727995a
commit f4dea6919a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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,36 +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] = '\\';
}
else
{
#if defined (UNICODE)
getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExW");
#else
getModuleFileNameExWPtr = (GetModuleFileNameFunc)GetProcAddress(lib, "GetModuleFileNameExA");
#endif
if (getModuleFileNameExWPtr == NULL)
{
appDir[0] = '\\';
}
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)
{ {
@ -3049,10 +3010,12 @@ const char *GetApplicationDirectory(void)
} }
} }
} }
else
{
appDir[0] = '.';
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);
@ -3068,6 +3031,11 @@ const char *GetApplicationDirectory(void)
} }
} }
} }
else
{
appDir[0] = '.';
appDir[1] = '/';
}
#elif defined(__APPLE__) #elif defined(__APPLE__)
uint32_t size = sizeof(appDir); uint32_t size = sizeof(appDir);
@ -3083,6 +3051,11 @@ const char *GetApplicationDirectory(void)
} }
} }
} }
else
{
appDir[0] = '.';
appDir[1] = '/';
}
#endif #endif
return appDir; return appDir;