Search code examples
vb.netshell32.dll

Win32 handle that was passed to Icon is not valid or is the wrong type


I have the following issue with my code.

Win32 handle that was passed to Icon is not valid or is the wrong type

The line of codes are as follow:

SHFILEINFO Declaration

Private Structure SHFILEINFO
    Public hIcon As IntPtr            ' : iconc
    Public iIcon As Integer           ' : icondex
    Public dwAttributes As Integer    ' : SFGAO_ flags
     _
    Public szDisplayName As String
     _
    Public szTypeName As String
End Structure

SHGetFileInfo Declaration

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

Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0    ' Large icon
Private Const MAX_PATH = 260

SHGetFileInfo Usage

Private Sub AddImageToImageListBox(ByVal strFileName As String)
    On Error GoTo errHandler

    Dim shInfo As SHFILEINFO
    shInfo = New SHFILEINFO()

    shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
    shInfo.szTypeName = New String(vbNullChar, 80)

    Dim hIcon As IntPtr
    hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)

    Dim MyIcon As Drawing.Bitmap
    MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
    imgAttachment.AddImage(MyIcon)
    ilstAttachments.Items.Add(strFileName.ToString(), imgAttachment.Images.Count - 1)

    Exit Sub
errHandler:
    ErrMsg("AddImageToImageListBox (errHandler)")
End Sub

Runtime

Here are the values that being passed into SHGetFileInfo.

strFileName = "Copy (223) of Uncollected Card - Multiple Pages.TIF"
shInfo.dwAttributes = 0
shInfo.hIcon = 0
shInfo.iIcon = 0
shInfo.szDisplayName = ""
shInfo.szTypeName = ""

Error

When the stated values above are being passed to SHGetFileInfo, it returns 0 value thus making hIcon = 0.

When it reaches

MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap

The following error occurred

Win32 handle that was passed to Icon is not valid or is the wrong type 

Can you guys help me to identify what is the issue?

Thank you


Solution

  • Try changing SHFILEINFO and SHGetFileInfo to this

       Private Structure SHFILEINFO
            Public hIcon As IntPtr            ' : iconc
            Public iIcon As Integer           ' : icondex
            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 Ansi 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
    

    Also, I would lose the On Error Goto and use a Try/Catch.