Skip to content

Detect Windows reboot (WM_QUERYENDSESSION and WM_ENDSESSION) #4360

@erith835

Description

@erith835

Description

Hello, I want my app to detect when Windows reboots. I wrote a test app that compiles and runs and detects lots of events, but not the WM_QUERYENDSESSION or WM_ENDSESSION events. What is wrong? Or is there any other way to detect a Windows reboot or shut down?

Cargo.toml

[package]
name = "test"
version = "0.1.0"
edition = "2024"

[dependencies]
winit = "0.30.12"

winapi = { version = "0.3.9", features = ["winuser"] }
windows-sys = { version = "0.61.0", features = [
    "Win32_UI_WindowsAndMessaging",
] }

main.rs

use winapi::um::winuser::MSG;
use windows_sys::Win32::UI::WindowsAndMessaging;
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
use winit::event_loop::EventLoop;
use winit::platform::windows::EventLoopBuilderExtWindows;
use winit::window::{Window, WindowId};

#[derive(Default)]
struct App {
    window: Option<Window>,
}
impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        self.window = Some(
            event_loop
                .create_window(Window::default_attributes())
                .unwrap(),
        );
    }
    fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
        match event {
            WindowEvent::CloseRequested => {
                log_event("CloseRequested");
                event_loop.exit();
            }
            WindowEvent::RedrawRequested => {
                self.window.as_ref().unwrap().request_redraw();
            }
            _ => (),
        }
    }
}
fn log_event(event: &str) {
    use std::io::Write;
    println!("{:?}", event);
    let mut file = std::fs::OpenOptions::new()
        .write(true)
        .create(true)
        .append(true)
        .open("log.txt")
        .unwrap();
    let _ = writeln!(file, "{}", event);
}
fn main() {
    let mut builder = EventLoop::builder();
    builder.with_msg_hook(|msg| {
        let msg = msg as *const MSG;
        let mess = unsafe { (*msg).message };
        match mess {
            WindowsAndMessaging::WM_PAINT => (),
            WindowsAndMessaging::WM_NCMOUSEMOVE => (),
            WindowsAndMessaging::WM_INPUT => (),
            WindowsAndMessaging::WM_MOUSEMOVE => (),
            WindowsAndMessaging::WM_QUERYENDSESSION => log_event("WM_QUERYENDSESSION"),
            WindowsAndMessaging::WM_ENDSESSION => log_event("WM_ENDSESSION"),
            _ => {
                log_event(&format!("{}", mess));
            }
        }
        false
    });
    let event_loop = builder.build().unwrap();
    let mut app = App::default();
    let _ = event_loop.run_app(&mut app);
}

Windows version

Microsoft Windows [Version 10.0.26100.6584]

Winit version

0.30.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    B - bugDang, that shouldn't have happened

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions