Using the function below, i can get icon from a file to imagelist, but when
exit my program, system will pop up a error box of "Memory can not Read",
why? My system is Windows XP & sp2
*************** *************** *************** *************** *************** **********
<Flags()> Private Enum SHGFI
SmallIcon = &H1
LargeIcon = &H0
Icon = &H100
DisplayName = &H200
Typename = &H400
SysIconIndex = &H4000
UseFileAttribut es = &H10
End Enum
Public Enum IconSize
SmallIcon = 1
LargeIcon = 0
End Enum
<StructLayout(L ayoutKind.Seque ntial)> _
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(Unma nagedType.LPStr , SizeConst:=260) > Public
szDisplayName As String
<MarshalAs(Unma nagedType.LPStr , SizeConst:=80)> Public
szTypeName As String
Public Sub New(ByVal B As Boolean)
hIcon = IntPtr.Zero
iIcon = 0
dwAttributes = 0
szDisplayName = vbNullString
szTypeName = vbNullString
End Sub
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal
pszPath As String, ByVal dwFileAttribute s As Integer, ByRef psfi As
SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlagsn As SHGFI) As Integer
'Get the icon
Public Shared Function GetDefaultIcon( ByVal Path As String, Optional
ByVal IconSize As IconSize = IconSize.SmallI con, Optional ByVal SaveIconPath
As String = "") As Icon
Dim info As New SHFILEINFO(True )
Dim cbSizeInfo As Integer = Marshal.SizeOf( info)
Dim flags As SHGFI = SHGFI.Icon Or SHGFI.UseFileAt tributes
flags = flags + IconSize
SHGetFileInfo(P ath, 256, info, cbSizeInfo, flags)
GetDefaultIcon = Icon.FromHandle (info.hIcon)
If SaveIconPath <> "" Then
Dim FileStream As New IO.FileStream(S aveIconPath,
IO.FileMode.Cre ate)
GetDefaultIcon. Save(FileStream )
FileStream.Clos e()
End If
End Function
exit my program, system will pop up a error box of "Memory can not Read",
why? My system is Windows XP & sp2
*************** *************** *************** *************** *************** **********
<Flags()> Private Enum SHGFI
SmallIcon = &H1
LargeIcon = &H0
Icon = &H100
DisplayName = &H200
Typename = &H400
SysIconIndex = &H4000
UseFileAttribut es = &H10
End Enum
Public Enum IconSize
SmallIcon = 1
LargeIcon = 0
End Enum
<StructLayout(L ayoutKind.Seque ntial)> _
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(Unma nagedType.LPStr , SizeConst:=260) > Public
szDisplayName As String
<MarshalAs(Unma nagedType.LPStr , SizeConst:=80)> Public
szTypeName As String
Public Sub New(ByVal B As Boolean)
hIcon = IntPtr.Zero
iIcon = 0
dwAttributes = 0
szDisplayName = vbNullString
szTypeName = vbNullString
End Sub
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal
pszPath As String, ByVal dwFileAttribute s As Integer, ByRef psfi As
SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlagsn As SHGFI) As Integer
'Get the icon
Public Shared Function GetDefaultIcon( ByVal Path As String, Optional
ByVal IconSize As IconSize = IconSize.SmallI con, Optional ByVal SaveIconPath
As String = "") As Icon
Dim info As New SHFILEINFO(True )
Dim cbSizeInfo As Integer = Marshal.SizeOf( info)
Dim flags As SHGFI = SHGFI.Icon Or SHGFI.UseFileAt tributes
flags = flags + IconSize
SHGetFileInfo(P ath, 256, info, cbSizeInfo, flags)
GetDefaultIcon = Icon.FromHandle (info.hIcon)
If SaveIconPath <> "" Then
Dim FileStream As New IO.FileStream(S aveIconPath,
IO.FileMode.Cre ate)
GetDefaultIcon. Save(FileStream )
FileStream.Clos e()
End If
End Function
Comment