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
UseFileAttributes = &H10
End Enum
Public Enum IconSize
SmallIcon = 1
LargeIcon = 0
End Enum
<StructLayout(LayoutKind.Sequential)> _
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.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 dwFileAttributes 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.SmallIcon, 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.UseFileAttributes
flags = flags + IconSize
SHGetFileInfo(Path, 256, info, cbSizeInfo, flags)
GetDefaultIcon = Icon.FromHandle(info.hIcon)
If SaveIconPath <> "" Then
Dim FileStream As New IO.FileStream(SaveIconPath,
IO.FileMode.Create)
GetDefaultIcon.Save(FileStream)
FileStream.Close()
End If
End Function 3 1337
Hi,
Here is an example that adds the filenames with there icons to a
listview and imagelist.
Private Function getLocalIcons(ByVal szFolderPath As String)
Dim dirInfo As New System.IO.DirectoryInfo(szFolderPath)
Dim di As System.IO.DirectoryInfo
Dim fi As System.IO.FileInfo
Dim lvitem As ListViewItem
Dim hIcon As Icon
Dim cIcon As New clsGetIcon
Dim htIcons As New Hashtable
Dim intIndex As Integer
imlIcon.Images.Clear()
lv.Items.Clear()
lv.SmallImageList = imlIcon
For Each di In dirInfo.GetDirectories()
lvitem = lv.Items.Add(di.Name)
hIcon = cIcon.getIcon(di.FullName)
If htIcons.ContainsKey(hIcon) Then
intIndex = htIcons(hIcon)
Else
imlIcon.Images.Add(hIcon.ToBitmap)
intIndex = imlIcon.Images.Count - 1
End If
lvitem.ImageIndex = intIndex
Next
For Each fi In dirInfo.GetFiles()
lvitem = lv.Items.Add(fi.Name)
hIcon = cIcon.getIcon(fi.FullName)
If htIcons.ContainsKey(hIcon) Then
intIndex = htIcons(hIcon)
Else
imlIcon.Images.Add(hIcon.ToBitmap)
intIndex = imlIcon.Images.Count - 1
End If
lvitem.ImageIndex = intIndex
Next
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lv.SuspendLayout()
getLocalIcons("C:\")
lv.ResumeLayout()
End Sub
The helper class.
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 As Integer = &H100
Private Const SHGFI_SMALLICON As Integer = &H1 'Small icon
Private Const SHGFI_TYPENAME As Integer = &H400 ' get type name
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)
aSHFileInfo.szTypeName = Space(255)
SHGetFileInfo(szFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)
Trace.WriteLine(aSHFileInfo.szTypeName)
Return myIcon
Catch ex As Exception
Debug.WriteLine(ex.ToString)
Return Nothing
End Try
End Function
End Class
Ken
---------------------------
"yxq" <ga***@163.net> wrote in message
news:eA**************@TK2MSFTNGP14.phx.gbl...
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
UseFileAttributes = &H10
End Enum
Public Enum IconSize
SmallIcon = 1
LargeIcon = 0
End Enum
<StructLayout(LayoutKind.Sequential)> _
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> Public
szDisplayName As String
<MarshalAs(UnmanagedType.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 dwFileAttributes 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.SmallIcon, 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.UseFileAttributes
flags = flags + IconSize
SHGetFileInfo(Path, 256, info, cbSizeInfo, flags)
GetDefaultIcon = Icon.FromHandle(info.hIcon)
If SaveIconPath <> "" Then
Dim FileStream As New IO.FileStream(SaveIconPath,
IO.FileMode.Create)
GetDefaultIcon.Save(FileStream)
FileStream.Close()
End If
End Function
"yxq" <ga***@163.net> schrieb: <StructLayout(LayoutKind.Sequential)> _
Replace the line above with this one:
\\\
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
///
Private Structure SHFILEINFO Public hIcon As IntPtr Public iIcon As Integer Public dwAttributes As Integer <MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> Public szDisplayName As String <MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public szTypeName As String
Replace the two lines above with these:
\\\
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Publiv szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
"Ken Tucker [MVP]" <vb***@bellsouth.net> schrieb Hi,
Here is an example that adds the filenames with there icons to a listview and imagelist. [...]
Ken, if you paste your code to notepade first, then into your posting,
formatting remains and no additional lines are inserted. Better to read.
Armin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andreas Suurkuusk |
last post by:
Hi,
I just noticed your post in the "C# memory problem: no end for our problem?"
thread.
In the post you implied that I do not how the garbage collector works and
that I mislead people. Since...
|
by: Ian Taite |
last post by:
Hello,
I'm exploring why one of my C# .NET apps has "high" memory usage, and
whether I can reduce the memory usage.
I have an app that wakes up and processes text files into a database...
|
by: Ganesan selvaraj |
last post by:
I using C# .net. i want to split the text files based of the some condition.
my source text file size may be 4 kb to 4 gb. some time when i split the
i got the "out of memory exception. when i...
|
by: vikram |
last post by:
i have series of questions
1.How a c program is loaded in memory
i mean the whats is the structure that the code segment?? data segment??
2.When you say
const int *p;
where is p...
|
by: ravi |
last post by:
I have a situation where i want to free the memory pointed by a
pointer, only if it is not freed already. Is there a way to know
whether the memory is freed or not?
|
by: jacob navia |
last post by:
In C, we have read-only memory (const), read/write memory
(normal data), and write only memory.
Let's look at the third one in more detail.
Write only memory is a piece of RAM that can only...
|
by: clsmyth |
last post by:
Folks,
Hi, I have never posted to a language group before so please excuse me
if this is inappropriate. I have posted this to comp.unix.solaris
(well, I am one of the folks on the thread at...
|
by: William Stacey [MVP] |
last post by:
Here is an interesting writing on memory barriers. Not sure if this helps
my understanding or raises more questions, but interesting...
...
|
by: Johan Tibell |
last post by:
I've written a piece of code that uses sockets a lot (I know that
sockets aren't portable C, this is not a question about sockets per
se). Much of my code ended up looking like this:
if...
|
by: KWienhold |
last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and
C#) that stores files and additional information in a single compound
file using IStorage/IStream.
Since files in a compound...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
| |