Ouput produced by C# version of this code:

* * *
The associated icon and the description for each file type are stored in the Registry.
To get these associates for DBF files, for example, the first step is to locate "HKEY_CLASSES_ROOT\.dbf" registry key. The default value for this key is "Visual.FoxPro.Table". Which means "HKEY_CLASSES_ROOT\Visual.FoxPro.Table" key must be located next.
The latter has the default value "Microsoft Visual FoxPro Table", which is the actual description that the OS sticks to DBF file type.
The "DefaultIcon" subkey for this key has value "C:\Program Files\Microsoft Visual FoxPro 9\vfp9.exe,-103". That means that Group Icon #103 resource exists in VFP9 executable.

This resource contains several icons that the OS uses for representing Visual FoxPro DBF files whenever required; for example, when displaying list of files in Explorer window.
A Resource Viewer shows this and other resources stored in VFP9 executable.

In a similar way any other file type (read "file extension") can be traced to an icon+description pair.
There is no single rule, and the ways of finding associations are tricky if not messy. Going this way would require rather extensive coding. Luckily, MS had bothered to hide the complexity of the process inside the SHGetFileInfo API call.
nBufsize=1024
cBuffer = REPLICATE(CHR(0), nBufsize)
nFlags = BITOR(SHGFI_SYSICONINDEX, SHGFI_SMALLICON,;
SHGFI_ICON, SHGFI_TYPENAME,
SHGFI_USEFILEATTRIBUTES)
nResult = SHGetFileInfo(m.cFilename,;
FILE_ATTRIBUTE_NORMAL,;
@cBuffer, nBufsize, nFlags)
For the specified cFilename, the call above populates the SHFILEINFO structure with valuable information. That includes the description for the file type as well as the associated icon presented as the index in the System Image List.
The OS creates the System Image List for each running process, with the process mainly responsible for populating the list with ListImage items via SHGetFileInfo calls and may be by some other means.
This is how the handle to the System Image List (HIMAGELIST) can be obtained.
cBuffer = REPLICATE(CHR(0), 1024)
hSysImageList = SHGetFileInfo("", FILE_ATTRIBUTE_NORMAL,;
@cBuffer, LEN(cBuffer),;
BITOR(SHGFI_SYSICONINDEX, SHGFI_SMALLICON,;
SHGFI_ICON, SHGFI_TYPENAME,;
SHGFI_USEFILEATTRIBUTES))
Such System Image List is very similar if not identical to the ImageList ActiveX control. That is why the list can be linked to Icons or SmallIcons property of a MS Common Control (ListView or TreeView) placed on VFP form, which hence allows this control to display images found in the System Image List.
* * *
As stated in MSDN, the SHGetFileInfo may not be the best way, even if the simplest, for retrieving associated icons.
The IExtractIcon Interface performs this task better. The Shell uses nothing else bu the the IExtractIcon for retrieving icons when displaying the contents of a folder.
* * *
Calling AssocQueryString API provides yet another way of obtaining the path to the associated icon as well as so called friendly names for the application and the file.

|