How to get a file's associated icon.

By: mykle hoban

Abstract: This FAQ describes how to retrieve a file's associated default icon without (directly) using the registry.


How can I programatically get a file's associated default icon without looking it up in the registry and loading it explicitly?


This is a lot easier than most people think. There is a relatively little-known function called ExtractAssociatedIcon. The prototype is as follows:

HICON ExtractAssociatedIcon(
    HINSTANCE hInst, // application instance handle
    LPTSTR lpIconPath, // path and filename of file for which icon is wanted
    LPWORD lpiIcon // pointer to icon index


void __fastcall TForm1::Button1Click(TObject *Sender)
  HICON ico;
  WORD idx;
  ico = ExtractAssociatedIcon(HInstance,"c:WINNT\\system32\\ntimage.gif",&idx);
  Image1->Picture->Icon->Handle = ico;

Server Response from: ETNASC04