From f75177e9b64087ccefcf1b087d94bc4545df0afb Mon Sep 17 00:00:00 2001 From: Mirko Covizzi Date: Mon, 25 May 2020 22:15:58 +0200 Subject: [PATCH] Setup unique class naming for windows on Windows --- Cargo.toml | 2 +- src/win/window.rs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d1e864b..4af40fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ edition = "2018" [dependencies] [target.'cfg(windows)'.dependencies] -winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", "minwindef"] } +winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", "minwindef", "guiddef", "combaseapi"] } diff --git a/src/win/window.rs b/src/win/window.rs index 5c460cc..4684907 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -1,8 +1,10 @@ extern crate winapi; use self::winapi::_core::mem::MaybeUninit; +use self::winapi::shared::guiddef::GUID; use self::winapi::shared::minwindef::{LPARAM, LPVOID, LRESULT, UINT, WPARAM}; use self::winapi::shared::windef::{HBRUSH, HICON, HMENU, HWND}; +use self::winapi::um::combaseapi::CoCreateGuid; use self::winapi::um::libloaderapi::GetModuleHandleA; use self::winapi::um::winuser::{ CreateWindowExA, DefWindowProcA, DispatchMessageA, PeekMessageA, PostQuitMessage, @@ -47,13 +49,31 @@ pub unsafe extern "system" fn wnd_proc( pub fn create_window(w: WindowOpenOptions) { unsafe { + // We generate a unique name for the new window class to prevent name collisions + let mut guid: GUID = MaybeUninit::uninit().assume_init(); + CoCreateGuid(&mut guid); + let class_name = format!( + "Baseview-{}-{}-{}-{}{}{}{}{}{}{}{}\0", + guid.Data1, + guid.Data2, + guid.Data3, + guid.Data4[0], + guid.Data4[1], + guid.Data4[2], + guid.Data4[3], + guid.Data4[4], + guid.Data4[5], + guid.Data4[6], + guid.Data4[7] + ); + let hinstance = GetModuleHandleA(0 as *const i8); let wnd_class = WNDCLASSA { // todo: for OpenGL, will use it later style: CS_OWNDC | CS_HREDRAW | CS_VREDRAW, lpfnWndProc: Some(wnd_proc), hInstance: hinstance, - lpszClassName: "BaseviewClass\0".as_ptr() as *const i8, + lpszClassName: class_name.as_ptr() as *const i8, cbClsExtra: 0, cbWndExtra: 0, hIcon: 0 as HICON, @@ -65,7 +85,8 @@ pub fn create_window(w: WindowOpenOptions) { let _hwnd = CreateWindowExA( 0, - "BaseviewClass\0".as_ptr() as *const i8, + class_name.as_ptr() as *const i8, + // todo: change title based on options struct "Baseview\0".as_ptr() as *const i8, // todo: fine for now, will have to change with a parent WS_OVERLAPPEDWINDOW | WS_VISIBLE,