From 7ce38ed1615f0f1e7e0977f50976c979bf09e7c6 Mon Sep 17 00:00:00 2001 From: Jeffery Myers Date: Fri, 14 Jan 2022 10:58:48 -0800 Subject: [PATCH] remove the need for the dll and just use the normal GetModuleFileName function --- src/rcore.c | 88 ++++++++++++++--------------------------------------- 1 file changed, 22 insertions(+), 66 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index 70ebdbaac..8e93709ba 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -166,29 +166,13 @@ #ifndef MAX_PATH #define MAX_PATH 1025 #endif - void *LoadLibraryA(void *lpLibFileName); - void *LoadLibraryW(void *lpLibFileName); - - #ifdef UNICODE - #define LoadLibrary LoadLibraryW - #else - #define LoadLibrary LoadLibraryA - #endif // !UNICODE - - void *GetProcAddress(void *hModule, void *lpProcName); - - void *GetCurrentProcess(void); - bool FreeLibrary(void *hLibModule); - + unsigned int GetModuleFileNameA( void* hModule, const char* lpFilename, unsigned int nSize); + unsigned int GetModuleFileNameW( void* hModule, const unsigned short* lpFilename, unsigned int nSize); 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__) #include - const char pathDelim = '/'; #elif defined(__APPLE__) #include - const char pathDelim = '/'; #endif // OSs #endif // PLATFORM_DESKTOP @@ -3007,59 +2991,31 @@ const char *GetApplicationDirectory(void) memset(appDir, 0, MAX_FILEPATH_LENGTH); #if defined(_WIN32) - typedef unsigned long(*GetModuleFileNameFunc)(void*, void*, void*, unsigned long); - - GetModuleFileNameFunc getModuleFileNameExWPtr = NULL; - void *lib = LoadLibrary(L"psapi.dll"); - - if (lib == NULL) + int len = 0; +#if defined (UNICODE) + unsigned short widePath[MAX_PATH]; + len = GetModuleFileNameW(NULL, widePath, MAX_PATH); + len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL); +#else + len = GetModuleFileNameA(NULL, appDir, MAX_PATH); +#endif + if (len > 0) + { + for (int i = len; i >= 0; --i) + { + if (appDir[i] == '\\') + { + appDir[i + 1] = '\0'; + i = -1; + } + } + } + else { 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; -#if defined (UNICODE) - unsigned short widePath[MAX_PATH]; - len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, widePath, MAX_PATH); - len = WideCharToMultiByte(0, 0, widePath, len, appDir, MAX_PATH, NULL, NULL); -#else - len = getModuleFileNameExWPtr(GetCurrentProcess(), NULL, appDir, MAX_PATH); -#endif - if (len > 0) - { - for (int i = len; i >= 0; --i) - { - if (appDir[i] == '\\') - { - appDir[i + 1] = '\0'; - i = -1; - } - } - } - else - { - appDir[0] = '.'; - appDir[1] = '\\'; - } - } - - FreeLibrary(lib); - } #elif defined(__linux__) unsigned int size = sizeof(appDir); ssize_t len = readlink("/proc/self/exe", appDir, size);