473,542 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

!Help with API call from vb. Must be trampling memory or something!

Hi all,

I really needed to get the icons associated with each file that I want to
show in a listview.

I used the follow modified code sniplets found on the internet.

I have left in commented code for anyone else who may be looking to do the
same.

I only care about the small icons in my program--detail view only.

Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
Public dwAttributes As Integer ' SFGAO flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName As
String
End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath
As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFileInfo, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80

Dim hImg As IntPtr
Dim shinfo As New SHFileInfo

' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon

hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)

' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName

'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function

from another method I do this

'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list
and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)

as you can see I get the filenames and then place them into an imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....

The above code is some how trampling memory. It shows up in strange ways,
systemexecutionexception or even stackoverflowexceptions

commenting out this call makes it run fine so I know the problem is there.

Do I need to somehow release this icon pointer or something?

If so how can I add the icon I want to the imagelist, and release this icon
ptr to it and still have the image available in the imagelist?

Please help me solve this. My app really needs it if I will show icons.

If there is a simpler way that definitely works, please let me know.
I tried many before this one.

It works great except for this.

Thanks,

Shane


Nov 20 '05 #1
5 2293
Hi,

Here is a Class that I use to get the icons.
Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon
representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.UnmanagedType.ByValTStr,
SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As
String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON = &H100

Private Const SHGFI_SMALLICON = &H1 'Small icon

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

-------------------------

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:eO**************@TK2MSFTNGP09.phx.gbl...
Hi all,

I really needed to get the icons associated with each file that I want to
show in a listview.

I used the follow modified code sniplets found on the internet.

I have left in commented code for anyone else who may be looking to do the
same.

I only care about the small icons in my program--detail view only.

Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
Public dwAttributes As Integer ' SFGAO flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName
As
String
End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal
pszPath
As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFileInfo, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON
Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80

Dim hImg As IntPtr
Dim shinfo As New SHFileInfo

' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon

hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)

' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName

'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function

from another method I do this

'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list
and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)

as you can see I get the filenames and then place them into an
imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....

The above code is some how trampling memory. It shows up in strange ways,
systemexecutionexception or even stackoverflowexceptions

commenting out this call makes it run fine so I know the problem is there.

Do I need to somehow release this icon pointer or something?

If so how can I add the icon I want to the imagelist, and release this
icon
ptr to it and still have the image available in the imagelist?

Please help me solve this. My app really needs it if I will show icons.

If there is a simpler way that definitely works, please let me know.
I tried many before this one.

It works great except for this.

Thanks,

Shane

Nov 20 '05 #2
Thank you Ken,

This seems to work.

Had to modify a little though.

I needed to get the icons via file extension.
That requires two more flags or it fails.

But with that added, works great.

The only different I see in what you have and what I had was the return
value of the api call
you used Integer and I had intptr.

That will probably do it.

Thanks a bunch. It was showing up in wierd places.

Shane
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:eL**************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a Class that I use to get the icons.
Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON = &H100

Private Const SHGFI_SMALLICON = &H1 'Small icon

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

-------------------------

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:eO**************@TK2MSFTNGP09.phx.gbl...
Hi all,

I really needed to get the icons associated with each file that I want to show in a listview.

I used the follow modified code sniplets found on the internet.

I have left in commented code for anyone else who may be looking to do the same.

I only care about the small icons in my program--detail view only.

Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
Public dwAttributes As Integer ' SFGAO flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName
As
String
End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal
pszPath
As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFileInfo, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON
Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80

Dim hImg As IntPtr
Dim shinfo As New SHFileInfo

' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon

hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)

' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName

'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function

from another method I do this

'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)

as you can see I get the filenames and then place them into an
imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....

The above code is some how trampling memory. It shows up in strange ways, systemexecutionexception or even stackoverflowexceptions

commenting out this call makes it run fine so I know the problem is there.
Do I need to somehow release this icon pointer or something?

If so how can I add the icon I want to the imagelist, and release this
icon
ptr to it and still have the image available in the imagelist?

Please help me solve this. My app really needs it if I will show icons.

If there is a simpler way that definitely works, please let me know.
I tried many before this one.

It works great except for this.

Thanks,

Shane


Nov 20 '05 #3
Ken,

I took you class and modified it to do what I need but still seem to have
problems. I thought it was working but I am still having
system.executionengine exceptions in a place down below the call into this
class. If I comment out the call all goes well.

The error always shows up in sharpziplib classes--but when I do the
following

dim IconGetter as new clsGetIcon
dim ico as icon

'get the icon
ico=getIconViaFileExtension(".EXE")

next I put the icon into the imagelist if it is not already in there.
(tracking that by an arraylist--what extensions already looked up)

if it getIconViaFileExtension call is left intact then I get all sorts of
bizarre errors later on down and usually in trying to get the datetime in
the ZipEntry class of SharpZipLib.

Somehow it is as if memory is getting stomped on. I get stackoverflow
execeptions and also sometimes the system.executionengine exception.
(your class modified for my needs--needed to be able to pass in an extension
("*.exe") or whatever--don't have the filename uncompressed. That seems to
require
these flags
SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_USEFILEATTRIBUTES)

Public Class clsGetIcon
Private Structure SHFILEINFO
Dim hIcon As IntPtr
Dim iIcon As Integer
Dim dwAttributes As Integer
<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, _
SizeConst:=260)> Public szDisplayName As String
'String that contains the name of the file as it appears in the
Microsoft®
'Windows® Shell, or the path and file name of the file that contains the
icon
'representing the file.
<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, _
SizeConst:=80)> Public szTypeName As String
End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As
String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const FILE_ATTRIBUTE_NORMAL = &H80

'if bGetSmallIcon is false, large is used
Public Function getIconForFile(ByVal szFilename As String, Optional ByVal
bGetSmallIcon As Boolean = True) As Icon
Const SMALL_DEFAULT_FLAGS As Integer = SHGFI_ICON Or SHGFI_SMALLICON
Const LARGE_DEFAULT_FLAGS As Integer = SHGFI_ICON Or SHGFI_LARGEICON
Try
Dim aSHFileInfo As New SHFILEINFO
Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)
If bGetSmallIcon Then
SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo,
SMALL_DEFAULT_FLAGS)
Else
SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo,
LARGE_DEFAULT_FLAGS)
End If

Dim myIcon As Icon
myIcon = Icon.FromHandle(aSHFileInfo.hIcon)
Return myIcon
Catch ex As Exception
Throw New Exception("There was a problem in getting the icon
associated with the file " & szFilename & ".", ex)
Debug.WriteLine(ex.ToString)
Return Nothing
End Try
End Function

'if bGetSmallIcon is false, large is used
Public Function getIconViaFileExtension(ByVal szFileExtension As String,
Optional ByVal bGetSmallIcon As Boolean = True) As Icon
Const SMALL_DEFAULT_FLAGS As Integer = SHGFI_TYPENAME Or SHGFI_ICON Or
SHGFI_SMALLICON Or SHGFI_USEFILEATTRIBUTES
Const LARGE_DEFAULT_FLAGS As Integer = SHGFI_TYPENAME Or SHGFI_ICON Or
SHGFI_LARGEICON Or SHGFI_USEFILEATTRIBUTES
Try
Dim aSHFileInfo As New SHFILEINFO
Dim cbFileInfo As Integer = Marshal.SizeOf(aSHFileInfo)
If bGetSmallIcon Then
SHGetFileInfo(szFileExtension, 0, aSHFileInfo, cbFileInfo,
SMALL_DEFAULT_FLAGS)
Else
SHGetFileInfo(szFileExtension, 0, aSHFileInfo, cbFileInfo,
LARGE_DEFAULT_FLAGS)
End If

Dim myIcon As Icon
myIcon = Icon.FromHandle(aSHFileInfo.hIcon)
Return myIcon
Catch ex As Exception
Throw New Exception("There was a problem in getting the icon
associated with the " & szFileExtension & " file extension.", ex)
Debug.WriteLine(ex.ToString)
Return Nothing
End Try
End Function
End Class

So again, is there something that I need to do with that icon--releasing
memory or some graphics release or something after getting it? What could
cause this problem?

Any help from you or ANYONE, would be greatly appreciated.

Shane
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:eL**************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a Class that I use to get the icons.
Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON = &H100

Private Const SHGFI_SMALLICON = &H1 'Small icon

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

-------------------------

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:eO**************@TK2MSFTNGP09.phx.gbl...
Hi all,

I really needed to get the icons associated with each file that I want to show in a listview.

I used the follow modified code sniplets found on the internet.

I have left in commented code for anyone else who may be looking to do the same.

I only care about the small icons in my program--detail view only.

Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
Public dwAttributes As Integer ' SFGAO flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName
As
String
End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal
pszPath
As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFileInfo, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON
Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80

Dim hImg As IntPtr
Dim shinfo As New SHFileInfo

' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon

hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)

' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName

'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function

from another method I do this

'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)

as you can see I get the filenames and then place them into an
imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....

The above code is some how trampling memory. It shows up in strange ways, systemexecutionexception or even stackoverflowexceptions

commenting out this call makes it run fine so I know the problem is there.
Do I need to somehow release this icon pointer or something?

If so how can I add the icon I want to the imagelist, and release this
icon
ptr to it and still have the image available in the imagelist?

Please help me solve this. My app really needs it if I will show icons.

If there is a simpler way that definitely works, please let me know.
I tried many before this one.

It works great except for this.

Thanks,

Shane


Nov 20 '05 #4
FOLLOWUP THOUGHT:

Ken, this seems to be an intermittent problem.
At present it is working, but 5 minutes ago it wasn't. Something is amuck.
(worse kind of problems).

THanks,

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:eL**************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a Class that I use to get the icons.
Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON = &H100

Private Const SHGFI_SMALLICON = &H1 'Small icon

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

-------------------------

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:eO**************@TK2MSFTNGP09.phx.gbl...
Hi all,

I really needed to get the icons associated with each file that I want to show in a listview.

I used the follow modified code sniplets found on the internet.

I have left in commented code for anyone else who may be looking to do the same.

I only care about the small icons in my program--detail view only.

Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
Public dwAttributes As Integer ' SFGAO flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName
As
String
End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal
pszPath
As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFileInfo, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON
Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80

Dim hImg As IntPtr
Dim shinfo As New SHFileInfo

' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon

hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)

' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName

'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function

from another method I do this

'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)

as you can see I get the filenames and then place them into an
imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....

The above code is some how trampling memory. It shows up in strange ways, systemexecutionexception or even stackoverflowexceptions

commenting out this call makes it run fine so I know the problem is there.
Do I need to somehow release this icon pointer or something?

If so how can I add the icon I want to the imagelist, and release this
icon
ptr to it and still have the image available in the imagelist?

Please help me solve this. My app really needs it if I will show icons.

If there is a simpler way that definitely works, please let me know.
I tried many before this one.

It works great except for this.

Thanks,

Shane


Nov 20 '05 #5
New oddity....

It seems that if I don't launch the splash screen which runs on a different
thread, it appears to be working.....
Maybe this is a multithreading problem.

Got the code for the splash screen from CodeProject so I wouldn't think it
would be a problem.

I didn't want to do.

frmsplash.showdialog for a few seconds and show my app.. Wanted to show the
splash and when the app was opened wait a few seconds before closing. This
has caused some ridiculous problems for a simple splash screen.

maybe showdialog is the way to go with the splash for a small app--forget
multithreading.

My app starts from Sub Main in a module that declares an Application object
of my own making which has the repsponsibility of calling an MDI window via
ShowDialog.

Seemed not to work until I made the splash on another thread--but now have
this intermittent problem that appears to go away if I don't show the
splash.

Any thoughts, ideas, help would be GREATLY APPRECIATED! I'm going nuts here
over something that should be simple.

Thanks,

Shane
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:eL**************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a Class that I use to get the icons.
Imports System.Runtime.InteropServices

Public Class clsGetIcon

Public Structure SHFILEINFO

Dim hIcon As IntPtr

Dim iIcon As Integer

Dim dwAttributes As Integer

<VBFixedString(260),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=260)> Public szDisplayName As String

'String that contains the name of the file as it appears in the Microsoft®
Windows® Shell, or the path and file name of the file that contains the icon representing the file.

<VBFixedString(80),
System.Runtime.InteropServices.MarshalAs(System.Ru ntime.InteropServices.Unma
nagedType.ByValTStr, SizeConst:=80)> Public szTypeName As String

End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As String, _

ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, _

ByVal cbFileInfo As Integer, ByVal uFlags As Integer) As Integer

Private Const SHGFI_ICON = &H100

Private Const SHGFI_SMALLICON = &H1 'Small icon

Public Function getIcon(ByVal szFilename As String) As Icon

Try

Dim aSHFileInfo As New SHFILEINFO

Dim cbFileInfo As Integer = _

Marshal.SizeOf(aSHFileInfo)

Dim uflags As Integer = SHGFI_ICON Or SHGFI_SMALLICON

SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, uflags)

Dim myIcon As Icon

myIcon = Icon.FromHandle(aSHFileInfo.hIcon)

Return myIcon

Catch ex As Exception

Debug.WriteLine(ex.ToString)

Return Nothing

End Try

End Function

End Class

Ken

-------------------------

"SStory" <Th*******@TAKEOUTTHISSPAMBUSTERsofthome.net> wrote in message
news:eO**************@TK2MSFTNGP09.phx.gbl...
Hi all,

I really needed to get the icons associated with each file that I want to show in a listview.

I used the follow modified code sniplets found on the internet.

I have left in commented code for anyone else who may be looking to do the same.

I only care about the small icons in my program--detail view only.

Private Structure SHFileInfo ' Shell File Info structure
Public hIcon As IntPtr ' Icon handle
Public iIcon As Integer ' Icon index
Public dwAttributes As Integer ' SFGAO flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> Public szTypeName
As
String
End Structure

Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" (ByVal
pszPath
As String, _
ByVal dwFileAttributes As Integer, ByRef psfi As SHFileInfo, ByVal
cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr

'Gets the small icon for a file extension
Private Function GetIconViaExt(ByVal FileExt As String) As
System.Drawing.Icon
Const SHGFI_DISPLAYNAME = &H200
Const SHGFI_ICON = &H100
Const SHGFI_SMALLICON = &H1 ' Small icon
Const SHGFI_USEFILEATTRIBUTES = &H10 ' use passed dwFileAttribute
Const SHGFI_LARGEICON = &H0 ' Large icon
Const SHGFI_TYPENAME = &H400 ' get type name
Const DEFAULT_FLAGS = SHGFI_TYPENAME Or SHGFI_ICON Or SHGFI_SMALLICON
Or
SHGFI_USEFILEATTRIBUTES
Const FILE_ATTRIBUTE_NORMAL = &H80

Dim hImg As IntPtr
Dim shinfo As New SHFileInfo

' Depending on the size, get the icon from the file
' If iSize = IconSize.Small Then ' Return small icon

hImg = SHGetFileInfo(FileExt, FILE_ATTRIBUTE_NORMAL, shinfo,
Marshal.SizeOf(shinfo), DEFAULT_FLAGS)

' ElseIf iSize = IconSize.Large Then ' Return large icon
' hImg = SHGetFileInfo(Filename, 0, shinfo, Marshal.SizeOf(shinfo),
SHGFI_ICON Or SHGFI_LARGEICON)
' End If
' strFileTypeName = shinfo.szTypeName

'The icon is returned in the hIcon member of the shinfo struct.
Debug.Write("calling GetIconViaExt....")
GetIconViaExt = System.Drawing.Icon.FromHandle(shinfo.hIcon)
Debug.WriteLine("....GetIconViaExt call returned")
End Function

from another method I do this

'fi is a fileinfo obj
Dim ico As Icon
ico = GetIconViaExt(fi.Extension)
'if GetIconViaExt returned something then lets add it to the image list and remember it in the sorted array
If Not IsNothing(ico) Then
'add the icon to the image list
imlFileIcons.Images.Add(ico)

as you can see I get the filenames and then place them into an
imagelist--I
actually keep up with them by another array and the extension to avoid
regetting the icon for the same extension once I have it, but anyhow....

The above code is some how trampling memory. It shows up in strange ways, systemexecutionexception or even stackoverflowexceptions

commenting out this call makes it run fine so I know the problem is there.
Do I need to somehow release this icon pointer or something?

If so how can I add the icon I want to the imagelist, and release this
icon
ptr to it and still have the image available in the imagelist?

Please help me solve this. My app really needs it if I will show icons.

If there is a simpler way that definitely works, please let me know.
I tried many before this one.

It works great except for this.

Thanks,

Shane


Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2005
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant memory in terms of the length of the list i? If so why not and how could I change it to be so? I am sure the solution is O(n) since the list must...
1
1953
by: Silver | last post by:
Hi everyone, my program compiles and executes, but I get an error during run-time. I guess it has something to do with memory allocation (which I don't seem to fully control yet).. Here's the code: #include <iostream> #include <malloc.h> #include <iomanip> using namespace std;
4
6758
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a = Linux galahad 2.4.19-64GB-SMP #1 SMP /etc/sysctl.conf kernel.shmmax=268435456
5
1998
by: Crimzon | last post by:
I am using MSVC++ 6.0 compiler. I am declaring an array char ch. Program works fine for these arbitrary sizes. But if I make the size of the array bigger like ch, the program gives me an error message pertaining to the array size and doesnt work. Does it mean that the total size of the array allocated (10000 * 20) should be less than 65535...
2
1370
by: nguyenlh | last post by:
I'm studying about Memory managerment - stack memory and I can't understand the following problems : Who can help me >> - Role of Stack memory -How to store address in stack memory -Garbage collection process in stack memory -Reuse and access address in stack memory I'm a newbie >>> Thanks very much...
0
1192
by: hairobinson | last post by:
Can any one help me explaing about Kernel Memory leak? what is kernel memory leak? how do we debug Kernel level Memory Leak? Do we have standard tool for finding it? How do we differentiate Kernel level memory leak with Application level Memory leak? Thanks in Advance,
1
3093
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
2
1372
by: Jim Rutledge | last post by:
i am gettin back into programming with visual studio 2003 and using c# to program in . I am having trouble in debug mode when i click to use the Memory 1 window .. it has completely disappeared , and i cant unhide it or find it . i can autohide memory 2 ,3 and 4 and find them all the time , so its JUST memory 1 that has disappeared .. oh and...
0
1453
by: Tosh2pointO | last post by:
Hello, I'm trying to make a program that will multiply 2 matrices using shmget() and fork(). For example, I would need to multiply a 64 x 64 matrix using 4 processes or 16 processes, and the multi-processes will be created using fork. Each process will calculate a partition of the final Matrix Now, I'm not sure whether I should write all the...
0
7330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7721
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5246
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
620
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.