473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Memory can not Read

yxq
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
Nov 21 '05 #1
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

Nov 21 '05 #2
"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/>
Nov 21 '05 #3
"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

Nov 21 '05 #4

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

Similar topics

0
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...
3
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...
6
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...
8
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...
72
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?
18
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...
8
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...
31
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... ...
66
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...
3
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
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...
0
agi2029
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,...
0
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...

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.