Getting the associated icon of a file extension

This article introduces a function you can use to get the associated icon of a file or document based on its file extension (i.e. it doesn't matter if the file or document exists on disk or not)

ExtractAssociatedIcon

To get the icon of an application or document we can use this API function (declared in the ShellAPI unit):

function ExtractAssociatedIcon(hInst: HINST; lpIconPath: PChar;

var lpiIcon: Word): HICON; stdcall;

hInst: The application handle. This value is contained in the predefined variable HInstance.

lpIconPath: A pointer to a character buffer that should contain a null terminated string with the full path name of the application, library (DLL) or document. If it is a document, the function will place there the full pathname of the associated application from where the icon was extracted, so we should allocate a buffer large enough.

lpiIcon: The icon index (the first icon in the file has an index of 0). If lpIconPath specifies a document, then lpiIcon is set by the function (that's why it is passed by reference) to the index position of the actual icon taken from the associated executable (defined in the file association).

Return value: If the function fails, it returns 0. If it succeeds, it returns an icon handle, which is an integer value Windows uses to identify the allocated resource. It is not necessary to call the API DestroyIcon to release the icon since it'll be deallocated automatically when the application finishes, although you can do it if you want.

SAMPLE CALL

Now, what do we do with the icon handle? Normally what we want is an icon, namely and instance of the TIcon class. All we have to do is create a TIcon object and assign this handle to its Handle property. If later we assign the Handle property to another value, the previous icon will be automatically be released. The same happens if the TIcon object is freed.

Here is an example that changes the icon of the form:

uses SysUtils, Windows, ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);

var

IconIndex: word;

Buffer: array[0..2048] of char;

IconHandle: HIcon;

begin

StrCopy(@Buffer, 'C:\Windows\Help\Windows.hlp');

IconIndex := 0;

IconHandle := ExtractAssociatedIcon(HInstance, Buffer, IconIndex);

if IconHandle <> 0 then

Icon.Handle := IconHandle;

end;

GETASSOCIATEDICON

Unfortunately, ExtractAssociatedIcon fails if the file does not exist on disk, so we defined a procedure that gets the icon of a file whether it exists or not, and can also get the small icon (ideal for a TListView that can be shown in vsIcon or vsReport view styles). The procedure receives three parameters: the filename and two pointers to HICON (integer) variables: one for the large icon (32x32) and another one for the small icon (16x16). Any of them can be nil if you don't need one of these icons. The icons "returned" by the procedure must be freed with the DestroyIcon API. This will be done automatically if you assign the icon handle (HICON) to the Handle property of a TIcon object (the icon will be released when this object gets freed or a new value is assigned to it).