From deb9c6e11116c458a39f0bab475c5ea7170a9043 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Tue, 21 Jun 2016 13:31:12 +0300 Subject: [PATCH] Global SO_PATH variable removed. --- templates/library_constructor.c.in | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/templates/library_constructor.c.in b/templates/library_constructor.c.in index 6c9e7c5..96868b1 100644 --- a/templates/library_constructor.c.in +++ b/templates/library_constructor.c.in @@ -12,10 +12,25 @@ #include "gettext-config.h" -char SO_PATH[FILENAME_MAX]; +#if defined(_WIN32) || defined(_WIN64) + HINSTANCE hCurrentModuleInstance; +#endif @LC_GLOB_VARS@ +gboolean get_library_path (gchar *so_path, void *addr) +{ + gboolean ret; +#if defined(_WIN32) || defined(_WIN64) + ret = 0 != GetModuleFileName (hCurrentModuleInstance, so_path, FILENAME_MAX); +#elif defined(linux) || defined(UNIX) || defined(__unix__) + Dl_info dl_info; + ret = 0 != dladdr(addr, &dl_info); + strcpy (so_path, dl_info.dli_fname); +#endif + return ret; +} + #if defined(_WIN32) || defined(_WIN64) BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) #elif defined(linux) || defined(UNIX) || defined(__unix__) @@ -25,19 +40,18 @@ void __attribute__ ((constructor)) load_library (void) gchar *soDir, *localePath; @LC_VARS@ + gchar *so_path = g_new (gchar, FILENAME_MAX); #if defined(_WIN32) || defined(_WIN64) - GetModuleFileName (hInstance, SO_PATH, FILENAME_MAX); - + get_library_path (so_path, NULL); + hCurrentModuleInstance = hInstance; @LC_WIN32_CODE@ #elif defined(linux) || defined(UNIX) || defined(__unix__) - Dl_info dl_info; - dladdr(load_library, &dl_info); - strcpy (SO_PATH, dl_info.dli_fname); - + get_library_path (so_path, load_library); @LC_UNIX_CODE@ #endif - soDir = g_path_get_dirname (SO_PATH); + soDir = g_path_get_dirname (so_path); + g_free (so_path); localePath = g_build_filename (soDir, "@LC_RELATIVE_PREFIX@/share/locale", NULL); g_free (soDir); bindtextdomain (GETTEXT_PACKAGE, localePath);