Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

How do I know the associated icon number of a file?


  • Please log in to reply
8 replies to this topic
  • Guests
  • Last active:
  • Joined: --
Executable files are usually use #1 for the icon resource number; however, other files do not use #1. So how do I know which icon number a specified file is using?

For example, in Windows 7, from the Windows start menu, I go to Games and drag&drop the Chess Titans icon onto the desktop. The shortcut file has the icon of Chess Titan but how can I know the icon resource information for it? If I right click on it and choose "Open file location," it opens the Game control panel listing other built-in Windows games. However, the actual executable for Chess Titan is located at "C:\Program Files\Microsoft Games\Chess\chess.exe" and the icon can be retrieved with the path and the resource number of #1.

Also, I'd like to know where the icons are specified for files such as .ahk, .chm or any files that have extensions.

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
FileGetShortcut may help you. Btw, don't do executables use #0?

Icons for other file types are stored in the registry, in HKEY_CLASSES_ROOT.

For example for AutoHotkey:
HKCR\.ahk (Default) ; contains "AutoHotkeyScript"
HKCR\AutoHotkeyScript\DefaultIcon (Default) ; contains path + icon number
This may differ for other extensions. For example, it could be directly in HKCR\.[EXT]\DefaultIcon (I think so), and apps like VisualStudio contain a shell extension to define the icon dynamically.
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

  • Guests
  • Last active:
  • Joined: --

FileGetShortcut may help you.

The command does not work for all shortcut files. It does not retrieve the target path with the shortcut file of Chess Titan described in the initial post.

Btw, don't do executables use #0?

I don't know. Just the #1 works most of the time.

This may differ for other extensions. For example, it could be directly in HKCR\.[EXT]\DefaultIcon (I think so), and apps like VisualStudio contain a shell extension to define the icon dynamically.

That's the problem. .chm also has the value, chm.file but there is no registroy path with chm.file.

I need some stable way to retrieve the icon path from a given file. Thanks for your input.

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
SHGetFileInfo is another way (more complicated). However, you don't get the path but the hIcon (which you could save or display).

Edit: It seems you can get the path, too. Don't know what this would return in case of a VisualStudio file (e.g. *.sln).
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

  • Guests
  • Last active:
  • Joined: --
How do I use SHGetFileInfo? I cannot figure out what to pass to the parameters.

FileSelectFile, shortcutfile, 32,, Pick a shortcut to analyze., Shortcuts (*.lnk)
if shortcutfile =
    return

;DllCall("MessageBox", "Int", "0", "Str", "Press Yes or No", "Str", "Title of box", "Int", 4)

DllCall("SHGetFileInfo", "Ptr", &shorcutfile, "Ptr", ....)	

/*
	DWORD_PTR SHGetFileInfo(
	  __in     LPCTSTR pszPath,
	  DWORD dwFileAttributes,
	  __inout  SHFILEINFO *psfi,
	  UINT cbFileInfo,
	  UINT uFlags
	);
*/


maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011

How do I use SHGetFileInfo? I cannot figure out what to pass to the parameters.

Good question. I recommend you reading through the Structures tutorial by Uberi.

But for this special case I couldn't figure it out either :(
My attempt (using HotKeyIt's Struct class):
#include Struct.ahk
#include sizeof.ahk

SHGFI_ICON := 0x000000100
SHGFI_ICONLOCATION := 0x000001000
MAX_PATH := 260

Flags := SHGFI_ICON|SHGFI_ICONLOCATION

definition := "HICON hIcon, int iIcon, DWORD dwAttributes, TCHAR szDisplayName[" MAX_PATH "], TCHAR szTypeName[80]"
fileinfo := new _Struct(definition)

FileSelectFile, shortcutfile, 32,, Pick a shortcut to analyze., Shortcuts (*.lnk)

MsgBox % "Returns: " DllCall("SHGetFileInfoW", "str", shortcutfile, "uint", 0, "Ptr", fileinfo[""], "uint", sizeof(fileinfo), "uint", Flags)
MsgBox % "hIcon: " fileinfo.hIcon
MsgBox % "Icon Index: " fileinfo.iIcon
MsgBox % "Icon Path: " fileinfo.szDisplayName

Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
I just found an example of SHGetFileInfo: <!-- m -->http://www.autohotke... ... 30&start=2<!-- m -->
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

  • Guests
  • Last active:
  • Joined: --
The link you provided solved the issue. Thanks.

  • Guests
  • Last active:
  • Joined: --
In case somebody needs a working example of SHGetFileInfo: (requrires the DllStruct library)
#Noenv
#include <Class_DllStruct>
/*
typedef struct _SHFILEINFO {
  HICON hIcon;
  int   iIcon;
  DWORD dwAttributes;
  TCHAR szDisplayName[MAX_PATH];
  TCHAR szTypeName[80];
} SHFILEINFO;
*/

MAX_PATH := 260
def__SHFILEINFO := "
(LTrim Join
  HICON hIcon;
  int   iIcon;
  DWORD dwAttributes;
  TCHAR szDisplayName[" MAX_PATH "];
  TCHAR szTypeName[80];
)"
SHGFI_ICON := 0x000000100
SHGFI_ICONLOCATION := 0x000001000
Flags := SHGFI_ICON|SHGFI_ICONLOCATION 
_SHFILEINFO := new DllStruct(def__SHFILEINFO)
; msgbox % _SHFILEINFO.GetSize()

FileSelectFile, shortcutfile, 32,, Pick a shortcut to analyze., Shortcuts (*.lnk)
DllCall("Shell32\SHGetFileInfo" (A_IsUnicode ? "W":"A"), "str", shortcutfile
			, "uint", 0, "Ptr", _SHFILEINFO.GetPtr(), "uint", _SHFILEINFO.GetSize(), "uint", Flags)
IconInfo := "hIcon:`t" _SHFILEINFO.hIcon
	. "`nIcon Index`t" _SHFILEINFO.iIcon
	. "`nIcon Path:`t" _SHFILEINFO.szDisplayName
	. "`nIcon Type:`t" _SHFILEINFO.szTypeName
	
Gui, Add, Text,, % IconInfo
Gui, Margin, 20, 10
Gui, Add, Text, w32 h32 hwndmypic1 0x3 ; 0x3 = SS_ICON
SendMessage, STM_SETICON := 0x0170, % _SHFILEINFO.hIcon, 0,, Ahk_ID %mypic1%
Gui, Show