Memory can not Read

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yxq

    #1

    Memory can not Read

    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


  • Ken Tucker [MVP]

    #2
    Re: Memory can not Read

    Hi,

    Here is an example that adds the filenames with there icons to a
    listview and imagelist.

    Private Function getLocalIcons(B yVal szFolderPath As String)

    Dim dirInfo As New System.IO.Direc toryInfo(szFold erPath)

    Dim di As System.IO.Direc toryInfo

    Dim fi As System.IO.FileI nfo

    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.SmallImageLi st = imlIcon

    For Each di In dirInfo.GetDire ctories()

    lvitem = lv.Items.Add(di .Name)

    hIcon = cIcon.getIcon(d i.FullName)

    If htIcons.Contain sKey(hIcon) Then

    intIndex = htIcons(hIcon)

    Else

    imlIcon.Images. Add(hIcon.ToBit map)

    intIndex = imlIcon.Images. Count - 1

    End If

    lvitem.ImageInd ex = intIndex

    Next

    For Each fi In dirInfo.GetFile s()

    lvitem = lv.Items.Add(fi .Name)

    hIcon = cIcon.getIcon(f i.FullName)

    If htIcons.Contain sKey(hIcon) Then

    intIndex = htIcons(hIcon)

    Else

    imlIcon.Images. Add(hIcon.ToBit map)

    intIndex = imlIcon.Images. Count - 1

    End If

    lvitem.ImageInd ex = intIndex

    Next

    End Function

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    lv.SuspendLayou t()

    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(Syst em.Runtime.Inte ropServices.Unm anagedType.ByVa lTStr,
    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(Syst em.Runtime.Inte ropServices.Unm anagedType.ByVa lTStr,
    SizeConst:=80)> Public szTypeName As String

    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 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(s zFilename, 0, aSHFileInfo, cbFileInfo, uflags)

    Dim myIcon As Icon

    myIcon = Icon.FromHandle (aSHFileInfo.hI con)

    aSHFileInfo.szT ypeName = Space(255)

    SHGetFileInfo(s zFilename, 0, aSHFileInfo, cbFileInfo, SHGFI_TYPENAME)

    Trace.WriteLine (aSHFileInfo.sz TypeName)

    Return myIcon

    Catch ex As Exception

    Debug.WriteLine (ex.ToString)

    Return Nothing

    End Try

    End Function

    End Class



    Ken

    ---------------------------
    "yxq" <gayxq@163.ne t> wrote in message
    news:eAsbgPXaFH A.4040@TK2MSFTN GP14.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
    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

    • Herfried K. Wagner [MVP]

      #3
      Re: Memory can not Read

      "yxq" <gayxq@163.ne t> schrieb:[color=blue]
      > <StructLayout(L ayoutKind.Seque ntial)> _[/color]

      Replace the line above with this one:

      \\\
      <StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)> _
      ///
      [color=blue]
      > 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[/color]

      Replace the two lines above with these:

      \\\
      <MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=260) > _
      Publiv szDisplayName As String
      <MarshalAs(Unma nagedType.ByVal TStr, 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/>

      Comment

      • Armin Zingler

        #4
        Re: Memory can not Read

        "Ken Tucker [MVP]" <vb2ae@bellsout h.net> schrieb[color=blue]
        > Hi,
        >
        > Here is an example that adds the filenames with there icons to
        > a
        > listview and imagelist.
        > [...][/color]


        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

        Comment

        Working...