From 5d85c10a2ccdb5254fc0143247f95cc8e5847c03 Mon Sep 17 00:00:00 2001 From: Aron Parker Date: Sun, 29 May 2022 17:12:46 +0200 Subject: [PATCH] [Windows] Avoid GetModuleHandle(NULL) (#2301) Use get_instance_handle() over GetModuleHandle(NULL) --- src/platform_impl/windows/event_loop.rs | 9 +++------ src/platform_impl/windows/icon.rs | 5 ++--- src/platform_impl/windows/util.rs | 22 ++++++++++++++++++++-- src/platform_impl/windows/window.rs | 5 ++--- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 5e8db28ac9..b3e9826649 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -27,10 +27,7 @@ use windows_sys::Win32::{ RDW_INTERNALPAINT, SC_SCREENSAVE, }, Media::{timeBeginPeriod, timeEndPeriod, timeGetDevCaps, TIMECAPS, TIMERR_NOERROR}, - System::{ - LibraryLoader::GetModuleHandleW, Ole::RevokeDragDrop, Threading::GetCurrentThreadId, - WindowsProgramming::INFINITE, - }, + System::{Ole::RevokeDragDrop, Threading::GetCurrentThreadId, WindowsProgramming::INFINITE}, UI::{ Controls::{HOVER_DEFAULT, WM_MOUSELEAVE}, Input::{ @@ -647,7 +644,7 @@ fn create_event_target_window() -> HWND { lpfnWndProc: Some(thread_event_target_callback::), cbClsExtra: 0, cbWndExtra: 0, - hInstance: GetModuleHandleW(ptr::null()), + hInstance: util::get_instance_handle(), hIcon: 0, hCursor: 0, // must be null in order for cursor state to work properly hbrBackground: 0, @@ -681,7 +678,7 @@ fn create_event_target_window() -> HWND { 0, 0, 0, - GetModuleHandleW(ptr::null()), + util::get_instance_handle(), ptr::null(), ); diff --git a/src/platform_impl/windows/icon.rs b/src/platform_impl/windows/icon.rs index 8ef47d6a4a..a6277da1e2 100644 --- a/src/platform_impl/windows/icon.rs +++ b/src/platform_impl/windows/icon.rs @@ -1,10 +1,9 @@ -use std::{fmt, io, mem, path::Path, ptr, sync::Arc}; +use std::{fmt, io, mem, path::Path, sync::Arc}; use windows_sys::{ core::PCWSTR, Win32::{ Foundation::HWND, - System::LibraryLoader::GetModuleHandleW, UI::WindowsAndMessaging::{ CreateIcon, DestroyIcon, LoadImageW, SendMessageW, HICON, ICON_BIG, ICON_SMALL, IMAGE_ICON, LR_DEFAULTSIZE, LR_LOADFROMFILE, WM_SETICON, @@ -111,7 +110,7 @@ impl WinIcon { let (width, height) = size.map(Into::into).unwrap_or((0, 0)); let handle = unsafe { LoadImageW( - GetModuleHandleW(ptr::null()), + util::get_instance_handle(), resource_id as PCWSTR, IMAGE_ICON, width as i32, diff --git a/src/platform_impl/windows/util.rs b/src/platform_impl/windows/util.rs index 43202316b3..a327701ffa 100644 --- a/src/platform_impl/windows/util.rs +++ b/src/platform_impl/windows/util.rs @@ -12,9 +12,12 @@ use std::{ use windows_sys::{ core::{HRESULT, PCWSTR}, Win32::{ - Foundation::{BOOL, HWND, RECT}, + Foundation::{BOOL, HINSTANCE, HWND, RECT}, Graphics::Gdi::{ClientToScreen, InvalidateRgn, HMONITOR}, - System::LibraryLoader::{GetProcAddress, LoadLibraryA}, + System::{ + LibraryLoader::{GetProcAddress, LoadLibraryA}, + SystemServices::IMAGE_DOS_HEADER, + }, UI::{ HiDpi::{DPI_AWARENESS_CONTEXT, MONITOR_DPI_TYPE, PROCESS_DPI_AWARENESS}, Input::KeyboardAndMouse::GetActiveWindow, @@ -205,6 +208,21 @@ pub fn is_focused(window: HWND) -> bool { window == unsafe { GetActiveWindow() } } +pub fn get_instance_handle() -> HINSTANCE { + // Gets the instance handle by taking the address of the + // pseudo-variable created by the microsoft linker: + // https://devblogs.microsoft.com/oldnewthing/20041025-00/?p=37483 + + // This is preferred over GetModuleHandle(NULL) because it also works in DLLs: + // https://stackoverflow.com/questions/21718027/getmodulehandlenull-vs-hinstance + + extern "C" { + static __ImageBase: IMAGE_DOS_HEADER; + } + + unsafe { &__ImageBase as *const _ as _ } +} + impl CursorIcon { pub(crate) fn to_windows_cursor(self) -> PCWSTR { match self { diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 60179ecefb..29c4d3898d 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -26,7 +26,6 @@ use windows_sys::Win32::{ Com::{ CoCreateInstance, CoInitializeEx, CoUninitialize, CLSCTX_ALL, COINIT_APARTMENTTHREADED, }, - LibraryLoader::GetModuleHandleW, Ole::{OleInitialize, RegisterDragDrop}, }, UI::{ @@ -974,7 +973,7 @@ where CW_USEDEFAULT, parent.unwrap_or(0), pl_attribs.menu.unwrap_or(0), - GetModuleHandleW(ptr::null()), + util::get_instance_handle(), &mut initdata as *mut _ as *mut _, ); @@ -1013,7 +1012,7 @@ unsafe fn register_window_class( lpfnWndProc: Some(super::event_loop::public_window_callback::), cbClsExtra: 0, cbWndExtra: 0, - hInstance: GetModuleHandleW(ptr::null()), + hInstance: util::get_instance_handle(), hIcon: h_icon, hCursor: 0, // must be null in order for cursor state to work properly hbrBackground: 0,