Skip to content

Commit

Permalink
[Windows] Avoid GetModuleHandle(NULL) (#2301)
Browse files Browse the repository at this point in the history
Use get_instance_handle() over GetModuleHandle(NULL)
  • Loading branch information
AronParker authored May 29, 2022
1 parent f11270d commit 5d85c10
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
9 changes: 3 additions & 6 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -647,7 +644,7 @@ fn create_event_target_window<T: 'static>() -> HWND {
lpfnWndProc: Some(thread_event_target_callback::<T>),
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,
Expand Down Expand Up @@ -681,7 +678,7 @@ fn create_event_target_window<T: 'static>() -> HWND {
0,
0,
0,
GetModuleHandleW(ptr::null()),
util::get_instance_handle(),
ptr::null(),
);

Expand Down
5 changes: 2 additions & 3 deletions src/platform_impl/windows/icon.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 20 additions & 2 deletions src/platform_impl/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use windows_sys::Win32::{
Com::{
CoCreateInstance, CoInitializeEx, CoUninitialize, CLSCTX_ALL, COINIT_APARTMENTTHREADED,
},
LibraryLoader::GetModuleHandleW,
Ole::{OleInitialize, RegisterDragDrop},
},
UI::{
Expand Down Expand Up @@ -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 _,
);

Expand Down Expand Up @@ -1013,7 +1012,7 @@ unsafe fn register_window_class<T: 'static>(
lpfnWndProc: Some(super::event_loop::public_window_callback::<T>),
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,
Expand Down

0 comments on commit 5d85c10

Please sign in to comment.