Do not keep HINSTANCE variables around
authorLuca Bacci <luca.bacci982@gmail.com>
Thu, 23 Mar 2023 15:27:17 +0000 (16:27 +0100)
committerLuca Bacci <luca.bacci982@gmail.com>
Tue, 30 May 2023 17:31:54 +0000 (19:31 +0200)
Use &__ImageBase for the GTK DLL and GetModuleHandle (NULL)
for the application module. Then remove DllMain as it's not
necessary anymore.

References:

 [1] Accessing the current module's HINSTANCE from a static library:
     https://devblogs.microsoft.com/oldnewthing/20041025-00/?p=37483

13 files changed:
gdk/win32/gdkclipdrop-win32.c
gdk/win32/gdkcursor-win32.c
gdk/win32/gdkdisplay-win32.c
gdk/win32/gdkevents-win32.c
gdk/win32/gdkglcontext-win32-wgl.c
gdk/win32/gdkglobals-win32.c
gdk/win32/gdkinput-winpointer.c
gdk/win32/gdkmain-win32.c
gdk/win32/gdkprivate-win32.h
gdk/win32/gdksurface-win32.c
gdk/win32/gdkvulkancontext-win32.c
gtk/gtkprintoperation-win32.c
gtk/gtkwin32.c

index 51c19a66cc26fe060f5942b9c7079a4f305c2be0..e9150a6a060ebfabe687f0308324495dde0fa697 100644 (file)
@@ -1436,7 +1436,7 @@ register_clipboard_notification ()
 
   wclass.lpszClassName = "GdkClipboardNotification";
   wclass.lpfnWndProc = _clipboard_window_procedure;
-  wclass.hInstance = _gdk_dll_hinstance;
+  wclass.hInstance = this_module ();
   wclass.cbWndExtra = sizeof (GdkWin32ClipboardThread *);
 
   klass = RegisterClass (&wclass);
@@ -1446,7 +1446,7 @@ register_clipboard_notification ()
   clipboard_thread_data->clipboard_window = CreateWindow (MAKEINTRESOURCE (klass),
                                                 NULL, WS_POPUP,
                                                 0, 0, 0, 0, NULL, NULL,
-                                                _gdk_dll_hinstance, NULL);
+                                                this_module (), NULL);
 
   if (clipboard_thread_data->clipboard_window == NULL)
     goto failed;
@@ -1471,7 +1471,7 @@ register_clipboard_notification ()
 
 failed:
   g_critical ("Failed to install clipboard viewer");
-  UnregisterClass (MAKEINTRESOURCE (klass), _gdk_dll_hinstance);
+  UnregisterClass (MAKEINTRESOURCE (klass), this_module ());
   return FALSE;
 }
 
index 5d388d04b229311311824d03292c4d4a584fb1e7..ee2ce8ba772f15e93fa773c7ff728d03fd649af4 100644 (file)
@@ -417,13 +417,12 @@ hcursor_from_x_cursor (int          i,
 #undef SET_BIT
 #undef RESET_BIT
 
-      rv = CreateCursor (_gdk_app_hmodule, cursors[i].hotx, cursors[i].hoty,
+      rv = CreateCursor (NULL, cursors[i].hotx, cursors[i].hoty,
                         w, h, and_plane, xor_plane);
     }
   else
     {
-      rv = CreateCursor (_gdk_app_hmodule, 0, 0,
-                        w, h, and_plane, xor_plane);
+      rv = CreateCursor (NULL, 0, 0, w, h, and_plane, xor_plane);
     }
 
   if (rv == NULL)
@@ -466,7 +465,7 @@ win32_cursor_create_win32hcursor (GdkWin32Display *display,
         break;
       case GDK_WIN32_CURSOR_LOAD_FROM_RESOURCE_THIS:
         result = gdk_win32_hcursor_new (display,
-                                        LoadImageA (_gdk_app_hmodule,
+                                        LoadImageA (GetModuleHandle (NULL),
                                                     (const char *) cursor->resource_name,
                                                     IMAGE_CURSOR,
                                                     cursor->width,
@@ -476,7 +475,7 @@ win32_cursor_create_win32hcursor (GdkWin32Display *display,
         break;
       case GDK_WIN32_CURSOR_LOAD_FROM_RESOURCE_GTK:
         result = gdk_win32_hcursor_new (display,
-                                        LoadImageA (_gdk_dll_hinstance,
+                                        LoadImageA (this_module (),
                                                     (const char *) cursor->resource_name,
                                                     IMAGE_CURSOR,
                                                     cursor->width,
@@ -858,9 +857,7 @@ create_blank_win32hcursor (GdkWin32Display *display)
   xor_plane = g_malloc ((w/8) * h);
   memset (xor_plane, 0, (w/8) * h);
 
-  rv = CreateCursor (_gdk_app_hmodule, 0, 0,
-                     w, h, and_plane, xor_plane);
-
+  rv = CreateCursor (NULL, 0, 0, w, h, and_plane, xor_plane);
   if (rv == NULL)
     WIN32_API_FAILED ("CreateCursor");
 
@@ -871,6 +868,7 @@ static GdkWin32HCursor *
 gdk_win32hcursor_create_for_name (GdkWin32Display  *display,
                                   const char *name)
 {
+  const HINSTANCE hinstance = GetModuleHandle (NULL);
   GdkWin32HCursor *win32hcursor = NULL;
 
   /* Blank cursor case */
@@ -885,7 +883,7 @@ gdk_win32hcursor_create_for_name (GdkWin32Display  *display,
   /* Allow to load named cursor resources linked into the executable.
    * Cursors obtained with LoadCursor() cannot be destroyed.
    */
-  return gdk_win32_hcursor_new (display, LoadCursor (_gdk_app_hmodule, name), FALSE);
+  return gdk_win32_hcursor_new (display, LoadCursor (hinstance, name), FALSE);
 }
 
 static HICON
index 843fe2c6533c2ca67f39f2381abfe9ce4f2a43c4..441f7fc914042c65f7df30c817ca39613035aec2 100644 (file)
@@ -481,7 +481,7 @@ register_display_change_notification (GdkDisplay *display)
 
   wclass.lpszClassName = "GdkDisplayChange";
   wclass.lpfnWndProc = display_change_window_procedure;
-  wclass.hInstance = _gdk_app_hmodule;
+  wclass.hInstance = this_module ();
   wclass.style = CS_OWNDC;
 
   klass = RegisterClass (&wclass);
@@ -490,10 +490,10 @@ register_display_change_notification (GdkDisplay *display)
       display_win32->hwnd = CreateWindow (MAKEINTRESOURCE (klass),
                                           NULL, WS_POPUP,
                                           0, 0, 0, 0, NULL, NULL,
-                                          _gdk_app_hmodule, NULL);
+                                          this_module (), NULL);
       if (!display_win32->hwnd)
         {
-          UnregisterClass (MAKEINTRESOURCE (klass), _gdk_app_hmodule);
+          UnregisterClass (MAKEINTRESOURCE (klass), this_module ());
         }
     }
 }
index 10803a48455ddd548d2f1c10e14f6fc477a3e9ad..7be0835d1efbb9aaa6d85fcf23c0b91b2addbd5b 100644 (file)
@@ -413,8 +413,7 @@ set_up_low_level_keyboard_hook (void)
 
   hook_handle = SetWindowsHookEx (WH_KEYBOARD_LL,
                                   (HOOKPROC) low_level_keyboard_proc,
-                                  _gdk_dll_hinstance,
-                                  0);
+                                  this_module (), 0);
 
   if (hook_handle != NULL)
     keyboard_hook = hook_handle;
index fdc8e50e6e5c994e8dbb5ae66b4a8a53339daa8d..055c8a6b939d735db329ace81121273312fbc022 100644 (file)
@@ -261,7 +261,7 @@ create_dummy_gl_window (void)
 
   wclass.lpszClassName = "GdkGLDummyWindow";
   wclass.lpfnWndProc = DefWindowProc;
-  wclass.hInstance = _gdk_app_hmodule;
+  wclass.hInstance = this_module ();
   wclass.style = CS_OWNDC;
 
   klass = RegisterClass (&wclass);
@@ -270,10 +270,10 @@ create_dummy_gl_window (void)
       hwnd = CreateWindow (MAKEINTRESOURCE (klass),
                            NULL, WS_POPUP,
                            0, 0, 0, 0, NULL, NULL,
-                           _gdk_app_hmodule, NULL);
+                           this_module (), NULL);
       if (!hwnd)
         {
-          UnregisterClass (MAKEINTRESOURCE (klass), _gdk_app_hmodule);
+          UnregisterClass (MAKEINTRESOURCE (klass), this_module ());
         }
     }
 
index 07a6e4aa07e502b899688bd41c33e4e6e24249ca..63b6118ef54b29e6e33268253c5cc6a9d22ea65a 100644 (file)
@@ -31,8 +31,6 @@ GdkDisplay     *_gdk_display = NULL;
 GdkDeviceManagerWin32 *_gdk_device_manager = NULL;
 
 HDC              _gdk_display_hdc;
-HINSTANCE        _gdk_dll_hinstance;
-HINSTANCE        _gdk_app_hmodule;
 
 int              _gdk_input_ignore_core;
 
index 3c651677031ad419a3cd6077b560531d2e9a6c39..078274345340bb91713123e54be3122cb1b33663 100644 (file)
@@ -1003,7 +1003,7 @@ winpointer_notif_window_create (void)
   wndclassex.cbSize = sizeof (wndclassex);
   wndclassex.lpszClassName = L"GdkWin32WinpointerNotificationsWindowClass";
   wndclassex.lpfnWndProc = winpointer_notifications_window_procedure;
-  wndclassex.hInstance = _gdk_dll_hinstance;
+  wndclassex.hInstance = this_module ();
 
   if ((notifications_window_class = RegisterClassExW (&wndclassex)) == 0)
     {
@@ -1018,7 +1018,7 @@ winpointer_notif_window_create (void)
                                                        0, 0, 0, 0,
                                                        HWND_MESSAGE,
                                                        NULL,
-                                                       _gdk_dll_hinstance,
+                                                       this_module (),
                                                        NULL)))
     {
       WIN32_API_FAILED ("CreateWindowExW");
index d2d06d694ac4dce684efc8d91c77623f2870b5f3..bcc1d21ad429fac9dacaff90950fd1f2b573c489 100644 (file)
@@ -66,7 +66,6 @@ _gdk_win32_surfaceing_init (void)
   if (gdk_synchronize)
     GdiSetBatchLimit (1);
 
-  _gdk_app_hmodule = GetModuleHandle (NULL);
   _gdk_display_hdc = CreateDC ("DISPLAY", NULL, NULL, NULL);
   _gdk_input_locale = GetKeyboardLayout (0);
   _gdk_win32_keymap_set_active_layout (win32_keymap, _gdk_input_locale);
index 918e961aa0cfa8057a8acc945042626547abe943..f4d9b4812affc166ee112ba5989f8999248e5228 100644 (file)
@@ -285,8 +285,6 @@ extern GdkDisplay       *_gdk_display;
 extern GdkDeviceManagerWin32 *_gdk_device_manager;
 
 extern HDC               _gdk_display_hdc;
-extern HINSTANCE         _gdk_dll_hinstance;
-extern HINSTANCE         _gdk_app_hmodule;
 
 extern int               _gdk_input_ignore_core;
 
@@ -454,3 +452,11 @@ void          gdk_win32_set_modal_dialog_libgtk_only (HWND window);
 
 gpointer      gdk_win32_handle_table_lookup_       (HWND handle);
 
+extern IMAGE_DOS_HEADER __ImageBase;
+
+static inline HMODULE
+this_module (void)
+{
+  return (HMODULE) &__ImageBase;
+}
+
index df4cf37f9ab3ef9de486911890cce443c2b5c042..9e2564f85387d3bedeb0f3ab80b9248bce41b67e 100644 (file)
@@ -339,7 +339,7 @@ RegisterGdkClass (GType wtype)
   wcl.lpfnWndProc = _gdk_win32_surface_procedure;
   wcl.cbClsExtra = 0;
   wcl.cbWndExtra = 0;
-  wcl.hInstance = _gdk_dll_hinstance;
+  wcl.hInstance = this_module ();
   wcl.hIcon = 0;
   wcl.hIconSm = 0;
 
@@ -356,7 +356,7 @@ RegisterGdkClass (GType wtype)
           if (0 == hAppIcon && 0 == hAppIconSm)
             {
               // fallback : load icon from GTK DLL
-              if (0 != GetModuleFileName (_gdk_dll_hinstance, sLoc, MAX_PATH))
+              if (0 != GetModuleFileName (this_module (), sLoc, MAX_PATH))
                {
                  ExtractIconEx (sLoc, 0, &hAppIcon, &hAppIconSm, 1);
                }
@@ -493,7 +493,7 @@ gdk_win32_surface_constructed (GObject *object)
                                   CW_USEDEFAULT, CW_USEDEFAULT,
                                   owner,
                                   NULL,
-                                  _gdk_dll_hinstance,
+                                  this_module (),
                                   surface);
   if (impl->handle == NULL)
     {
@@ -2404,7 +2404,7 @@ RegisterGdkDumbClass ()
   wcl.lpfnWndProc = DefWindowProcW;
   wcl.cbClsExtra = 0;
   wcl.cbWndExtra = 0;
-  wcl.hInstance = _gdk_dll_hinstance;
+  wcl.hInstance = this_module ();
   wcl.hIcon = 0;
   wcl.hIconSm = 0;
   wcl.lpszMenuName = NULL;
@@ -2445,7 +2445,7 @@ ensure_snap_indicator_exists (GdkW32DragMoveResizeContext *context)
                                 0, 0,
                                 NULL,
                                 NULL,
-                                _gdk_dll_hinstance,
+                                this_module (),
                                 NULL);
 
       context->shape_indicator = handle;
index 894bbfa54890fac7aa868d764f93cf39fde50dbd..e31bc3839fefba28626c6b8d031a0be7231c0c3c 100644 (file)
@@ -30,8 +30,6 @@
 #include "gdkprivate-win32.h"
 #include "gdkwin32misc.h"
 
-extern HINSTANCE        _gdk_dll_hinstance;
-
 G_DEFINE_TYPE (GdkWin32VulkanContext, gdk_win32_vulkan_context, GDK_TYPE_VULKAN_CONTEXT)
 
 static VkResult
@@ -47,7 +45,7 @@ gdk_win32_vulkan_context_create_surface (GdkVulkanContext *context,
   info.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
   info.pNext = NULL;
   info.flags = 0;
-  info.hinstance = _gdk_dll_hinstance;
+  info.hinstance = this_module ();
   info.hwnd = GDK_SURFACE_HWND (window);
 
   /* This is necessary so that Vulkan sees the Window.
index 136f12270ceb1a2b2403dac10cdf893691822669..15d68abff44033fd56813d3ede0c0fb838131396 100644 (file)
@@ -1494,7 +1494,7 @@ create_application_page (GtkPrintOperation *op)
   memset (&page, 0, sizeof (page));
   page.dwSize = sizeof (page);
   page.dwFlags = PSP_DLGINDIRECT | PSP_USETITLE | PSP_PREMATURE;
-  page.hInstance = GetModuleHandle (NULL);
+  page.hInstance = NULL;
   page.pResource = template;
   
   tab_label = op->priv->custom_tab_label;
index a4f61030dd31f8bc6338dec96ced4ffbb635475a..2144279e03a7bd025f9acb4322020ea1380c085b 100644 (file)
 #include <commctrl.h>
 #undef STRICT
 
+extern IMAGE_DOS_HEADER __ImageBase;
+
+static inline HMODULE
+this_module ()
+{
+  return (HMODULE) &__ImageBase;
+}
+
 /* In practice, resulting DLL will have manifest resource under index 2.
  * Fall back to that value if we can't find resource index programmatically.
  */
 #define EMPIRIC_MANIFEST_RESOURCE_INDEX 2
 
-
-static HMODULE   gtk_dll;
-extern HINSTANCE _gdk_dll_hinstance;
-
-BOOL WINAPI
-DllMain (HINSTANCE hinstDLL,
-         DWORD     fdwReason,
-         LPVOID    lpvReserved);
-
-BOOL WINAPI
-DllMain (HINSTANCE hinstDLL,
-         DWORD     fdwReason,
-         LPVOID    lpvReserved)
-{
-  switch (fdwReason)
-    {
-    case DLL_PROCESS_ATTACH:
-      gtk_dll = (HMODULE) hinstDLL;
-      _gdk_dll_hinstance = hinstDLL;
-      break;
-    default:
-      break;
-    }
-
-  return TRUE;
-}
-
 static BOOL CALLBACK
 find_first_manifest (HMODULE  module_handle,
                      LPCSTR   resource_type,
@@ -111,7 +92,7 @@ _gtk_load_dll_with_libgtk3_manifest (const char *dll_name)
   DWORD error_code;
 
   resource_name = NULL;
-  EnumResourceNames (gtk_dll, RT_MANIFEST, find_first_manifest,
+  EnumResourceNames (this_module (), RT_MANIFEST, find_first_manifest,
                      (LONG_PTR) &resource_name);
 
   if (resource_name == NULL)
@@ -122,7 +103,7 @@ _gtk_load_dll_with_libgtk3_manifest (const char *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 = this_module ();
   activation_ctx_descriptor.lpResourceName = resource_name;
   activation_ctx_handle = CreateActCtx (&activation_ctx_descriptor);
   error_code = GetLastError ();
@@ -130,7 +111,7 @@ _gtk_load_dll_with_libgtk3_manifest (const char *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 ());
+               this_module (), resource_name, GetLastError ());
   else if (error_code != ERROR_SXS_PROCESS_DEFAULT_ALREADY_SET)
     {
       activation_cookie = 0;
@@ -157,7 +138,7 @@ _gtk_get_libdir (void)
   static char *gtk_libdir = NULL;
   if (gtk_libdir == NULL)
     {
-      char *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
+      char *root = g_win32_get_package_installation_directory_of_module (this_module ());
       char *slash = strrchr (root, '\\');
       if (slash != NULL &&
           g_ascii_strcasecmp (slash + 1, ".libs") == 0)
@@ -188,7 +169,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 (this_module ());
       temp = g_build_filename (root, p, NULL);
       g_free (root);
 
@@ -207,7 +188,7 @@ _gtk_get_datadir (void)
   static char *gtk_datadir = NULL;
   if (gtk_datadir == NULL)
     {
-      char *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
+      char *root = g_win32_get_package_installation_directory_of_module (this_module ());
       gtk_datadir = g_build_filename (root, "share", NULL);
       g_free (root);
     }
@@ -221,7 +202,7 @@ _gtk_get_sysconfdir (void)
   static char *gtk_sysconfdir = NULL;
   if (gtk_sysconfdir == NULL)
     {
-      char *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
+      char *root = g_win32_get_package_installation_directory_of_module (this_module ());
       gtk_sysconfdir = g_build_filename (root, "etc", NULL);
       g_free (root);
     }
@@ -234,7 +215,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 (this_module ());
 
   return gtk_data_prefix;
 }