0

I'm using SHGetFileInfo to get folders icons. All works fine except when invoking SHGetFileInfo on "My Computer" special folder - CLSID ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}. For that particular case after call to SHGetFileInfo the SHFILEINFO.hIcon struct has zero value. This happens on Windows 7. Earlier on WinXP the code worked fine.

The flags I use for invoking SHGetFileInfo are SHGFI_ICON and SHGFI_SMALLICON, so nothing fancy.

What may be the cause of this? How can I get the "My Computer" icon on Windows 7?

SiliconMind
  • 2,185
  • 4
  • 25
  • 49

2 Answers2

0

You will have to first get the PIDL of "My Computer" using SHGetSpecialFolderLocation and then pass this PIDL as the first parameter of SHGetFileInfo.

IntPtr pidl;
SHGetSpecialFolderLocation(0, CSIDL_DRIVES, pidl);
SHGetFileInfo(pidl, 0, shinfo, Marshal.SizeOf(shinfo), (SHGFI_PIDL | SHGFI_DISPLAYNAME | SHGFI_ICON | SHGFI_SMALLICON));
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Thanks, but `CSIDL` was replaced in VISTA by `KNOWNFOLDERID`. This requires some additional work to support both implementations. I've decided to use `ExtractIconEx` instead: http://stackoverflow.com/a/15947163/899092 – SiliconMind Apr 11 '13 at 11:07
  • I really wanted to use this... It looks like it would be perfect except one thing... The declare for SHGetFileInfo is public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags); ... The first param is a string and not a IntPtr... So how do you get that to work? Ref: http://pinvoke.net/default.aspx/shell32.SHGetFileInfo – Arvo Bowen Jan 17 '15 at 18:42
  • In addition just for reference... I found another answer on SO (ref: http://stackoverflow.com/a/3598417/1039753) that converts the "LPCTSTR" to a "[MarshalAs(UnmanagedType.LPWStr)] string". Still confused though, I would like to use it as a IntPtr. Please explain how you did that? – Arvo Bowen Jan 17 '15 at 18:54
0

I've decided to use ExtractIconEx and read MyComputer icon directly from shell32.dll (icon index 15). Unlike Win API it seems that the icon lists do not change :)

SiliconMind
  • 2,185
  • 4
  • 25
  • 49