views:

41

answers:

2

Hi all,

i need to find the default icon of a windows executable (PE file = dll, exe, com..) programatically. I do know how to walk throught the resources and identify what is an icon, what a cursor etc, but as far as i know none of the icons is in any way marked as the default one. So, does somebody know, how to find the default icon? Moreover, i do not want to use any windows api call, i want to code the function myself. The problem is that i don't know which one of all the icons is the default one.

+2  A: 

The first one you find is the default one.

The default icon is simply the icon with the lowest id, so, by definition, is the first icon discovered when enumerating resources.

Chris Becke
Could you please tell me where did you find this information? I tried to google but found nothing relevant.
PeterK
+1, this is correct. You can find it here: http://stackoverflow.com/questions/3270757/in-resources-of-a-executable-file-how-does-one-find-the-default-icon
Hans Passant
The same place you did apparently. Of course, I interpret "ICON_GROUP" to mean "The icon", as the icon group is a collection of the individual device images in the source icon (ico file).As such, I stand by my claim - the first icon found, is the default one. And that will tend to be the one with the lowest id as most resource editors will structure the resource file with low id's first.
Chris Becke
@Chris Becke: technically you are correct, unfortunatelly the assumption that the icon with the lowest id will be the default one is not good enough for me (for reasons, look here: http://www.microsoft.com/technet/security/bulletin/ms05-002.mspx ). Anyway thanks for your answer!
PeterK
A: 

After a lot of searching, i found out that the default icon is not the one with the lowest id.

Windows use several sizes of one icon for various things. For more information, look here, but in short here is the important information:


When the system displays an icon, it must extract the appropriate icon image from the .exe or .dll file. The system uses the following steps to select the icon image:

  1. Select the RT_GROUP_ICON resource. If more than one such resource exists, the system uses the first resource listed in the resource scrip.

    • Select the appropriate RT_ICON image from the RT_GROUP_ICON resource. If more than one image exists, the system uses the following criteria to choose an image:

    • The image closest in size to the requested size is chosen.

    • If two or more images of that size are present, the one that matches the color depth of the display is chosen.

    • If no images exactly match the color depth of the display, the image with the greatest color depth that does not exceed the color depth of the display is chosen. If all exceed the color depth, the one with the lowest color depth is chosen.

Note: The system treats all color depths of 8 or more bpp as equal. Therefore, there is no advantage of including a 16x16 256-color image and a 16x16 16-color image in the same resource — the system will simply choose the first one it encounters. When the display is in 8-bpp mode, the system will choose a 16-color icon over a 256-color icon, and will display all icons using the system default palette.


Since the requested size is 16x16 (because thats the system small icon size, ie. the default icon size) i think we can say that the default icon is the icon from the first icon group which has the smallest size (no smaller icon than 16x16 can exist) with the highest color depth.

EDIT: a small correction. A icon of size smaller than 16x16 might apparently be in the resources, but that indicates that the file does not have a default icon and the system then does supply its own icon instead.

PeterK