Skip to content

Commit 5d85c10

Browse files
authored
[Windows] Avoid GetModuleHandle(NULL) (#2301)
Use get_instance_handle() over GetModuleHandle(NULL)
1 parent f11270d commit 5d85c10

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/platform_impl/windows/event_loop.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ use windows_sys::Win32::{
2727
RDW_INTERNALPAINT, SC_SCREENSAVE,
2828
},
2929
Media::{timeBeginPeriod, timeEndPeriod, timeGetDevCaps, TIMECAPS, TIMERR_NOERROR},
30-
System::{
31-
LibraryLoader::GetModuleHandleW, Ole::RevokeDragDrop, Threading::GetCurrentThreadId,
32-
WindowsProgramming::INFINITE,
33-
},
30+
System::{Ole::RevokeDragDrop, Threading::GetCurrentThreadId, WindowsProgramming::INFINITE},
3431
UI::{
3532
Controls::{HOVER_DEFAULT, WM_MOUSELEAVE},
3633
Input::{
@@ -647,7 +644,7 @@ fn create_event_target_window<T: 'static>() -> HWND {
647644
lpfnWndProc: Some(thread_event_target_callback::<T>),
648645
cbClsExtra: 0,
649646
cbWndExtra: 0,
650-
hInstance: GetModuleHandleW(ptr::null()),
647+
hInstance: util::get_instance_handle(),
651648
hIcon: 0,
652649
hCursor: 0, // must be null in order for cursor state to work properly
653650
hbrBackground: 0,
@@ -681,7 +678,7 @@ fn create_event_target_window<T: 'static>() -> HWND {
681678
0,
682679
0,
683680
0,
684-
GetModuleHandleW(ptr::null()),
681+
util::get_instance_handle(),
685682
ptr::null(),
686683
);
687684

src/platform_impl/windows/icon.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::{fmt, io, mem, path::Path, ptr, sync::Arc};
1+
use std::{fmt, io, mem, path::Path, sync::Arc};
22

33
use windows_sys::{
44
core::PCWSTR,
55
Win32::{
66
Foundation::HWND,
7-
System::LibraryLoader::GetModuleHandleW,
87
UI::WindowsAndMessaging::{
98
CreateIcon, DestroyIcon, LoadImageW, SendMessageW, HICON, ICON_BIG, ICON_SMALL,
109
IMAGE_ICON, LR_DEFAULTSIZE, LR_LOADFROMFILE, WM_SETICON,
@@ -111,7 +110,7 @@ impl WinIcon {
111110
let (width, height) = size.map(Into::into).unwrap_or((0, 0));
112111
let handle = unsafe {
113112
LoadImageW(
114-
GetModuleHandleW(ptr::null()),
113+
util::get_instance_handle(),
115114
resource_id as PCWSTR,
116115
IMAGE_ICON,
117116
width as i32,

src/platform_impl/windows/util.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ use std::{
1212
use windows_sys::{
1313
core::{HRESULT, PCWSTR},
1414
Win32::{
15-
Foundation::{BOOL, HWND, RECT},
15+
Foundation::{BOOL, HINSTANCE, HWND, RECT},
1616
Graphics::Gdi::{ClientToScreen, InvalidateRgn, HMONITOR},
17-
System::LibraryLoader::{GetProcAddress, LoadLibraryA},
17+
System::{
18+
LibraryLoader::{GetProcAddress, LoadLibraryA},
19+
SystemServices::IMAGE_DOS_HEADER,
20+
},
1821
UI::{
1922
HiDpi::{DPI_AWARENESS_CONTEXT, MONITOR_DPI_TYPE, PROCESS_DPI_AWARENESS},
2023
Input::KeyboardAndMouse::GetActiveWindow,
@@ -205,6 +208,21 @@ pub fn is_focused(window: HWND) -> bool {
205208
window == unsafe { GetActiveWindow() }
206209
}
207210

211+
pub fn get_instance_handle() -> HINSTANCE {
212+
// Gets the instance handle by taking the address of the
213+
// pseudo-variable created by the microsoft linker:
214+
// https://devblogs.microsoft.com/oldnewthing/20041025-00/?p=37483
215+
216+
// This is preferred over GetModuleHandle(NULL) because it also works in DLLs:
217+
// https://stackoverflow.com/questions/21718027/getmodulehandlenull-vs-hinstance
218+
219+
extern "C" {
220+
static __ImageBase: IMAGE_DOS_HEADER;
221+
}
222+
223+
unsafe { &__ImageBase as *const _ as _ }
224+
}
225+
208226
impl CursorIcon {
209227
pub(crate) fn to_windows_cursor(self) -> PCWSTR {
210228
match self {

src/platform_impl/windows/window.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use windows_sys::Win32::{
2626
Com::{
2727
CoCreateInstance, CoInitializeEx, CoUninitialize, CLSCTX_ALL, COINIT_APARTMENTTHREADED,
2828
},
29-
LibraryLoader::GetModuleHandleW,
3029
Ole::{OleInitialize, RegisterDragDrop},
3130
},
3231
UI::{
@@ -974,7 +973,7 @@ where
974973
CW_USEDEFAULT,
975974
parent.unwrap_or(0),
976975
pl_attribs.menu.unwrap_or(0),
977-
GetModuleHandleW(ptr::null()),
976+
util::get_instance_handle(),
978977
&mut initdata as *mut _ as *mut _,
979978
);
980979

@@ -1013,7 +1012,7 @@ unsafe fn register_window_class<T: 'static>(
10131012
lpfnWndProc: Some(super::event_loop::public_window_callback::<T>),
10141013
cbClsExtra: 0,
10151014
cbWndExtra: 0,
1016-
hInstance: GetModuleHandleW(ptr::null()),
1015+
hInstance: util::get_instance_handle(),
10171016
hIcon: h_icon,
10181017
hCursor: 0, // must be null in order for cursor state to work properly
10191018
hbrBackground: 0,

0 commit comments

Comments
 (0)