mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 22:01:31 +11:00
[Windows] Avoid GetModuleHandle(NULL) (#2301)
Use get_instance_handle() over GetModuleHandle(NULL)
This commit is contained in:
parent
f11270dac0
commit
5d85c10a2c
|
@ -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<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,
|
||||
|
@ -681,7 +678,7 @@ fn create_event_target_window<T: 'static>() -> HWND {
|
|||
0,
|
||||
0,
|
||||
0,
|
||||
GetModuleHandleW(ptr::null()),
|
||||
util::get_instance_handle(),
|
||||
ptr::null(),
|
||||
);
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<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,
|
||||
|
|
Loading…
Reference in a new issue