Using Shell-Icons?!

Posted on 2006-05-01
Last Modified: 2011-10-03

i wrote a component (in delphi but that is not important), that displays for example all files in a folder. How can i use the explorer-like-icons for file-extensions in my application?

Maybe there is a routine like GetIconFromFile[Extension](char* File):HBITMAP... or something like this???

Thanks for advance!
Question by:LenWinSonSoft
    LVL 8

    Expert Comment

    Use ExtractIcon() or ExtractIconEx()
    LVL 86

    Expert Comment

    See also ("Icons in Win32"):

    // Load the DLL/EXE without executing its code
    hLib = LoadLibraryEx( szFileName, NULL, LOAD_LIBRARY_AS_DATAFILE );
    // Find the group resource which lists its images
    hRsrc = FindResource( hLib, MAKEINTRESOURCE( nId ), RT_GROUP_ICON );
    // Load and Lock to get a pointer to a GRPICONDIR
    hGlobal = LoadResource( hLib, hRsrc );
    lpGrpIconDir = LockResource( hGlobal );
    // Using an ID from the group, Find, Load and Lock the RT_ICON
    hRsrc = FindResource( hLib, MAKEINTRESOURCE( lpGrpIconDir->idEntries[0].nID ),
                          RT_ICON );
    hGlobal = LoadResource( hLib, hRsrc );
    lpIconImage = LockResource( hGlobal );
    // Here, lpIconImage points to an ICONIMAGE structure

    as well as ("Using Icons"):

    HICON hIcon1;       // icon handle
    HINSTANCE hExe;     // handle to loaded .EXE file
    HRSRC hResource;    // handle for FindResource  
    HRSRC hMem;         // handle for LoadResource
    BYTE *lpResource;   // pointer to resource data  
    int nID;            // ID of resource that best fits current screen
    HDC hdc;        // handle to display context
    // Load the file from which to copy the icon.
    // Note: LoadLibrary should have a fully explicit path.
    hExe = LoadLibrary("myapp.exe");
    if (hExe == NULL)
        //Error loading module -- fail as securely as possible
    // Find the icon directory whose identifier is 440.
    hResource = FindResource(hExe,
    // Load and lock the icon directory.
    hMem = LoadResource(hExe, hResource);
    lpResource = LockResource(hMem);
    // Get the identifier of the icon that is most appropriate
    // for the video display.
    nID = LookupIconIdFromDirectoryEx((PBYTE) lpResource, TRUE,
    // Find the bits for the nID icon.
    hResource = FindResource(hExe,
    // Load and lock the icon.
    hMem = LoadResource(hExe, hResource);
    lpResource = LockResource(hMem);
    // Create a handle to the icon.
    hIcon1 = CreateIconFromResourceEx((PBYTE) lpResource,
        SizeofResource(hExe, hResource), TRUE, 0x00030000,
    // Draw the icon in the client area.
    DrawIcon(hdc, 10, 20, hIcon1);

    Author Comment


    That is for the individual icons of any exe... Am I right?
    Thats also what i need...

    But more important is that all this default extensions like ".txt", ".dll", ".inf", ".cfg", etc.... are visible with their explorer-like-icons...

    Do you have any idea how to realize this?

    Thanks for advance...
    LVL 86

    Expert Comment

    >>That is for the individual icons of any exe... Am I right?


    >>But more important is that all this default extensions like ".txt", ".dll", ".inf", ".cfg", etc.... are visible
    >>with their explorer-like-icons...

    You need to look them up in the registry before loading them with the above code. E.g. '.txt' is listed in


    as 'txtfile'. Under HKEY_CLASSES_ROOT\txtfile\DefaultIcon, you'll then find where to load the icon from.

    OK, that's the long route, BUT:

    Alternatively, you can call 'SHGetFileInfo()' with 'SHGFI_ICON' to directly obtain the icon.
    LVL 86

    Assisted Solution

    BTW, see also ("How To Retrieve an Icon for a File Class Without a File"):

       SHFILEINFO sfi;
       HIMAGELIST hil;

       hil = SHGetFileInfo(".zip",

       // hil now contains the handle to the system image list
       // containing the large icon images.
       // sfi.hIcon contains the large icon for the file.
    LVL 8

    Expert Comment

    LVL 8

    Accepted Solution

    ExtractAssociatedIcon() behaves like Windows - if there's icon in file it uses it, otherwise it gets associated with file extension icon from registry.

    Author Comment

    Thanks for advance!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now