2016-07-11 65 views
1

私のコードで次のような問題があります。アイコンに渡されたWin32ハンドルが有効でないか、間違ったタイプです

SHFILEINFO宣言

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宣言

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 

S:

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

コードの行は、以下のとおりでありますHGetFileInfo使用

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 

ランタイムここで

はSHGetFileInfoに渡される値です。記載された値は、上記SHGetFileInfoに渡される場合

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

エラー

、それは

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

達すると従ってHICON = 0

を作る0の値を返します次のエラーが発生しました

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

問題の原因を特定するのにお手伝いできますか?

答えて

1

はまた、私はOn Error Gotoを失い、Try/Catchを使用することになり、この

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 

SHFILEINFOSHGetFileInfoを変更してみてください、ありがとうございました。

+0

私はあなたのコードを使用しようとしましたが、同じエラーがまだ現れました。 On Error GoToに関しては、カスタムMessageBoxを使ってエラーと位置を表示するので、Try/Catchメソッドを使用することはできません。 –

+0

私のマシンで動作しました。 –

+0

本当ですか?私はあなたのコードをコピーし、私の代わりに同じ問題が私のサイトで発生しました。ありがとうBTW。 –

関連する問題