Skip to content

Commit 8486856

Browse files
committed
[WIN32API] Use Wide(W) variants consistently
1 parent 13700f0 commit 8486856

3 files changed

Lines changed: 38 additions & 24 deletions

File tree

rio-window/src/platform_impl/windows/dark_mode.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use std::{ffi::c_void, ptr};
44

55
use crate::utils::Lazy;
66
use windows_sys::core::PCSTR;
7+
use windows_sys::w;
78
use windows_sys::Win32::Foundation::{BOOL, HWND, NTSTATUS, S_OK};
8-
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
9+
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW};
910
use windows_sys::Win32::System::SystemInformation::OSVERSIONINFOW;
10-
use windows_sys::Win32::UI::Accessibility::{HCF_HIGHCONTRASTON, HIGHCONTRASTA};
11+
use windows_sys::Win32::UI::Accessibility::{HCF_HIGHCONTRASTON, HIGHCONTRASTW};
1112
use windows_sys::Win32::UI::Controls::SetWindowTheme;
1213
use windows_sys::Win32::UI::WindowsAndMessaging::{
13-
SystemParametersInfoA, SPI_GETHIGHCONTRAST,
14+
SystemParametersInfoW, SPI_GETHIGHCONTRAST,
1415
};
1516

1617
use crate::window::Theme;
@@ -146,7 +147,7 @@ fn should_apps_use_dark_mode() -> bool {
146147
return None;
147148
}
148149

149-
let module = LoadLibraryA("uxtheme.dll\0".as_ptr());
150+
let module = LoadLibraryW(w!("uxtheme.dll"));
150151

151152
if module.is_null() {
152153
return None;
@@ -163,14 +164,14 @@ fn should_apps_use_dark_mode() -> bool {
163164
}
164165

165166
fn is_high_contrast() -> bool {
166-
let mut hc = HIGHCONTRASTA {
167+
let mut hc = HIGHCONTRASTW {
167168
cbSize: 0,
168169
dwFlags: 0,
169170
lpszDefaultScheme: ptr::null_mut(),
170171
};
171172

172173
let ok = unsafe {
173-
SystemParametersInfoA(
174+
SystemParametersInfoW(
174175
SPI_GETHIGHCONTRAST,
175176
std::mem::size_of_val(&hc) as _,
176177
&mut hc as *mut _ as _,

rio-window/src/platform_impl/windows/event_loop.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use std::{mem, panic, ptr};
1818

1919
use crate::utils::Lazy;
2020

21+
use windows_sys::core::PCWSTR;
22+
use windows_sys::w;
2123
use windows_sys::Win32::Foundation::{
2224
GetLastError, FALSE, HANDLE, HWND, LPARAM, LRESULT, POINT, RECT, WAIT_FAILED, WPARAM,
2325
};
@@ -50,7 +52,7 @@ use windows_sys::Win32::UI::Input::{
5052
use windows_sys::Win32::UI::WindowsAndMessaging::{
5153
CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, GetClientRect,
5254
GetCursorPos, GetMenu, LoadCursorW, MsgWaitForMultipleObjectsEx, PeekMessageW,
53-
PostMessageW, RegisterClassExW, RegisterWindowMessageA, SetCursor, SetWindowPos,
55+
PostMessageW, RegisterClassExW, RegisterWindowMessageW, SetCursor, SetWindowPos,
5456
TranslateMessage, CREATESTRUCTW, GIDC_ARRIVAL, GIDC_REMOVAL, GWL_STYLE, GWL_USERDATA,
5557
HTCAPTION, HTCLIENT, MINMAXINFO, MNC_CLOSE, MSG, MWMO_INPUTAVAILABLE,
5658
NCCALCSIZE_PARAMS, PM_REMOVE, PT_PEN, PT_TOUCH, QS_ALLINPUT, RI_MOUSE_HWHEEL,
@@ -898,15 +900,15 @@ pub struct LazyMessageId {
898900
id: AtomicU32,
899901

900902
/// The name of the message.
901-
name: &'static str,
903+
name: PCWSTR,
902904
}
903905

904906
/// An invalid custom window ID.
905907
const INVALID_ID: u32 = 0x0;
906908

907909
impl LazyMessageId {
908910
/// Create a new `LazyId`.
909-
const fn new(name: &'static str) -> Self {
911+
const fn new(name: PCWSTR) -> Self {
910912
Self {
911913
id: AtomicU32::new(INVALID_ID),
912914
name,
@@ -923,15 +925,20 @@ impl LazyMessageId {
923925
}
924926

925927
// Register the message.
926-
// SAFETY: We are sure that the pointer is a valid C string ending with '\0'.
927-
assert!(self.name.ends_with('\0'));
928-
let new_id = unsafe { RegisterWindowMessageA(self.name.as_ptr()) };
928+
// assert!(self.name.ends_with('\0'));
929+
let new_id = unsafe { RegisterWindowMessageW(self.name) };
929930

930931
assert_ne!(
931932
new_id,
932933
0,
933-
"RegisterWindowMessageA returned zero for '{}': {}",
934-
self.name,
934+
"RegisterWindowMessageW returned zero for '{}': {}",
935+
unsafe {
936+
let mut len = 0;
937+
while *self.name.add(len) != 0 {
938+
len += 1;
939+
}
940+
String::from_utf16_lossy(std::slice::from_raw_parts(self.name, len))
941+
},
935942
std::io::Error::last_os_error()
936943
);
937944

@@ -944,27 +951,30 @@ impl LazyMessageId {
944951
new_id
945952
}
946953
}
954+
// SAFETY: PCWSTR is read-only in this context
955+
unsafe impl Sync for LazyMessageId {}
947956

948957
// Message sent by the `EventLoopProxy` when we want to wake up the thread.
949958
// WPARAM and LPARAM are unused.
950-
static USER_EVENT_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::WakeupMsg\0");
959+
static USER_EVENT_MSG_ID: LazyMessageId = LazyMessageId::new(w!("Winit::WakeupMsg"));
951960
// Message sent when we want to execute a closure in the thread.
952961
// WPARAM contains a Box<Box<dyn FnMut()>> that must be retrieved with `Box::from_raw`,
953962
// and LPARAM is unused.
954-
static EXEC_MSG_ID: LazyMessageId = LazyMessageId::new("Winit::ExecMsg\0");
963+
static EXEC_MSG_ID: LazyMessageId = LazyMessageId::new(w!("Winit::ExecMsg"));
955964
// Message sent by a `Window` when it wants to be destroyed by the main thread.
956965
// WPARAM and LPARAM are unused.
957966
pub(crate) static DESTROY_MSG_ID: LazyMessageId =
958-
LazyMessageId::new("Winit::DestroyMsg\0");
967+
LazyMessageId::new(w!("Winit::DestroyMsg"));
959968
// WPARAM is a bool specifying the `WindowFlags::MARKER_RETAIN_STATE_ON_SIZE` flag. See the
960969
// documentation in the `window_state` module for more information.
961970
pub(crate) static SET_RETAIN_STATE_ON_SIZE_MSG_ID: LazyMessageId =
962-
LazyMessageId::new("Winit::SetRetainMaximized\0");
971+
LazyMessageId::new(w!("Winit::SetRetainMaximized"));
963972
static THREAD_EVENT_TARGET_WINDOW_CLASS: Lazy<Vec<u16>> =
964973
Lazy::new(|| util::encode_wide("Winit Thread Event Target"));
965974
/// When the taskbar is created, it registers a message with the "TaskbarCreated" string and then
966975
/// broadcasts this message to all top-level windows <https://docs.microsoft.com/en-us/windows/win32/shell/taskbar#taskbar-creation-notification>
967-
pub(crate) static TASKBAR_CREATED: LazyMessageId = LazyMessageId::new("TaskbarCreated\0");
976+
pub(crate) static TASKBAR_CREATED: LazyMessageId =
977+
LazyMessageId::new(w!("TaskbarCreated"));
968978

969979
fn create_event_target_window() -> HWND {
970980
use windows_sys::Win32::UI::WindowsAndMessaging::{CS_HREDRAW, CS_VREDRAW};

rio-window/src/platform_impl/windows/util.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::utils::Lazy;
99
use windows_sys::core::{HRESULT, PCWSTR};
1010
use windows_sys::Win32::Foundation::{BOOL, HANDLE, HMODULE, HWND, RECT};
1111
use windows_sys::Win32::Graphics::Gdi::{ClientToScreen, HMONITOR};
12-
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryA};
12+
use windows_sys::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW};
1313
use windows_sys::Win32::System::SystemServices::IMAGE_DOS_HEADER;
1414
use windows_sys::Win32::UI::HiDpi::{
1515
DPI_AWARENESS_CONTEXT, MONITOR_DPI_TYPE, PROCESS_DPI_AWARENESS,
@@ -193,12 +193,15 @@ pub(crate) fn to_windows_cursor(cursor: CursorIcon) -> PCWSTR {
193193
// may not be available on all Windows platforms supported by winit.
194194
//
195195
// `library` and `function` must be zero-terminated.
196-
pub(super) fn get_function_impl(library: &str, function: &str) -> Option<*const c_void> {
197-
assert_eq!(library.chars().last(), Some('\0'));
196+
pub(super) fn get_function_impl(
197+
library: PCWSTR,
198+
function: &str,
199+
) -> Option<*const c_void> {
200+
// assert_eq!(library.chars().last(), Some('\0'));
198201
assert_eq!(function.chars().last(), Some('\0'));
199202

200203
// Library names we will use are ASCII so we can use the A version to avoid string conversion.
201-
let module = unsafe { LoadLibraryA(library.as_ptr()) };
204+
let module = unsafe { LoadLibraryW(library) };
202205
if module.is_null() {
203206
return None;
204207
}
@@ -210,7 +213,7 @@ pub(super) fn get_function_impl(library: &str, function: &str) -> Option<*const
210213
macro_rules! get_function {
211214
($lib:expr, $func:ident) => {
212215
crate::platform_impl::platform::util::get_function_impl(
213-
concat!($lib, '\0'),
216+
::windows_sys::w!($lib),
214217
concat!(stringify!($func), '\0'),
215218
)
216219
.map(|f| unsafe { std::mem::transmute::<*const _, $func>(f) })

0 commit comments

Comments
 (0)