Load HICON from the buffer (*.ico file)

后端 未结 2 581
南方客
南方客 2021-01-16 11:58

I\'m just wondering, if there an API in Windows for loading the HICON from the byte array (buffer)? Let\'s say that I downloaded an *.ico file and

2条回答
  •  借酒劲吻你
    2021-01-16 12:13

    CreateIconFromResourceEx((PBYTE)data + offset, 0, ...)
    

    The second parameter should not be zero. Windows doesn't know how far it can read the buffer without causing buffer overflow. Apparently Windows does allow for this error in some cases, but maybe it's not prepared in the case of PNG files, it stops when it doesn't see a BITMAPINFOHEADER

    Just provide the maximum available buffer size, that should solve the problem with PNG files:

    CreateIconFromResourceEx((PBYTE)data + offset, fsize - offset, ...)
    

    Documentation says that LookupIconIdFromDirectoryEx expects resource data. This API does appear to work with icon files, it returns the offset for the first icon. Either way it doesn't appear to have a bug based on what the documentation says.

    It's better to calculate the offset manually. It appears you already know how to calculate the offset values. You can simply calculate the offset as follows, based on ICONDIRENTRY

    WORD icon_count = 0;
    fseek(f, 2 * sizeof(WORD), SEEK_SET);
    fread(&icon_count, sizeof(WORD), 1, f);
    int offset = 3 * sizeof(WORD) + sizeof(ICONDIRENTRY) * icon_count;
    

    sizeof(ICONDIRENTRY) is 16. The icon file starts with 3 WORD values, the third value is icon_count, followed sizeof(ICONDIRENTRY) * icon_count bytes, followed by the bytes for the first HICON

提交回复
热议问题