@@ -18,6 +18,8 @@ use std::{mem, panic, ptr};
1818
1919use crate :: utils:: Lazy ;
2020
21+ use windows_sys:: core:: PCWSTR ;
22+ use windows_sys:: w;
2123use 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::{
5052use 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.
905907const INVALID_ID : u32 = 0x0 ;
906908
907909impl 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.
957966pub ( 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.
961970pub ( crate ) static SET_RETAIN_STATE_ON_SIZE_MSG_ID : LazyMessageId =
962- LazyMessageId :: new ( "Winit::SetRetainMaximized\0 " ) ;
971+ LazyMessageId :: new ( w ! ( "Winit::SetRetainMaximized" ) ) ;
963972static 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
969979fn create_event_target_window ( ) -> HWND {
970980 use windows_sys:: Win32 :: UI :: WindowsAndMessaging :: { CS_HREDRAW , CS_VREDRAW } ;
0 commit comments