Simpler way to get the module HANDLE
authorLuca Bacci <luca.bacci982@gmail.com>
Thu, 29 Dec 2022 13:51:43 +0000 (14:51 +0100)
committerØyvind "pippin" Kolås <pippin@gimp.org>
Tue, 3 Jan 2023 10:33:37 +0000 (10:33 +0000)
Also works in case of static compilation

babl/babl-util.c
babl/babl-util.h
babl/babl.c

index cab9ab1d9135e4c81e195eeb9ab3b14910aa7fdd..d446a430ba6b0e9313969f0793967fc6133f82bb 100644 (file)
@@ -148,3 +148,14 @@ _babl_file_get_contents (const char  *path,
   return 0;
 }
 
+#ifdef _WIN32
+
+extern IMAGE_DOS_HEADER __ImageBase;
+
+void *
+get_libbabl_module (void)
+{
+  return &__ImageBase;
+}
+
+#endif /* _WIN32 */
index 9caec36dc07e06aba09882d284749f39442a0569..f24101dadbbf47b7b78273f7a9ef4f07095ceaca 100644 (file)
@@ -26,4 +26,12 @@ double
 babl_rel_avg_error (const double *imgA,
                     const double *imgB,
                     long          samples);
+
+#ifdef _WIN32
+
+void *
+get_libbabl_module (void);
+
+#endif /* _WIN32 */
+
 #endif
index 95ac256694194b896f99abb1cdbdae6f59430e0b..57bb916e3267a4f0ac5700e2342d12be0b4e55d9 100644 (file)
 
 static int ref_count = 0;
 
-#ifdef _WIN32
-static HMODULE libbabl_dll = NULL;
-
-/* Minimal DllMain that just stores the handle to this DLL */
-
-/* Avoid silly "no previous prototype" gcc warning */
-BOOL WINAPI
-DllMain (HINSTANCE hinstDLL,
-         DWORD     fdwReason,
-         LPVOID    lpvReserved);
-
-BOOL WINAPI
-DllMain (HINSTANCE hinstDLL,
-         DWORD     fdwReason,
-         LPVOID    lpvReserved)
-{
-  switch (fdwReason)
-    {
-      case DLL_PROCESS_ATTACH:
-        libbabl_dll = hinstDLL;
-        break;
-    }
-
-  return TRUE;
-}
-
-#else
+#ifndef _WIN32
 #define BABL_PATH              LIBDIR BABL_DIR_SEPARATOR BABL_LIBRARY
-#endif /* _WIN32 */
+#endif
 
 /*
  * Returns a list of directories if the environment variable $BABL_PATH
@@ -77,7 +51,7 @@ babl_dir_list (void)
       wchar_t w_filename[MAX_PATH];
       DWORD nSize = sizeof (w_filename) / sizeof ((w_filename)[0]);
 
-      if (GetModuleFileNameW (libbabl_dll, w_filename, nSize) == 0)
+      if (GetModuleFileNameW (get_libbabl_module (), w_filename, nSize) == 0)
         babl_fatal ("GetModuleFilenameW failed");
 
       filename_size = WideCharToMultiByte (CP_UTF8, 0, w_filename, -1, NULL, 0,