Sinono3 / souvlaki

A cross-platform library for handling OS media controls and metadata.
MIT License
79 stars 15 forks source link

Handle Access is denied error on Windows from not running as Administrator #30

Open ShayBox opened 1 year ago

ShayBox commented 1 year ago

I'm trying to handle the windows error from MediaControls::new that it panics when the program isn't run as Administrator, but I don't see how to convert the error to a WindowsError, only from.

I also can't seem to use error-stack IntoReport on souvlaki errors like other errors, they don't implement std::error::Error.

The Administrator requirement may be from using the existing HWND created by the terminal when opening my program, because I don't spawn a window myself.

#[cfg(not(target_os = "windows"))]
let hwnd = None;

#[cfg(target_os = "windows")]
let hwnd = {
    use windows::Win32::System::Console::GetConsoleWindow;

    let console_window = unsafe { GetConsoleWindow() };
    let hwnd = console_window.0 as *mut c_void;
    Some(hwnd)
};

let platform_config = PlatformConfig {
    dbus_name: "VRC_OSC",
    display_name: "VRC-OSC",
    hwnd,
};

let controls = match MediaControls::new(platform_config) {
    Ok(controls) => controls,
    Err(error) => {
        // I can see the error code when printing but I can't access it
        println!("{:?}", error);
        panic!("{error:?}")
    }
};

EDIT: I can't seem to set_playback for sources other than my own with set_metadata, is it possible to do this?

Sinono3 commented 1 year ago

As for the std::error::Error problem, I opened #32. Let me check the other.

Sinono3 commented 1 year ago

I can't replicate the error. Access denied still pops up when I ran the code as administrator. Have you managed to get it working?

ShayBox commented 1 year ago

You have to run the compiled .exe directly, not through a terminal or editor, that allowed for using the HWND without access denied