extract all icons of a file

Use LoadLibraryEx to load the file, EnumResourceNames to enumerate icons, and CreateIconFromResourceEx to lead each icon.

Note that driving a class from std::vector and other C++ Standard Library containers is not recommended.

The example below uses LR_SHARED, you might want to change that.

#include <windows.h>
#include <vector>

BOOL CALLBACK EnumIcons(HMODULE hmodule, LPCTSTR type, LPTSTR lpszName, 
    LONG_PTR ptr)
{
    if (!ptr)
        return FALSE;
    auto pvec = (std::vector<HICON>*)ptr;
    auto hRes = FindResource(hmodule, lpszName, type);
    if (!hRes)
        return TRUE;
    auto size = SizeofResource(hmodule, hRes);
    auto hg = LoadResource(hmodule, hRes);
    if (!hg)
        return TRUE;
    auto bytes = (BYTE*)LockResource(hg);
    auto hicon = CreateIconFromResourceEx(bytes, size, TRUE, 0x00030000, 
        0, 0, LR_SHARED);
    if (hicon)
        pvec->push_back(hicon);
    return TRUE;
}

int main()
{
    std::vector<HICON> vec;
    const char* modulepath = "file.exe";
    HMODULE hmodule = LoadLibraryEx(modulepath, NULL,
        LOAD_LIBRARY_AS_IMAGE_RESOURCE);
    if (!hmodule)
        return 0;

    EnumResourceNames(hmodule, RT_ICON,(ENUMRESNAMEPROC)EnumIcons,(LONG_PTR)&vec);
    for (auto e : vec)
    {
        ICONINFOEX ii = { sizeof(ii) };
        if (!GetIconInfoEx(e, &ii) || !ii.hbmColor)
            continue;
        BITMAP bm;
        GetObject(ii.hbmColor, sizeof(bm), &bm);
        printf("%d %d %d\n", bm.bmWidth, bm.bmHeight, bm.bmBitsPixel);
    }

    //free icons...
    return 0;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top