Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Extract Associated Icon/IntPtr to ImageList/Error in docs???

54 views
Skip to first unread message

Jason Sellers

unread,
Jan 29, 2002, 1:17:03 PM1/29/02
to
I'm trying to get the associated Icon from a file in the file system.
Anyone have any suggestions on a better way to do this?

If not... I'm able to get the SHGetFileInfo example from the mvps link below
ported to C#... almost. SHGetFileInfo returns a pointer to an ImageList as
a long. I'm able to create an IntPtr from the long, but how do I get that
to an instance of an ImageList? In the docs for the (read-only) Handle
property of ImageList, it says, "This corresponds to a Win32 HIMAGELIST
handle. Unless you provided the handle in the constructor, the handle is not
created until you need to use it." But there is no constructor that takes a
pointer... ???

Any ideas?
Thanks!
jason

"adam" <nos...@msdn-beacon-ct.co.uk> wrote in message
news:exyEBR2oBHA.2564@tkmsftngp04...
> I've written some code to get the Icon from a file based on its type using
> the ExtractAssociatedIcon API from Shell32.dll, it works rather nicely
> except that I am getting the 32x32 icons.
>
> I have looked around and this seems to have been solved by enlarge using
> SHGetFileInfo as explained at
> http://www.mvps.org/vbnet/index.html?code/icon/icon1632simple.htm.
>
> It strikes me that there must be some new .net way to do this as in XP
icons
> are expected to have some where in the region of 9 images.
>
> So, how's it done?
>
> adam
>
>
>


Patrick Steele [MVP]

unread,
Jan 29, 2002, 5:18:41 PM1/29/02
to
In article <OubQuDPqBHA.2304@tkmsftngp07> (from Jason Sellers
<jas...@act.com>),

> I'm trying to get the associated Icon from a file in the file system.
> Anyone have any suggestions on a better way to do this?
>
> If not... I'm able to get the SHGetFileInfo example from the mvps link below
> ported to C#... almost. SHGetFileInfo returns a pointer to an ImageList as
> a long. I'm able to create an IntPtr from the long, but how do I get that
> to an instance of an ImageList? In the docs for the (read-only) Handle
> property of ImageList, it says, "This corresponds to a Win32 HIMAGELIST
> handle. Unless you provided the handle in the constructor, the handle is not
> created until you need to use it." But there is no constructor that takes a
> pointer... ???

Here's a snippet of code I've got:

public const uint SHGFI_USEFILEATTRIBUTES = 0x4000;
public const uint FILE_ATTRIBUTRE_NORMAL = 0x4000;
public const uint SHGFI_SYSICONINDEX = 0x4000;
public const uint ILD_TRANSPARENT = 0x1;
public const uint SHGFI_ICON = 0x1; // large icon
public const uint SHGFI_LARGEICON = 0x0;// large icon
public const uint SHGFI_SHELLICONSIZE = 0x4;
public const uint SHGFI_SMALLICON = 0x1; // small icon
public const uint SHGFI_TYPENAME = 0x400;


[DllImport("Shell32.dll")]
public static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbfileInfo,
uint uFlags
);

public struct SHFILEINFO
{
public long hIcon;
public int iIcon;
int dwAttributes;
string szDisplayName;
string szTypeName;
}

int handle = SHGetFileInfo
(fileName,veShell.FILE_ATTRIBUTRE_NORMAL,ref info,
System.Convert.ToUInt32(cbFileInfo), uFlags)
unsafe
{
IntPtr pointer = new IntPtr(handle);
}
System.Drawing.Icon ico = System.Drawing.Icon.FromHandle(pointer);
ImageList img = new ImageList();
img.Images.Add(ico);

--
Patrick Steele
Microsoft .NET MVP

Mattias Sjögren

unread,
Jan 30, 2002, 5:00:36 PM1/30/02
to
Jason,

>I've attached my code. I seem to be getting the Icon correctly, but when I
>try to draw it I'm getting a divide by zero exception. Any ideas?

Declare the return value of SHGetImageList as IntPtr rather than int.
You can also remove the unsafe block in the calling code.

Forget about that BASIC_SHGFI_FLAGS thing. If all you want is the
icon, simply pass (SHGFI_ICON | SHGFI_SMALLICON) or (SHGFI_ICON |
SHGFI_LARGEICON) to the function, depending on which icon you want.
The value of SHGFI_ICON is 0x100.

The icon handle is returned in the shinfo.hIcon member, not the return
value.


Mattias

===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/

adam

unread,
Jan 31, 2002, 9:15:36 AM1/31/02
to
As I was reading this originally through google I've been a bit behind so
developed a standalone class. Just call ExtractIcon.GetIcon();

adam

using System;
using System.Runtime.InteropServices;
using System.Drawing;

/// <summary>
/// Summary description for ExtractIcon.
/// </summary>
public class ExtractIcon
{
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo


(
string pszPath,
uint dwFileAttributes,

out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
);

[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon=IntPtr.Zero;iIcon=0;dwAttributes=0;szDisplayName="";szTypeName="";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]
public string szTypeName;
};

private ExtractIcon()
{
}

private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}

/// <summary>
/// Get the associated Icon for a file or application, this method always
returns
/// an icon. If the strPath is invalid or there is no idonc the default
icon is returned
/// </summary>
/// <param name="strPath">full path to the file</param>
/// <param name="bSmall">if true, the 16x16 icon is returned otherwise the
32x32</param>
/// <returns></returns>
public static Icon GetIcon(string strPath, bool bSmall)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (bSmall)
flags = SHGFI.Icon|SHGFI.SmallIcon|SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon|SHGFI.LargeIcon|SHGFI.UseFileAttributes;

SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
}


0 new messages