From 7ab8e5ee71ec3c59d1c4e568b965ac3d9d756695 Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Thu, 29 Dec 2022 14:51:43 +0100 Subject: [PATCH] Simpler way to get the module HANDLE Also works in case of static compilation --- babl/babl-util.c | 11 +++++++++++ babl/babl-util.h | 8 ++++++++ babl/babl.c | 32 +++----------------------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/babl/babl-util.c b/babl/babl-util.c index cab9ab1..d446a43 100644 --- a/babl/babl-util.c +++ b/babl/babl-util.c @@ -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 */ diff --git a/babl/babl-util.h b/babl/babl-util.h index 9caec36..f24101d 100644 --- a/babl/babl-util.h +++ b/babl/babl-util.h @@ -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 diff --git a/babl/babl.c b/babl/babl.c index 95ac256..57bb916 100644 --- a/babl/babl.c +++ b/babl/babl.c @@ -22,35 +22,9 @@ 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, -- 2.30.2