From 1a058a41c9115681b834b2b4de519a4b43dfe534 Mon Sep 17 00:00:00 2001 From: Luca Bacci Date: Thu, 23 Mar 2023 16:27:17 +0100 Subject: [PATCH] Do not keep HINSTANCE variables around 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 --- gdk/win32/gdkclipdrop-win32.c | 6 ++-- gdk/win32/gdkcursor-win32.c | 16 ++++------ gdk/win32/gdkdisplay-win32.c | 6 ++-- gdk/win32/gdkevents-win32.c | 3 +- gdk/win32/gdkglcontext-win32-wgl.c | 6 ++-- gdk/win32/gdkglobals-win32.c | 2 -- gdk/win32/gdkinput-winpointer.c | 4 +-- gdk/win32/gdkmain-win32.c | 1 - gdk/win32/gdkprivate-win32.h | 10 ++++-- gdk/win32/gdksurface-win32.c | 10 +++--- gdk/win32/gdkvulkancontext-win32.c | 4 +-- gtk/gtkprintoperation-win32.c | 2 +- gtk/gtkwin32.c | 51 ++++++++++-------------------- 13 files changed, 50 insertions(+), 71 deletions(-) diff --git a/gdk/win32/gdkclipdrop-win32.c b/gdk/win32/gdkclipdrop-win32.c index 51c19a66cc..e9150a6a06 100644 --- a/gdk/win32/gdkclipdrop-win32.c +++ b/gdk/win32/gdkclipdrop-win32.c @@ -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; } diff --git a/gdk/win32/gdkcursor-win32.c b/gdk/win32/gdkcursor-win32.c index 5d388d04b2..ee2ce8ba77 100644 --- a/gdk/win32/gdkcursor-win32.c +++ b/gdk/win32/gdkcursor-win32.c @@ -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 diff --git a/gdk/win32/gdkdisplay-win32.c b/gdk/win32/gdkdisplay-win32.c index 843fe2c653..441f7fc914 100644 --- a/gdk/win32/gdkdisplay-win32.c +++ b/gdk/win32/gdkdisplay-win32.c @@ -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 ()); } } } diff --git a/gdk/win32/gdkevents-win32.c b/gdk/win32/gdkevents-win32.c index 10803a4845..7be0835d1e 100644 --- a/gdk/win32/gdkevents-win32.c +++ b/gdk/win32/gdkevents-win32.c @@ -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; diff --git a/gdk/win32/gdkglcontext-win32-wgl.c b/gdk/win32/gdkglcontext-win32-wgl.c index fdc8e50e6e..055c8a6b93 100644 --- a/gdk/win32/gdkglcontext-win32-wgl.c +++ b/gdk/win32/gdkglcontext-win32-wgl.c @@ -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 ()); } } diff --git a/gdk/win32/gdkglobals-win32.c b/gdk/win32/gdkglobals-win32.c index 07a6e4aa07..63b6118ef5 100644 --- a/gdk/win32/gdkglobals-win32.c +++ b/gdk/win32/gdkglobals-win32.c @@ -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; diff --git a/gdk/win32/gdkinput-winpointer.c b/gdk/win32/gdkinput-winpointer.c index 3c65167703..0782743453 100644 --- a/gdk/win32/gdkinput-winpointer.c +++ b/gdk/win32/gdkinput-winpointer.c @@ -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"); diff --git a/gdk/win32/gdkmain-win32.c b/gdk/win32/gdkmain-win32.c index d2d06d694a..bcc1d21ad4 100644 --- a/gdk/win32/gdkmain-win32.c +++ b/gdk/win32/gdkmain-win32.c @@ -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); diff --git a/gdk/win32/gdkprivate-win32.h b/gdk/win32/gdkprivate-win32.h index 918e961aa0..f4d9b4812a 100644 --- a/gdk/win32/gdkprivate-win32.h +++ b/gdk/win32/gdkprivate-win32.h @@ -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; +} + diff --git a/gdk/win32/gdksurface-win32.c b/gdk/win32/gdksurface-win32.c index df4cf37f9a..9e2564f853 100644 --- a/gdk/win32/gdksurface-win32.c +++ b/gdk/win32/gdksurface-win32.c @@ -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; diff --git a/gdk/win32/gdkvulkancontext-win32.c b/gdk/win32/gdkvulkancontext-win32.c index 894bbfa548..e31bc3839f 100644 --- a/gdk/win32/gdkvulkancontext-win32.c +++ b/gdk/win32/gdkvulkancontext-win32.c @@ -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. diff --git a/gtk/gtkprintoperation-win32.c b/gtk/gtkprintoperation-win32.c index 136f12270c..15d68abff4 100644 --- a/gtk/gtkprintoperation-win32.c +++ b/gtk/gtkprintoperation-win32.c @@ -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; diff --git a/gtk/gtkwin32.c b/gtk/gtkwin32.c index a4f61030dd..2144279e03 100644 --- a/gtk/gtkwin32.c +++ b/gtk/gtkwin32.c @@ -33,38 +33,19 @@ #include #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; } -- 2.30.2