qdot / systray-rs

Allows rust applications to show a platform specific system tray icon and menu.
BSD 3-Clause "New" or "Revised" License
170 stars 66 forks source link

Windows: Possible problem with set_icon_from_buffer (with possible fix) #44

Open Cinimajig opened 4 years ago

Cinimajig commented 4 years ago

I wanted to use set_icon_from_buffer, with an embeded icon from the incude_bytes! macro, but i kept getting the Error "Cannot load icon from the buffer: 0". The icon worked fine with set_icon_from_file, so that wasnt the issue.

After looking at the code I looked up some of the documentaion on the Windows API and discovered a mistake with the use of CreateIconFromResourceEx (https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createiconfromresourceex). The second argument should be the length of the first arguments i bytes, but in the module it was set to 0.

After chaning this (/src/api/win32/mod.rs):

let icon_data = &buffer[offset as usize..];
let hicon = unsafe {
    winuser::CreateIconFromResourceEx(
        icon_data.as_ptr() as PBYTE,
        0,
        TRUE,
        0x30000,
        width as i32,
        height as i32,
        LR_DEFAULTCOLOR,
    )
};

to this:

let icon_data = &buffer[offset as usize..];
let hicon = unsafe {
    winuser::CreateIconFromResourceEx(
        icon_data.as_ptr() as PBYTE,
        icon_data.len() as u32,
        TRUE,
        0x30000,
        width as i32,
        height as i32,
        LR_DEFAULTCOLOR,
    )
};

It worked like a charm! I don't know if anyone else have this issue, but in my case I had to do it.

nicolasbauw commented 4 years ago

I indeed have this issue, too. This fix works fine with ICO files. See PR #45.