[gtk/gtk-3-24: 1/4] GdkWin32: Retrieve module HINSTANCE in static builds




commit d5ce63508fe93568d9b967ced632a522b7b84479
Author: Luca Bacci <luca bacci982 gmail com>
Date:   Tue Mar 1 09:25:28 2022 +0100

    GdkWin32: Retrieve module HINSTANCE in static builds

 gdk/win32/gdkmain-win32.c | 11 +++++++++++
 gtk/gtkwin32.c            | 46 ++++++++++++++++++++++++++++++++++++----------
 2 files changed, 47 insertions(+), 10 deletions(-)
---
diff --git a/gdk/win32/gdkmain-win32.c b/gdk/win32/gdkmain-win32.c
index 043341a255..f9e18f42ff 100644
--- a/gdk/win32/gdkmain-win32.c
+++ b/gdk/win32/gdkmain-win32.c
@@ -69,6 +69,17 @@ _gdk_win32_windowing_init (void)
   if (gdk_synchronize)
     GdiSetBatchLimit (1);
 
+#ifndef DLL_EXPORT
+  if (!GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
+                           GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+                           (LPCWSTR)_gdk_win32_windowing_init,
+                           &_gdk_dll_hinstance))
+    {
+      g_error ("GetModuleHandleExW failed with error code %u\n",
+               (unsigned) GetLastError ());
+    }
+#endif
+
   _gdk_app_hmodule = GetModuleHandle (NULL);
   _gdk_display_hdc = CreateDC ("DISPLAY", NULL, NULL, NULL);
   _gdk_input_locale = GetKeyboardLayout (0);
diff --git a/gtk/gtkwin32.c b/gtk/gtkwin32.c
index a086ad61ac..dfe068d215 100644
--- a/gtk/gtkwin32.c
+++ b/gtk/gtkwin32.c
@@ -38,10 +38,9 @@
  */
 #define EMPIRIC_MANIFEST_RESOURCE_INDEX 2
 
-
+#ifdef DLL_EXPORT
 static HMODULE gtk_dll;
 
-#ifdef DLL_EXPORT
 BOOL WINAPI
 DllMain (HINSTANCE hinstDLL,
          DWORD     fdwReason,
@@ -56,6 +55,32 @@ DllMain (HINSTANCE hinstDLL,
 
   return TRUE;
 }
+
+static HMODULE
+get_gtk_dll (void)
+{
+  return gtk_dll;
+}
+#else
+static HMODULE
+get_gtk_dll (void)
+{
+  static HMODULE gtk_dll = NULL;
+
+  if G_UNLIKELY (gtk_dll == NULL)
+    {
+      if (!GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
+                               GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+                               (LPCWSTR) get_gtk_dll,
+                               &gtk_dll))
+        {
+          g_error ("GetModuleHandleExW failed with error code %u\n",
+                   (unsigned) GetLastError ());
+        }
+    }
+
+  return gtk_dll;
+}
 #endif
 
 static BOOL CALLBACK
@@ -104,7 +129,8 @@ _gtk_load_dll_with_libgtk3_manifest (const gchar *dll_name)
   DWORD error_code;
 
   resource_name = NULL;
-  EnumResourceNames (gtk_dll, RT_MANIFEST, find_first_manifest,
+  EnumResourceNames (get_gtk_dll (), RT_MANIFEST,
+                     find_first_manifest,
                      (LONG_PTR) &resource_name);
 
   if (resource_name == NULL)
@@ -115,7 +141,7 @@ _gtk_load_dll_with_libgtk3_manifest (const gchar *dll_name)
   activation_ctx_descriptor.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID |
                                       ACTCTX_FLAG_HMODULE_VALID |
                                       ACTCTX_FLAG_SET_PROCESS_DEFAULT;
-  activation_ctx_descriptor.hModule = gtk_dll;
+  activation_ctx_descriptor.hModule = get_gtk_dll ();
   activation_ctx_descriptor.lpResourceName = resource_name;
   activation_ctx_handle = CreateActCtx (&activation_ctx_descriptor);
   error_code = GetLastError ();
@@ -123,7 +149,7 @@ _gtk_load_dll_with_libgtk3_manifest (const gchar *dll_name)
   if (activation_ctx_handle == INVALID_HANDLE_VALUE &&
       error_code != ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET)
     g_warning ("Failed to CreateActCtx for module %p, resource %p: %lu",
-               gtk_dll, resource_name, GetLastError ());
+               get_gtk_dll (), resource_name, GetLastError ());
   else if (error_code != ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET)
     {
       activation_cookie = 0;
@@ -150,7 +176,7 @@ _gtk_get_libdir (void)
   static char *gtk_libdir = NULL;
   if (gtk_libdir == NULL)
     {
-      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
+      gchar *root = g_win32_get_package_installation_directory_of_module (get_gtk_dll ());
       gchar *slash = strrchr (root, '\\');
       if (slash != NULL &&
           g_ascii_strcasecmp (slash + 1, ".libs") == 0)
@@ -181,7 +207,7 @@ _gtk_get_localedir (void)
       while (*--p != '/')
         ;
 
-      root = g_win32_get_package_installation_directory_of_module (gtk_dll);
+      root = g_win32_get_package_installation_directory_of_module (get_gtk_dll ());
       temp = g_build_filename (root, p, NULL);
       g_free (root);
 
@@ -200,7 +226,7 @@ _gtk_get_datadir (void)
   static char *gtk_datadir = NULL;
   if (gtk_datadir == NULL)
     {
-      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
+      gchar *root = g_win32_get_package_installation_directory_of_module (get_gtk_dll ());
       gtk_datadir = g_build_filename (root, "share", NULL);
       g_free (root);
     }
@@ -214,7 +240,7 @@ _gtk_get_sysconfdir (void)
   static char *gtk_sysconfdir = NULL;
   if (gtk_sysconfdir == NULL)
     {
-      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
+      gchar *root = g_win32_get_package_installation_directory_of_module (get_gtk_dll ());
       gtk_sysconfdir = g_build_filename (root, "etc", NULL);
       g_free (root);
     }
@@ -227,7 +253,7 @@ _gtk_get_data_prefix (void)
 {
   static char *gtk_data_prefix = NULL;
   if (gtk_data_prefix == NULL)
-    gtk_data_prefix = g_win32_get_package_installation_directory_of_module (gtk_dll);
+    gtk_data_prefix = g_win32_get_package_installation_directory_of_module (get_gtk_dll ());
 
   return gtk_data_prefix;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]