Results 1 to 19 of 19

Thread: Fatal chrashing when enumerting a folder for thumbnails.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Fatal chrashing when enumerting a folder for thumbnails.

    When not thumbnails are created I got a function that retuns the corresponding iconindex from the system (shell32) or if it's a own created imagelist the corresponding hIcon for that item in the enumeration which not creates a thumbnail (non image or movie clips). However than I ported the project from Win11 to the Win10 machine I started to encounter fatal crashes in the attemt to get the iconindexs or hicons. I use the API ShGetFileInfo (Alias W) in this way:

    Code:
    Public Type SHFILEINFO
        hIcon As Long
        iIcon As Long
        dwAttributes As SFGAO_Flags
        lpDisplayName As Long
        lpTypeName As Long
    End Type
    
    Public Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoW" (ByVal pidl As Long, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    
    Public Function GetItemIconIndex(ByVal pidl As Long, Optional bLargeIcon As Boolean) As Long
      Dim lpSHFI As SHFILEINFO
      Dim lr As Long
      Dim iIcon As Long
      
      If bLargeIcon = True Then
        lr = SHGetFileInfo(pidl, -1, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_SYSICONINDEX)
      Else
        lr = SHGetFileInfo(pidl, -1, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX)
      End If
      
      GetItemIconIndex = lpSHFI.iIcon
    End Function
    And the corresponding for hIcon in same fasion.

    I have tried both pidlenum and pidlCombine but both are crashing.

    Below is the IShellItem Enumeration:

    Code:
    Public Function EnumLVItem(ByVal pidlRoot As Long, ucLV As ucBrowseForFolderList, Optional ByRef nCount As Long, Optional ByVal iGroupId As Integer = -1) As Long
      Dim pidlParent As Long
      Dim pidlEnum As Long
      Dim pidlChild As Long
      Dim pidlCombine As Long
      Dim pISI As IShellItem
      Dim pISI2 As IShellItem2
      Dim pISI_child As IShellItem
      Dim pISIA As IShellItemArray
      Dim pIPAI As IParentAndItem
      Dim pISM As IShellMenu
      Dim pISF As IShellFolder2
      Dim pEIDL As IEnumIDList
      Static BHID_StorageEnum As UUID
      Dim pIESHI As IEnumShellItems
      Dim hr As Long
      Dim lpName As Long
      Dim lpNameRoot As Long
      Dim sName As String
      Dim sNameRoot As String
      Dim sTypeName As String
      Dim sExt As String
      
      Dim hIcon As Long
      Dim iIconIndex As Long
      Dim iRootIcon As Integer
      Dim dwAttribs As Long
      Dim hWndIL As Long
      Dim n As Long
      Dim pHTN As Long
     
      n = 0
        
      hr = SHCreateItemFromIDList(pidlRoot, IID_IShellItem, pISI)
    '
    '  'IIDFromString StrPtr("{4621A4E3-F0D6-4773-8A9C-46E77B174840}"), BHID_StorageEnum
    '  'BHID_EnumItems
      hr = pISI.BindToHandler(0, BHID_EnumItems, IID__IEnumShellItems, pIESHI)
    '  'hr = pISI.BindToHandler(0, BHID_SFObject, IID__IShellFolder, pISF)
    '
    '  'pISF.EnumObjects 0, SHCONTF_FOLDERS Or SHCONTF_NONFOLDERS Or SHCONTF_STORAGE, pEIDL
    '  'Do While pEIDL.Next(1, pidlEnum, 0) = S_OK
    '
    '
      Do While pIESHI.Next(1, pISI_child, 0) = S_OK
           Set pIPAI = pISI_child
           Set pISI2 = pISI_child
    
           pIPAI.GetParentAndItem pidlParent, pISF, pidlChild
    
           pISI_child.GetDisplayName SIGDN_NORMALDISPLAY, lpName
    
           pidlEnum = GetPIDLFromObject(pISI_child)
    
           pidlCombine = ILCombine(pidlParent, pidlChild)
    
           DoEvents
    
           If ucLV.UseThumbNails = True Then
             hWndIL = ucLV.LargeImageList
             GetThumbNailFromPidlItem pidlEnum, ucLV, pHTN
             If pHTN <> 0 Then
               iIconIndex = ImageList_Add(hWndIL, pHTN, pHTN)
               Debug.Print "ThmbIndex: " & iIconIndex
               'If iIconIndex > -1 Then MsgBox iIconIndex
             Else
               'Error - item probably has no thumbnail.
               hIcon = GetItemIconHandle(pidlEnum, True)
               If hIcon <> 0 Then
                 iIconIndex = ImageList_AddIcon(hWndIL, hIcon)
                 DestroyIcon hIcon
               End If
             End If
           Else
             iIconIndex = GetItemIconIndex(pidlEnum, False)
           End If
    '
    '       'GetIShellLibrary pidlEnum  '<---- HOW TO JUMP OVER THE AUTOMATION ERROR?
           If ucLV.IsGroupViewEnabled = True Then
             SHGetNameFromIDList pidlRoot, SIGDN_NORMALDISPLAY, lpNameRoot
             sNameRoot = StrConv(SysAllocString(lpNameRoot), vbFromUnicode)
             iRootIcon = GetItemIconIndex(pidlRoot, False)
             MsgBox iRootIcon
             'sNameRoot = StrConv(SysAllocString(lpNameRoot), vbFromUnicode)
    '
             ucLV.AddGroup n, sNameRoot, iRootIcon, sNameRoot & " Mappar - Fälls ut eller fälls in via pilikonen i högra hörnet.    " & n & " Object."
    
             pidlCombine = ILCombine(pidlParent, pidlChild)
    
             SHGetNameFromIDList pidlCombine, SIGDN_NORMALDISPLAY, lpName
    
             sName = StrConv(SysAllocString(lpName), vbFromUnicode)
    
             ucLV.AddItems n, sName, iIconIndex, iGroupId, pidlCombine
           Else
    
             pidlCombine = ILCombine(pidlParent, pidlChild)
    
             SHGetNameFromIDList pidlCombine, SIGDN_NORMALDISPLAY, lpName
    
             sName = StrConv(SysAllocString(lpName), vbFromUnicode)
    
             ucLV.AddItems n, sName, iIconIndex, 2, pidlCombine
           End If
           
           n = n + 1
           nCount = n
           CoTaskMemFree pidlEnum
           CoTaskMemFree pidlChild
           CoTaskMemFree pidlCombine
           CoTaskMemFree lpName
           CoTaskMemFree lpNameRoot
           Set pISF = Nothing
           Set pISI_child = Nothing
           Set pIPAI = Nothing
    
           'SHUpdateImageW StrPtr(sName), iIconIndex, &H2, iIconIndex
           'iIconIndex = Shell_GetCachedImageIndexW(lpName, iIconIndex, 0&)
           lpNameRoot = 0
           pidlRoot = 0
           lpName = 0
           sNameRoot = 0
      Loop
    
    End Function

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Now I tried to isolate what I belive is the problem and when I isolated this routine I instantly got a chrash!!

    Code:
    Public Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoW" (ByVal pszPath As Long, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    
    Public Type SHFILEINFO
        hIcon As Long
        iIcon As Long
        dwAttributes As SFGAO_Flags
        lpDisplayName As Long
        lpTypeName As Long
    End Type
    
    
    Public Function GetItemIconIndex(ByVal pidl As Long, Optional bLargeIcon As Boolean) As Long
      Dim lpSHFI As SHFILEINFO
      Dim lr As Long
      
      If bLargeIcon = True Then
        lr = SHGetFileInfo(pidl, -1, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_SYSICONINDEX)
      Else
        lr = SHGetFileInfo(pidl, -1, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX)
      End If
      
      GetItemIconIndex = lpSHFI.iIcon
    End Function
    So what is the problem?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Now I have sorted out that it's the SHGFI_ICON flag thats corrupt everyting...When I select SmallIcon option everything works fine.
    Public Const SHGFI_ICON = &H100 <-- in my declaration
    Has it changed?

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,546

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Where'd you get that definition of SHFILEINFO?

    For SHGetFileInfoW:

    Code:
    Public Type SHFILEINFO 'SHFILEINFOW
      hIcon As LongPtr
      iIcon As Long
      dwAttributes As SFGAO_Flags
      szDisplayName(MAX_PATH - 1) As Integer
      szTypeName(79) As Integer
    End Type

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    From the old API Viewer and I have checked that defintion with MSDN and it states the same
    Where do you get yourt information from?

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    I begin to be a little tired of this...I have forked out where the problem is but I can't take it in because it's so contra productive.
    This is the routine:
    Code:
    Public Function GetItemIconIndex(ByVal pidl As Long, Optional bLargeIcon As Boolean) As Long
      Dim lpSHFI As SHFILEINFOW
      Dim lr As Long
      
      If bLargeIcon = True Then
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SYSICONINDEX)
      Else
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX)
      End If
      
      GetItemIconIndex = lpSHFI.iIcon
    End Function
    When I pass the given pidl and I choose bLargeIcoon to False I get it all correct as it should be..
    But if I choose bLargeIcon to True it crash in a blink.

    Code:
    Public Const SHGFI_ATTRIBUTES = &H800
    Public Const SHGFI_DISPLAYNAME = &H200
    Public Const SHGFI_EXETYPE = &H2000
    Public Const SHGFI_ICON = &H100
    Public Const SHGFI_ICONLOCATION = &H1000
    Public Const SHGFI_LARGEICON = &H0
    Public Const SHGFI_LINKOVERLAY = &H8000
    Public Const SHGFI_OPENICON = &H2
    Public Const SHGFI_ADDOVERLAYS = &H20
    Public Const SHGFI_PIDL = &H8
    Public Const SHGFI_SELECTED = &H10000
    Public Const SHGFI_SHELLICONSIZE = &H4
    Public Const SHGFI_SMALLICON = &H1
    Public Const SHGFI_SYSICONINDEX = &H4000
    Public Const SHGFI_TYPENAME = &H400
    Public Const SHGFI_USEFILEATTRIBUTES = &H10
    
    Public Type SHFILEINFOW
      hIcon As LongPtr
      iIcon As Long
      dwAttributes As Long
      szDisplayName(MAX_PATH - 1) As Integer
      szTypeName(79) As Integer
    End Type
    
    Public Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoW" (ByVal pszPath As Long, ByVal dwFileAttributes As Long, psfi As SHFILEINFOW, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Also today I got this problem...as you can see in the caption of form1 I did this just for "notifying the problem."
    Whatever I do, the sysListView32 will not show the images that is added to the imagelist.
    The space in the ListView are there but no images.
    Attached Images Attached Images  

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    I started a new clean project without your TypeLib with same settings and same function.
    And it works flawless...just as suposed to do.

    Code:
    Private Const SHGFI_ATTRIBUTES = &H800
    Private Const SHGFI_DISPLAYNAME = &H200
    Private Const SHGFI_EXETYPE = &H2000
    Private Const SHGFI_ICON = &H100
    Private Const SHGFI_ICONLOCATION = &H1000
    Private Const SHGFI_LARGEICON = &H0
    Private Const SHGFI_LINKOVERLAY = &H8000
    Private Const SHGFI_OPENICON = &H2
    Private Const SHGFI_PIDL = &H8
    Private Const SHGFI_SELECTED = &H10000
    Private Const SHGFI_SHELLICONSIZE = &H4
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_SYSICONINDEX = &H4000
    Private Const SHGFI_TYPENAME = &H400
    Private Const SHGFI_USEFILEATTRIBUTES = &H10
    
    Private Type SHFILEINFOW
            hIcon As Long                      '  out: icon
            iIcon As Long          '  out: icon index
            dwAttributes As Long               '  out: SFGAO_ flags
            szDisplayName As String * 256 '  out: display name (or path)
            szTypeName As String * 80         '  out: type name
    End Type
    
    Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoW" (ByVal pszPath As Long, ByVal dwFileAttributes As Long, psfi As SHFILEINFOW, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    Private Declare Function ILCreateFromPath Lib "shell32" Alias "ILCreateFromPathW" (ByVal pszPath As Long) As Long
    
    Public Function GetItemIconIndex(ByVal pidl As Long, Optional bLargeIcon As Boolean) As Long
      Dim lpSHFI As SHFILEINFOW
      Dim lr As Long
      
      If bLargeIcon = True Then
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SYSICONINDEX)
      Else
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX)
      End If
      
      GetItemIconIndex = lpSHFI.iIcon
    End Function
    
    Private Sub Command1_Click()
      Dim pidl As Long
      
      pidl = ILCreateFromPath(StrPtr("C:\"))
      
      If pidl Then
        MsgBox GetItemIconIndex(pidl, True)
      End If
      
    End Sub
    Since this works as it supposed to do I suspect there is something wrong with your TypeLib.

  9. #9
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,219

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Before using any function from a TypeLib you need to check whether its parameters have been declared the way you wanted them or not. There are many variations on how to declare function parameters, whether they are declared "As Any", "ByVal As Long", "As SomeUDT" or any other variation.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    I havn't used any functions from this Fafalon's typelib in this matter about SysIconIndex...I belive this typelib is leaking memory or something that screw other things up.

  11. #11
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,546

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Quote Originally Posted by nebeln View Post
    From the old API Viewer and I have checked that defintion with MSDN and it states the same
    Where do you get yourt information from?
    No it doesn't.

    Code:
    typedef struct _SHFILEINFOW {
      HICON hIcon;
      int   iIcon;
      DWORD dwAttributes;
      WCHAR szDisplayName[MAX_PATH];
      WCHAR szTypeName[80];
    } SHFILEINFOW;
    and the authoritative source, the actual shellapi.h header:

    Code:
    typedef struct _SHFILEINFOW
    {
            HICON       hIcon;                      // out: icon
            int         iIcon;                      // out: icon index
            DWORD       dwAttributes;               // out: SFGAO_ flags
            WCHAR       szDisplayName[MAX_PATH];    // out: display name (or path)
            WCHAR       szTypeName[80];             // out: type name
    } SHFILEINFOW;
    The API viewer's win32api.txt is riddled with errors. When I did tbShellLib, I re-did everything from scratch because it's too unreliable.

  12. #12
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,546

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Quote Originally Posted by nebeln View Post
    I started a new clean project without your TypeLib with same settings and same function.
    And it works flawless...just as suposed to do.

    Code:
    Private Const SHGFI_ATTRIBUTES = &H800
    Private Const SHGFI_DISPLAYNAME = &H200
    Private Const SHGFI_EXETYPE = &H2000
    Private Const SHGFI_ICON = &H100
    Private Const SHGFI_ICONLOCATION = &H1000
    Private Const SHGFI_LARGEICON = &H0
    Private Const SHGFI_LINKOVERLAY = &H8000
    Private Const SHGFI_OPENICON = &H2
    Private Const SHGFI_PIDL = &H8
    Private Const SHGFI_SELECTED = &H10000
    Private Const SHGFI_SHELLICONSIZE = &H4
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_SYSICONINDEX = &H4000
    Private Const SHGFI_TYPENAME = &H400
    Private Const SHGFI_USEFILEATTRIBUTES = &H10
    
    Private Type SHFILEINFOW
            hIcon As Long                      '  out: icon
            iIcon As Long          '  out: icon index
            dwAttributes As Long               '  out: SFGAO_ flags
            szDisplayName As String * 256 '  out: display name (or path)
            szTypeName As String * 80         '  out: type name
    End Type
    
    Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoW" (ByVal pszPath As Long, ByVal dwFileAttributes As Long, psfi As SHFILEINFOW, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    Private Declare Function ILCreateFromPath Lib "shell32" Alias "ILCreateFromPathW" (ByVal pszPath As Long) As Long
    
    Public Function GetItemIconIndex(ByVal pidl As Long, Optional bLargeIcon As Boolean) As Long
      Dim lpSHFI As SHFILEINFOW
      Dim lr As Long
      
      If bLargeIcon = True Then
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SYSICONINDEX)
      Else
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX)
      End If
      
      GetItemIconIndex = lpSHFI.iIcon
    End Function
    
    Private Sub Command1_Click()
      Dim pidl As Long
      
      pidl = ILCreateFromPath(StrPtr("C:\"))
      
      If pidl Then
        MsgBox GetItemIconIndex(pidl, True)
      End If
      
    End Sub
    Since this works as it supposed to do I suspect there is something wrong with your TypeLib.
    Like what? Since neither that UDT nor that API are in it...

    A typelib cannot "leak memory".

    That works because you've used an equivalent of the correct definition instead of the one you originally claimed MSDN said was right.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    So tell me why this small test sample works clockwork?
    All ate taken from old API Viwer exept from my little routine.
    The same routine worked very well for me up to yesterday when I suddenly become aware this little routine stopped working and causing me alot of errors....

    Code:
    Private Const SHGFI_ATTRIBUTES = &H800
    Private Const SHGFI_DISPLAYNAME = &H200
    Private Const SHGFI_EXETYPE = &H2000
    Private Const SHGFI_ICON = &H100
    Private Const SHGFI_ICONLOCATION = &H1000
    Private Const SHGFI_LARGEICON = &H0
    Private Const SHGFI_LINKOVERLAY = &H8000
    Private Const SHGFI_OPENICON = &H2
    Private Const SHGFI_PIDL = &H8
    Private Const SHGFI_SELECTED = &H10000
    Private Const SHGFI_SHELLICONSIZE = &H4
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_SYSICONINDEX = &H4000
    Private Const SHGFI_TYPENAME = &H400
    Private Const SHGFI_USEFILEATTRIBUTES = &H10
    
    Private Type SHFILEINFOW
            hIcon As Long                      '  out: icon
            iIcon As Long          '  out: icon index
            dwAttributes As Long               '  out: SFGAO_ flags
            szDisplayName As String * 256 '  out: display name (or path)
            szTypeName As String * 80         '  out: type name
    End Type
    
    Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoW" (ByVal pszPath As Long, ByVal dwFileAttributes As Long, psfi As SHFILEINFOW, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    Private Declare Function ILCreateFromPath Lib "shell32" Alias "ILCreateFromPathW" (ByVal pszPath As Long) As Long
    
    Public Function GetItemIconIndex(ByVal pidl As Long, Optional bLargeIcon As Boolean) As Long
      Dim lpSHFI As SHFILEINFOW
      Dim lr As Long
      
      If bLargeIcon = True Then
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SYSICONINDEX)
      Else
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX)
      End If
      
      GetItemIconIndex = lpSHFI.iIcon
    End Function
    
    Private Sub Command1_Click()
      Dim pidl As Long
      
      pidl = ILCreateFromPath(StrPtr("C:"))
      
      If pidl Then
        MsgBox GetItemIconIndex(pidl, True)
      End If
      
    End Sub
    This routine doesn't work any longer in writing moment in the project with the TypeLib involved...it worked yesterday and it works clock work as the sample I attached (without the TypeLib)...I still belive the TypeLib got issus because this shall not normaly occure.
    Last edited by nebeln; Dec 2nd, 2023 at 12:36 AM.

  14. #14
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,219

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    As far as I know the MAX_PATH constant is supposed to be 260, not 256 like you hardcoded in your sample.

  15. #15
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,546

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Quote Originally Posted by nebeln View Post
    So tell me why this small test sample works clockwork?
    All ate taken from old API Viwer exept from my little routine.
    The same routine worked very well for me up to yesterday when I suddenly become aware this little routine stopped working and causing me alot of errors....

    Code:
    Private Const SHGFI_ATTRIBUTES = &H800
    Private Const SHGFI_DISPLAYNAME = &H200
    Private Const SHGFI_EXETYPE = &H2000
    Private Const SHGFI_ICON = &H100
    Private Const SHGFI_ICONLOCATION = &H1000
    Private Const SHGFI_LARGEICON = &H0
    Private Const SHGFI_LINKOVERLAY = &H8000
    Private Const SHGFI_OPENICON = &H2
    Private Const SHGFI_PIDL = &H8
    Private Const SHGFI_SELECTED = &H10000
    Private Const SHGFI_SHELLICONSIZE = &H4
    Private Const SHGFI_SMALLICON = &H1
    Private Const SHGFI_SYSICONINDEX = &H4000
    Private Const SHGFI_TYPENAME = &H400
    Private Const SHGFI_USEFILEATTRIBUTES = &H10
    
    Private Type SHFILEINFOW
            hIcon As Long                      '  out: icon
            iIcon As Long          '  out: icon index
            dwAttributes As Long               '  out: SFGAO_ flags
            szDisplayName As String * 256 '  out: display name (or path)
            szTypeName As String * 80         '  out: type name
    End Type
    
    Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoW" (ByVal pszPath As Long, ByVal dwFileAttributes As Long, psfi As SHFILEINFOW, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    Private Declare Function ILCreateFromPath Lib "shell32" Alias "ILCreateFromPathW" (ByVal pszPath As Long) As Long
    
    Public Function GetItemIconIndex(ByVal pidl As Long, Optional bLargeIcon As Boolean) As Long
      Dim lpSHFI As SHFILEINFOW
      Dim lr As Long
      
      If bLargeIcon = True Then
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SYSICONINDEX)
      Else
        lr = SHGetFileInfo(pidl, 0, lpSHFI, LenB(lpSHFI), SHGFI_PIDL Or SHGFI_SMALLICON Or SHGFI_SYSICONINDEX)
      End If
      
      GetItemIconIndex = lpSHFI.iIcon
    End Function
    
    Private Sub Command1_Click()
      Dim pidl As Long
      
      pidl = ILCreateFromPath(StrPtr("C:"))
      
      If pidl Then
        MsgBox GetItemIconIndex(pidl, True)
      End If
      
    End Sub
    This routine doesn't work any longer in writing moment in the project with the TypeLib involved...it worked yesterday and it works clock work as the sample I attached (without the TypeLib)...I still belive the TypeLib got issus because this shall not normaly occure.
    I did tell you why. You changed the SHFILEINFO definition after inaccurately claiming MSDN had the incorrect def from win32api.txt.

    This is what you originally used, posted, and said MSDN had:

    Code:
    Public Type SHFILEINFO
        hIcon As Long
        iIcon As Long
        dwAttributes As SFGAO_Flags
        lpDisplayName As Long
        lpTypeName As Long
    End Type
    This is what you now have:

    Code:
    Private Type SHFILEINFOW
            hIcon As Long                      '  out: icon
            iIcon As Long          '  out: icon index
            dwAttributes As Long               '  out: SFGAO_ flags
            szDisplayName As String * 256 '  out: display name (or path)
            szTypeName As String * 80         '  out: type name
    End Type
    This is (almost) equivalent to what I posted and very different from what you originally posted. Even though you are not using those members, SHGetFileInfo validates whether the struct is a certain minimum size, and fails if it's not. It's still wrong, since MAX_PATH is 260, but it passes the minimum size check, so you won't have problems until you try using longer path names or retrieving the type name.

    This is hardly the first time you've had issues because you make small changes you think are inconsequential from the working code samples provided by me and others. It took me a long time to learn all these little nuances and esoteric details so you shouldn't feel bad about missing things when you first get into something as complicated and finnicky as shell programming, but when Brad's code worked, or Edanmo's code worked, in their samples, but not mine, I didn't go blaming their code.
    Last edited by fafalone; Dec 2nd, 2023 at 02:43 AM.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    I have found a similar problem discussed in Code Project Forum and similar flag settings as mine.
    So it doesn't have anyting todo with the Type members. It's an COM error of some sort.

    https://www.codeproject.com/Question...dled-exception

  17. #17
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,546

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    The thread has the solution for that persons problem (it's marked [SOLVED]), he hadn't initialized COM and SHGetFileInfo uses COM internally.

    VB6 initializes COM for you before your code runs.

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Ok, but my problem started just some days ago.
    Can it be any other installations of apps that can interfear and make it crash?

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    724

    Re: Fatal chrashing when enumerting a folder for thumbnails.

    Here is one more similar problem.
    https://stackoverflow.com/questions/...dled-exception
    This has never happened on my Win11 machine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width