3

I'm planning on building a hotkey-activated application launcher for Windows. I intend for it to be a pop-up grid of icons in which you can then click and launch what you need. I'd like for it to automatically scan the Start Menu and Desktop for program shortcuts and catalog them. However, I'm not sure of how to go about the icon retrieval process from the shortcuts/actual binaries and I was wondering if there are any libraries for C/C++ that handle this sort of thing? If not, how would I go about it otherwise?

1
  • An executable file can contain many icons - you specifically want the one that Windows displays in the Start menu or Explorer, correct? Commented Jun 23, 2011 at 18:58

5 Answers 5

5

I think you want to use ExtractAssociatedIcon

See http://msdn.microsoft.com/en-us/library/ms648067%28v=VS.85%29.aspx

2
  • 1
    I'm not an expert on this, but the ExtractIcon function looks like it might also be a good function to check out. Commented Jun 23, 2011 at 18:57
  • Nice! This looks like exactly what I need. I assume the index would be 0 for the icon I need right? Commented Jun 23, 2011 at 22:06
1

resources extract is one such tool which extracts images from dll/ocx/exe files.

Well, if you do not want to use a closed source application, here is something with source, Icon Extractor

1
  • Interesting. I'd rather not use this as I'd have to be dependent on an external program and someone else's closed source code at that, but I think it'll work as a last resort if I can't find anything else. Thanks for the suggestion! Commented Jun 23, 2011 at 18:50
1
  1. LoadLibraryEx - use LOAD_LIBRARY_AS_DATAFILE or LOAD_LIBRARY_AS_IMAGE_RESOURCE
  2. EnumResourceNames - to find the resource
  3. LoadImage/LoadIcon - to load the image/icon
1

ExtractIconEx. Full source code is in my open source project, file named icon.cpp. It supports expansion of system variables and getting a icon from an index, like %SYSTEMROOT%\system32\shell32.dll,43

Here is the guts of it :

HICON GoFindAnIcon(LPCTSTR path)
{
    HICON icon = 0;

    //not using our parent's icon
    if(_tcsicmp(L"parent", path))
    {
        icon = (HICON)LoadImage(0, path, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_LOADFROMFILE|LR_LOADMAP3DCOLORS);
        if(!icon)
        {
            //Try something else
            TCHAR mypath[MAX_PATH];
            const TCHAR *cleanpath = path;
            const TCHAR *comma;             

            comma = _tcsrchr(path, ',');
            UINT index = 1;

            if(comma)
            {
                _tcsncpy(mypath, path, comma-path); //Can you exploit this buffer overflow ?
                mypath[comma-path] = TCHAR(0);

                index = _ttoi(comma+1);

                cleanpath = mypath;
            }

            ExtractIconEx(cleanpath, index, 0, &icon, 1);
        }
    }
    else
    {
        icon = GetParentProcessIcon();
    }

    return icon;
}
1

You can use ExctractIconEx to load the specified icon from the executable. This article suggests checking for icon overrides in the registry at HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons if compatibility with the shell is desired.

Not the answer you're looking for? Browse other questions tagged or ask your own question.