A little FileIconInit for the System Image listI can hear the theme for Welcome Back Kotter as I write this. It seems that things have been quiet on here for quite sometime, however those that know me can attest to the fact that life away from here has been anything but that. Thankfully, things have returned to a point where a lot more content should find its way onto this world.
As things progress, I am doing a substantial amount of development using C# and the .NET Framework. I still continue to develop new utilities that satisfy my specific needs. I also try to update some of the available utilities. The most recent update has been to ASysIcon (this application was redeveloped using the .NET Framework). I've done quite a bit of work with icons in the past (also take a look at AIconExtract) and ASysIcon is a utility that displays the images found in the system image list. The system image list contains the icons that are associated with the different registered file types on a computer.
The retrieval of the system image list is a straight forward process. There are a few unmanaged API calls that work quite well for this task. One thing that may not be so apparent is that the the images retrieved will be those images that are cached in the system image list. If the system hasn't had a need to load them, well, they won't be in the list. This could pose some concern if you would like to retrieve the entire list. The trick is to get all the system images cached. Actually, this isn't really a trick. With little searching the FileIconInit function surfaces. This function will initialize or reinitialize this image list. FileIconInit has a boolean parameter determines if it should restore the image cache from disk. Passing true as this parameter will load all of the images, not just the recently cached ones. The following sample code shows how to display the system image list in a ListView:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices; // http://support.microsoft.com/kb/319350
namespace SysIcons1
...{
public partial class Form1 : Form
...{
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
...{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
//Retrieve the handle to the icon that represents the file and the index of the icon within the system image list
public const uint SHGFI_ICON = 0x100;
// Retrieve the index of a system image list icon.
public const uint SHGFI_SYSICONINDEX = 0x4000;
// Large icon
public const uint SHGFI_LARGEICON = 0x0;
// Small icon
public const uint SHGFI_SMALLICON = 0x1;
public const uint LVM_FIRST = 4096;
public const uint LVSIL_NORMAL = 0;
public const uint LVSIL_SMALL = 1;
public const uint LVM_SETIMAGELIST = LVM_FIRST + 3;
// need this style on Windows 9x
public const uint LVS_SHAREIMAGELISTS = 0x0040;
// Retrieves information about an object in the file system, such as a file, folder, directory, or drive root.
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
// The SendMessage function sends the specified message to a window or windows.
[DllImport("user32.dll")]
private static extern UInt32 SendMessage(IntPtr hWnd, UInt32 Msg,
UInt32 wParam, UInt32 lParam);
// Destroys an icon and frees any memory the icon occupied.
[DllImport("User32.dll")]
private static extern int DestroyIcon(System.IntPtr hIcon);
// Initializes or reinitializes the system image list.
// FileIconInit is not included in a header file. You must call it directly from Shell32.dll, using ordinal 660.
[DllImport( "Shell32.dll", EntryPoint = "#660")]
private static extern bool FileIconInit( bool fRestoreCache );
// Retrieves the number of images in an image list.
[DllImport( "comctl32.dll")]
private static extern int ImageList_GetImageCount( IntPtr himl);
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
IntPtr hImgSmall; //handle to the small system image list
IntPtr hImgLarge; //handle to the large system image list
SHFILEINFO shinfo = new SHFILEINFO();
FileIconInit(true);
//Small Icons
hImgSmall = SHGetFileInfo("", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_SYSICONINDEX SHGFI_SMALLICON);
//Large Icons
hImgLarge = SHGetFileInfo("", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_SYSICONINDEX SHGFI_LARGEICON);
// Set Icon Images
SendMessage(listView1.Handle, LVM_SETIMAGELIST, LVSIL_SMALL, (uint)hImgSmall.ToInt32());
SendMessage(listView1.Handle, LVM_SETIMAGELIST, LVSIL_NORMAL, (uint)hImgLarge.ToInt32());
for ( int i = 0; i < ImageList_GetImageCount(hImgLarge) ; i++ )
...{
listView1.Items.Add(i.ToString(),i);
}
}
private int Get_Image_Index(string filename)
...{
IntPtr hImgSmall;
SHFILEINFO shinfo = new SHFILEINFO();
hImgSmall = SHGetFileInfo(filename, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo.GetType()), SHGFI_ICON SHGFI_SMALLICON);
DestroyIcon(shinfo.hIcon);
return (int)shinfo.iIcon;
}
}