Skip to content

Commit f7bbd11

Browse files
committed
fix(win32): ignore ERROR_CLASS_ALREADY_EXISTS
1 parent ca97049 commit f7bbd11

1 file changed

Lines changed: 36 additions & 24 deletions

File tree

winio-ui-win32/src/ui/window.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@ use std::{
66

77
use compio::driver::syscall;
88
use inherit_methods_macro::inherit_methods;
9-
use widestring::U16CString;
10-
use windows_sys::{
11-
Win32::{
12-
Foundation::{ERROR_INVALID_HANDLE, HWND, LPARAM, LRESULT, POINT, SetLastError, WPARAM},
13-
Graphics::Gdi::{GetStockObject, MapWindowPoints, WHITE_BRUSH},
14-
System::LibraryLoader::GetModuleHandleW,
15-
UI::{
16-
Input::KeyboardAndMouse::{EnableWindow, IsWindowEnabled},
17-
WindowsAndMessaging::{
18-
CW_USEDEFAULT, CloseWindow, CreateWindowExW, DestroyWindow, GWL_EXSTYLE, GWL_STYLE,
19-
GetClientRect, GetParent, GetWindowLongPtrW, GetWindowRect, GetWindowTextLengthW,
20-
GetWindowTextW, HICON, HWND_DESKTOP, ICON_BIG, IDC_ARROW, IMAGE_ICON,
21-
IsWindowVisible, LR_DEFAULTCOLOR, LR_DEFAULTSIZE, LR_SHARED, LoadCursorW,
22-
LoadImageW, RegisterClassExW, SW_HIDE, SW_SHOW, SWP_NOMOVE, SWP_NOSIZE,
23-
SWP_NOZORDER, SendMessageW, SetWindowLongPtrW, SetWindowPos, SetWindowTextW,
24-
ShowWindow, WM_CLOSE, WM_MOVE, WM_SETICON, WM_SIZE, WNDCLASSEXW, WS_CHILDWINDOW,
25-
WS_OVERLAPPEDWINDOW,
26-
},
9+
use widestring::{U16CStr, U16CString, u16cstr};
10+
use windows_sys::Win32::{
11+
Foundation::{
12+
ERROR_CLASS_ALREADY_EXISTS, ERROR_INVALID_HANDLE, HWND, LPARAM, LRESULT, POINT,
13+
SetLastError, WPARAM,
14+
},
15+
Graphics::Gdi::{GetStockObject, MapWindowPoints, WHITE_BRUSH},
16+
System::LibraryLoader::GetModuleHandleW,
17+
UI::{
18+
Input::KeyboardAndMouse::{EnableWindow, IsWindowEnabled},
19+
WindowsAndMessaging::{
20+
CW_USEDEFAULT, CloseWindow, CreateWindowExW, DestroyWindow, GWL_EXSTYLE, GWL_STYLE,
21+
GetClientRect, GetParent, GetWindowLongPtrW, GetWindowRect, GetWindowTextLengthW,
22+
GetWindowTextW, HICON, HWND_DESKTOP, ICON_BIG, IDC_ARROW, IMAGE_ICON, IsWindowVisible,
23+
LR_DEFAULTCOLOR, LR_DEFAULTSIZE, LR_SHARED, LoadCursorW, LoadImageW, RegisterClassExW,
24+
SW_HIDE, SW_SHOW, SWP_NOMOVE, SWP_NOSIZE, SWP_NOZORDER, SendMessageW,
25+
SetWindowLongPtrW, SetWindowPos, SetWindowTextW, ShowWindow, WM_CLOSE, WM_MOVE,
26+
WM_SETICON, WM_SIZE, WNDCLASSEXW, WS_CHILDWINDOW, WS_OVERLAPPEDWINDOW,
2727
},
2828
},
29-
w,
3029
};
3130
use winio_handle::{AsRawWindow, AsWindow, BorrowedWindow, RawWindow};
3231
use winio_primitive::{Point, Size};
@@ -321,7 +320,8 @@ impl AsRawWindow for Widget {
321320
}
322321
}
323322

324-
pub(crate) const WINDOW_CLASS_NAME: *const u16 = w!("XamlWindow");
323+
pub(crate) const WINDOW_CLASS_NAME: &U16CStr =
324+
u16cstr!(concat!("WinioWindowVersion", env!("CARGO_PKG_VERSION")));
325325

326326
fn register() {
327327
unsafe {
@@ -338,10 +338,17 @@ fn register() {
338338
hCursor: unsafe { LoadCursorW(null_mut(), IDC_ARROW) },
339339
hbrBackground: unsafe { GetStockObject(WHITE_BRUSH) },
340340
lpszMenuName: null(),
341-
lpszClassName: WINDOW_CLASS_NAME,
341+
lpszClassName: WINDOW_CLASS_NAME.as_ptr(),
342342
hIconSm: null_mut(),
343343
};
344-
syscall!(BOOL, unsafe { RegisterClassExW(&cls) }).unwrap();
344+
match syscall!(BOOL, unsafe { RegisterClassExW(&cls) }) {
345+
Ok(_) => {}
346+
Err(e) if e.raw_os_error() == Some(ERROR_CLASS_ALREADY_EXISTS as _) => {
347+
// The class is already registered. We choose to ignore this error,
348+
// and hope that the class is still valid.
349+
}
350+
Err(e) => panic!("{e:?}"),
351+
}
345352
}
346353

347354
static REGISTER: Once = Once::new();
@@ -361,13 +368,18 @@ impl Window {
361368
register_once();
362369
let handle = if let Some(parent) = parent {
363370
Widget::new(
364-
WINDOW_CLASS_NAME,
371+
WINDOW_CLASS_NAME.as_ptr(),
365372
WS_OVERLAPPEDWINDOW | WS_CHILDWINDOW,
366373
0,
367374
parent.as_window().as_raw_window(),
368375
)
369376
} else {
370-
Widget::new(WINDOW_CLASS_NAME, WS_OVERLAPPEDWINDOW, 0, null_mut())
377+
Widget::new(
378+
WINDOW_CLASS_NAME.as_ptr(),
379+
WS_OVERLAPPEDWINDOW,
380+
0,
381+
null_mut(),
382+
)
371383
};
372384
let this = Self { handle };
373385
unsafe { window_use_dark_mode(this.as_raw_window()) };

0 commit comments

Comments
 (0)