Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

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

Open
Cinimajig opened this issue Jul 6, 2020 · 1 comment

Comments

@Cinimajig
Copy link

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants