Extract icon from Windows.lnk (shortcut)

I need to extract the icon from the Windows shortcut file (.lnk) (or find the icon file if it is just indicated by the shortcut).

I am not asking about extracting icons from exe, dll, etc. The shortcut in question is created when the installer starts. 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 the icon is located. But not one of the utilities that I have found addresses this - they all just go to .exe.

Many thanks!

+6
windows icons shortcut
source share
6 answers

This stream contains interesting information about the data contained in the .lnk file.

The sSHGetFileInfoss function must be able to retrieve the icon file.

Documented here and used for the 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 can create such a utility in C #, where you would declare this function as follows:

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

You can also create a utility in the autoit script language , where you should use this function, declared as follows:

 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 
+5
source share

Using Shell32's Supporting Bindings Method:

 String lnkPath = @"C:\Users\PriceR\Desktop\Microsoft Word 2010.lnk"; //--- run microsoft word var shl = new Shell32.Shell(); // Move this to class scope lnkPath = System.IO.Path.GetFullPath(lnkPath); var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath)); var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath)); var lnk = (Shell32.ShellLinkObject)itm.GetLink; //lnk.GetIconLocation(out strIcon); String strIcon; lnk.GetIconLocation(out strIcon); Icon awIcon = Icon.ExtractAssociatedIcon(strIcon); this.button1.Text = ""; this.button1.Image = awIcon.ToBitmap(); 
+5
source share

You can also analyze the .lnk file yourself, see this pdf or in this article about the details of the shortcut file format.

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

+1
source share

In 2010, Microsoft finally released the official LNK format specification . Of course, it is much more accurate and detailed than those that were deployed on the grid with reverse processing.

For completeness, here is a description of links and shortcuts on MSDN.

+1
source share

To add a few more resources to this, because presumably you are not using an application icon, not an icon that has a shortcut in the lower left corner:

0
source share

I use this to get an icon without a shortcut mini-arrow icon added on top of it as an image:

using IWshRuntimeLibrary;

 Image ShortcutIcon = System.Drawing.Icon.ExtractAssociatedIcon(((IWshShortcut)new WshShell().CreateShortcut(File)).TargetPath).ToBitmap(); 

If you want to get it as an icon:

 Icon ShortcutIcon = System.Drawing.Icon.ExtractAssociatedIcon(((IWshShortcut)new WshShell().CreateShortcut(File)).TargetPath); 
0
source share

All Articles