{"record":{"id":"96075c22ffc04db1","repo":"libnyanpasu/clash-nyanpasu","slug":"failed-to-register-window-class-err","errorCode":null,"errorMessage":"Failed to register window class: {err}","messagePattern":"Failed to register window class: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/shutdown_hook.rs","lineNumber":140,"sourceCode":"\n    SHUTDOWN_HOOK_INSTANCE.set(tx).unwrap();\n\n    let h_instance = unsafe { GetModuleHandleW(std::ptr::null()) };\n    if h_instance.is_null() {\n        let err = Error::from_win32();\n        anyhow::bail!(\"Failed to get module handle: {err}\");\n    }\n\n    let mut window_class_ex = unsafe { std::mem::zeroed::<WNDCLASSEXW>() };\n    window_class_ex.cbSize = std::mem::size_of::<WNDCLASSEXW>() as u32;\n    window_class_ex.lpszClassName = class_name.as_ptr();\n    window_class_ex.lpfnWndProc = Some(callback);\n    window_class_ex.hInstance = h_instance;\n\n    unsafe {\n        if RegisterClassExW(&window_class_ex) == 0 {\n            let err = Error::from_win32();\n            anyhow::bail!(\"Failed to register window class: {err}\");\n        }\n    }\n\n    let window_name = w!(\"TAURI_SHUTDOWN_HOOK_WINDOW\");\n    let hidden_window = unsafe {\n        CreateWindowExW(\n            WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE,\n            class_name.as_ptr(),\n            window_name.as_ptr(),\n            0,\n            0,\n            0,\n            0,\n            0,\n            std::ptr::null_mut(),\n            std::ptr::null_mut(),\n            h_instance,\n            std::ptr::null_mut(),","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/shutdown_hook.rs#L122-L158","documentation":"RegisterClassExW failed to register the Win32 window class used by the shutdown hook's hidden message-only window, returning 0. The hook converts the OS error via Error::from_win32() and aborts setup. Without the class, no shutdown-notification window can be created.","triggerScenarios":"Calling setup_shutdown_hook on Windows when RegisterClassExW returns 0 — e.g. invalid WNDCLASSEX fields, class name conflicts with a different style, or resource/ATOM exhaustion after many registrations in the same process/module.","commonSituations":"Long-running GUI sessions registering the hook repeatedly without unregistering, malformed window class configuration, or Windows desktop-station/availability issues at process startup.","solutions":["Check the from_win32 error text to identify the OS reason (e.g. class already exists, invalid parameter).","Ensure setup_shutdown_hook is called once per process; unregister the class on teardown if re-registering.","Verify the WNDCLASSEX fields (cbSize, lpfnWndProc, hInstance, lpszClassName) are valid before RegisterClassExW.","Confirm the process has an interactive window station/desktop at the time of registration."],"exampleFix":"// before\nif RegisterClassExW(&window_class_ex) == 0 {\n    let err = Error::from_win32();\n    anyhow::bail!(\"Failed to register window class: {err}\");\n}\n// after\nif RegisterClassExW(&window_class_ex) == 0 {\n    let err = Error::from_win32();\n    log::warn!(\"window class registration failed ({err}); shutdown hook disabled\");\n    return Ok(()); // degrade gracefully instead of failing startup\n}","handlingStrategy":"try-catch","validationCode":"// ensure single registration per process\nuse std::sync::atomic::{AtomicBool, Ordering};\nstatic CLASS_REGISTERED: AtomicBool = AtomicBool::new(false);\nfn can_register() -> bool { !CLASS_REGISTERED.load(Ordering::SeqCst) }","typeGuard":null,"tryCatchPattern":"match setup_shutdown_hook() {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"register window class\") => {\n        log::warn!(\"shutdown hook unavailable: {e:#}\"); // degrade, don't abort app\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Register the window class once per process lifecycle","Log the from_win32 GetLastError code for diagnosability","Degrade gracefully: a missing shutdown hook should not block app startup"],"tags":["windows","win32","window-class","startup"],"backgroundTag":"module-init-failed","analyzedSha":"f7dbce2997c633e484f54788035e770b3ee99773","analyzedAt":"2026-09-08T01:24:59.197Z","contentChangedAt":"2026-09-08T01:24:59.197Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}