views:

3227

answers:

3

I need to extract the icon from a windows shortcut (.lnk) file (or find the icon file, if it's just pointed to by the shortcut).

I'm not asking about extracting icons from exe's, dll's, etc. The shortcut in question is created when I run a installation program. And the icon displayed by the shortcut is not contained in the .exe that the shortcut points to. Presumably the icon is embedded in the .lnk file, or the .lnk file contains a pointer to where this icon lives. But none of the utilities I've found work address this -- they all just go to the .exe.

Many thanks!

+2  A: 

This thread provides interesting informations about the data contained in a .lnk file

The sSHGetFileInfoss function should be able to extract the icon file.

Documented here, and used for a lnk file:

Path2Link := 'C:\Stuff\TBear S Saver.lnk';
SHGetFileInfo(PChar(Path2Link), 0, ShInfo1, SizeOf(TSHFILEINFO),
          SHGFI_ICON);
// this ShInfo1.hIcon will have the Icon Handle for the Link Icon with
// the small ShortCut arrow added}

From the first link, you could build such an utility in c#, where you would declare this function like:

[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(
   string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, 
   uint cbSizeFileInfo, uint uFlags);


You could also built an utility in autoit script language, where you would use that function declared like this:

Func _ShellGetAssocIcon(Const $szFile,Const $IconFlags = 0)
    Local $tFileInfo = DllStructCreate($tagSHFILEINFO)
    If @error Then
        Return SetError(1,@extended,0)
    EndIf

    Local $Ret = DllCall("shell32.dll","int","SHGetFileInfo","str",$szFile,"dword",0, _
        "ptr",DllStructGetPtr($tFileInfo),"uint",DllStructGetSize($tFileInfo),"uint",BitOr($SHGFI_ICON,$IconFlags))
    MsgBox(0,0,@error)
    Return DllStructGetData($tFileInfo,"hIcon")
EndFunc
VonC
Maybe give him a little more info, like the struct etc format. But you got my vote.
Jonathan C Dickinson
+1  A: 

You can also parse the .lnk file yourself, see this pdf or this article on the details of the shortcut file format.

Or you can use the ShellLink class mentioned in the answer to this question.

Treb
A: 

To add a few more resources to this, because presumeably you wan't the Application's icon and not icon that has the shortcut in the bottom left corner:

Chris S