2016-07-11 14 views
1

내 코드에 다음과 같은 문제가 있습니다.Icon에 전달 된 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 = "" 

, 그것은 0 값은 따라서 HICON =

가 도달 0

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

을 반환 다음과 같은 오류가 발생했습니다.

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

여러분이 문제를 파악하는 데 도움을 줄 수 있습니까?

내가 On Error Goto을 잃고 Try/Catch을 사용하면

답변

1

시도 또한이

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

정말요? 귀하의 코드를 복사하고 대체합니다. 그러나 동일한 문제가 내 사이트에서 발생했습니다. 고마워요. –