Jump to content

What is wrong here?


Recommended Posts

#Include <File.au3>
#Include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
#include <GUIImageList.au3>
#Include <Constants.au3>

Dim $FileListC, $FileListA
#Region ### START Koda GUI section ### 
$Form1 = GUICreate("Form1", 620, 440, 192, 124)
$ListView1 = GUICtrlCreateListView("", 12, 12, 598, 414)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;~ #RequireAdmin
FileList()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func FileList()
    $hImage = _GUIImageList_Create()
    _GUICtrlListView_AddColumn($ListView1,'Archivo', 250)
    $FileListC=_FileListToArray(@DesktopDir,"*",2)
    $FileListA=_FileListToArray(@DesktopDir,"*",1)
    If @Error=1 Then
        ConsoleWriteError ("No Files\Folders Found.")
    Else
        For $ie=1 To $FileListC[0]
            _GUICtrlListView_AddItem ($ListView1,$FileListC[$ie],$ListView1)
;~          ConsoleWrite (_GetIcon($FileListC[$ie])&_GetIcon($FileListC[$ie],1)&@CR)
            _GUIImageList_AddIcon($hImage,_GetIcon($FileListC[$ie]),_GetIcon($FileListC[$ie],1))
        Next
        For $ia=1 To $FileListA[0]
            _GUICtrlListView_AddItem ($ListView1,$FileListA[$ia],$ListView1)
            _GUIImageList_AddIcon($hImage,_GetIcon($FileListA[$ia]),_GetIcon($FileListA[$ia],1))
        Next
        _GUICtrlListView_SetImageList($ListView1, $hImage,1)
    EndIf
EndFunc

Func _GetIcon($file, $ReturnType = 0) ;rexx function
    $FileType = StringSplit($file, ".")
    $FileType = $FileType[UBound($FileType) - 1]
    $FileParam = RegRead("HKEY_CLASSES_ROOT\." & $FileType, "")
    $DefaultIcon = RegRead("HKEY_CLASSES_ROOT\" & $FileParam & "\DefaultIcon", "")

    If Not @error Then
        $IconSplit = StringSplit($DefaultIcon, ",")
        ReDim $IconSplit[3]
        $Iconfile = $IconSplit[1]
        $IconID = $IconSplit[2]
        ConsoleWrite("OK!"&@CR)
    Else
        ConsoleWriteError("ERROR!:"&@error&@CR)
        $Iconfile = @SystemDir & "\shell32.dll"
        $IconID = 3
    EndIf
    
    If $ReturnType = 0 Then
        Return $Iconfile
    Else        
        Return $IconID
    EndIf
EndFunc  ;==>_GetIcon

The icons are the same in all the files.

I don't know what is wrong, please help...

Bye

Edited by PAULOGARCIA2005
Link to comment
Share on other sites

The third parameter for the _GUICtrlListView_AddItem() function should not be the listview control's ID. I guess that you'll find along the way that you've assumed different things incorrectly. Try to add ConsoleWrite() call here and there so you can see what values you're working with. For example, not every extension in the registry has DefaultIcon subkey. You may want to take a look here to see a quite simpler approach to get a specific extension's associate icon. For exe files you can use the ExtractIcon() API function or _WinAPI_ExtractIconEx() which is defined within WinAPI.au3 library.

Link to comment
Share on other sites

Ok, i do some changes:

#Include <File.au3>
#Include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
#include <GUIImageList.au3>
#Include <Constants.au3>

Dim $FileListC, $FileListA, $ruta=@SystemDir
#Region ### START Koda GUI section ### 
$Form1 = GUICreate("Test", 620, 440, 192, 124)
$ListView1 = GUICtrlCreateListView("", 12, 12, 598, 414)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;~ #RequireAdmin
FileList()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func FileList()
    $hImage = _GUIImageList_Create(16, 16, 5, 3,'',2048)
    _GUICtrlListView_AddColumn($ListView1,'Archivo', 275)
    _GUICtrlListView_AddColumn($ListView1,'Tipo', 205)
    $FileListC=_FileListToArray($ruta,"*",2)
    $FileListA=_FileListToArray($ruta,"*",1)
    If @Error=1 Then
        ConsoleWriteError ("No Files\Folders Found.")
    Else
        For $ie=1 To $FileListC[0]
            _GUIImageList_AddIcon($hImage,@SystemDir&'\shell32.dll',3)
            _GUICtrlListView_AddItem ($ListView1,$FileListC[$ie],$ie-1)
        Next
        For $ia=1 To $FileListA[0]
            $ico2=_GetRegDefIcon($FileListA[$ia])
            If $ico2[1] = -1 Or $ico2[1]=-2 Then 
                _GUIImageList_AddIcon($hImage,$ico2[0]);,$ico2[1])
            Else
                _GUIImageList_AddIcon($hImage,$ico2[0],$ico2[1])
            EndIf
            _GUICtrlListView_AddItem ($ListView1,$FileListA[$ia],$ia-1)
            ConsoleWrite ($ico2[0]&@CR&$ico2[1])
        Next
        _GUICtrlListView_SetImageList($ListView1, $hImage,1)
    EndIf
EndFunc

Func _GetRegDefIcon($Path)
    ConsoleWrite ($Path)
    Const $DF_NAME  = @SystemDir & '\shell32.dll'
    Const $DF_INDEX = 0  
    Local $filename, $name, $ext, $count, $curver, $defaulticon, $ret[2] = [$DF_NAME, $DF_INDEX] 
    $filename = StringTrimLeft($Path, StringInStr($Path, '\', 0, -1))
    $count = StringInStr($filename, '.', 0, -1)
    If $count > 0 Then
        $count = StringLen($filename) - $count + 1
    EndIf     
    $name = StringStripWS(StringTrimRight($filename, $count), 3)
    $ext = StringStripWS(StringRight($filename, $count - 1), 8)
    If (StringLen($name) = 0) or (StringLen($ext) = 0) Then
        Return $ret     
    EndIf     
    $curver = StringStripWS(RegRead('HKCR\' & RegRead('HKCR\' & '.' & $ext, '') & '\CurVer', ''), 3)
    ConsoleWrite ($curver)
    If (@error) or (StringLen($curver) = 0) Then
        $defaulticon =  _WinAPI_ExpandEnvironmentStrings(StringReplace(RegRead('HKCR\' & RegRead('HKCR\' & '.' & $ext, '') & '\DefaultIcon', ''), '''', ''))
    Else
        $defaulticon =  _WinAPI_ExpandEnvironmentStrings(StringReplace(RegRead('HKCR\' & $curver & '\DefaultIcon', ''), '''', ''))
    EndIf
    $count = StringInStr($defaulticon, ',', 0, -1)
    If $count > 0 Then
        $count = StringLen($defaulticon) - $count
        $ret[0] = StringStripWS(StringTrimRight($defaulticon, $count + 1), 3)
        If $count > 0 Then
            $ret[1] = StringStripWS(StringRight($defaulticon, $count), 8)
        EndIf
    Else
        $ret[0] = StringStripWS(StringTrimRight($defaulticon, $count), 3)
    EndIf     
    If StringLeft($ret[0], 1) = '%' Then
        $count = DllCall('shell32.dll', 'int', 'ExtractIcon', 'int', 0, 'str', $Path, 'int', -1)
        If $count[0] = 0 Then
            $ret[0] = $DF_NAME
            If StringLower($ext) = 'exe' Then
                $ret[1] = 2
            Else 
                $ret[1] = 0
            EndIf
        Else
            $ret[0] = StringStripWS($Path, 3)
            $ret[1] = 0
        EndIf
    Else
        If (StringLen($ret[0]) > 0) and (StringInStr($ret[0], '\', 0) = 0) Then
            $ret[0] = @SystemDir & '\' & $ret[0]
        EndIf
    EndIf
    If Not FileExists($ret[0]) Then
        $ret[0] = $DF_NAME
        $ret[1] = $DF_INDEX
    EndIf
    If $ret[1] < 0 Then
        $ret[1] = - $ret[1]
    Else
        $ret[1] = - $ret[1] - 1
    EndIf
    Return -$ret 
EndFunc; _GetRegDefIcon

But i have the next problems:

  • If i try whith a directory with only files the script works fine, but if i try with a diretory with files AND FOLDERS the script fails
  • The script launchs an error with some specifics extensions like docx or vbs, or specific files
I don't know what to do. Edited by PAULOGARCIA2005
Link to comment
Share on other sites

#Include <File.au3>
#Include <Array.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
#include <GUIImageList.au3>
#Include <Constants.au3>
Opt('ExpandEnvStrings', 1)

Dim $FileListC, $FileListA, $ruta= 'c:\'
#Region ### START Koda GUI section ### 
$Form1 = GUICreate("Test", 620, 440, 192, 124)
$ListView1 = GUICtrlCreateListView("", 12, 12, 598, 414)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;~ #RequireAdmin
FileList()
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Func FileList()
    $hImage = _GUIImageList_Create(16, 16, 5, 5,4,2048)
    _GUICtrlListView_AddColumn($ListView1,'Archivo', 275)
    _GUICtrlListView_AddColumn($ListView1,'Tipo', 205)
    _GUICtrlListView_SetImageList($ListView1, $hImage, 1)
    
    $FileListC=_FileListToArray($ruta,"*",2)
    $FileListA=_FileListToArray($ruta,"*",1)
    $iIndex = 0
    
    If @Error=1 Then
        ConsoleWriteError ("No Files\Folders Found.")
    Else
        _GUIImageList_AddIcon($hImage,@SystemDir&'\shell32.dll',3)
        For $ie=1 To $FileListC[0]
            _GUICtrlListView_AddItem ($ListView1,$FileListC[$ie], $iIndex)
        Next
        
        _GUIImageList_AddIcon($hImage, @SystemDir & '\shell32.dll', 0)
        For $ia=1 To $FileListA[0]
            $ico2=_GetRegDefIcon($FileListA[$ia])
            
            If $ico2[1] = -1 Or $ico2[1]=-2 Then 
                $iIndex = 1
            Else
                If $ico2[1] < 0 Then $ico2[1] = '"' & $ico2[1] & '"'
                $iIndex = _GUIImageList_AddIcon($hImage,$ico2[0], $ico2[1])
            EndIf
            _GUICtrlListView_AddItem ($ListView1,$FileListA[$ia], $iIndex)
        Next
    EndIf
EndFunc

Func _GetRegDefIcon($Path)
    Const $DF_NAME  = @SystemDir & '\shell32.dll'
    Const $DF_INDEX = 0  
    Local $filename, $name, $ext, $count, $curver, $defaulticon, $ret[2] = [$DF_NAME, $DF_INDEX] 
    $filename = StringTrimLeft($Path, StringInStr($Path, '\', 0, -1))

    $count = StringInStr($filename, '.', 0, -1)
    If $count > 0 Then
        $count = StringLen($filename) - $count + 1
    EndIf     
    $name = StringStripWS(StringTrimRight($filename, $count), 3)
    $ext = StringStripWS(StringRight($filename, $count - 1), 8)
    
    If (StringLen($name) = 0) or (StringLen($ext) = 0) Then  Return $ret
     
    $curver = StringStripWS(RegRead('HKCR\' & RegRead('HKCR\' & '.' & $ext, '') & '\CurVer', ''), 3)
    
    If (@error) or (StringLen($curver) = 0) Then
        $defaulticon =  StringReplace(RegRead('HKCR\' & RegRead('HKCR\' & '.' & $ext, '') & '\DefaultIcon', ''), "'", '')
    Else
        $defaulticon =  StringReplace(RegRead('HKCR\' & $curver & '\DefaultIcon', ''), "'", '')
    EndIf
    $count = StringInStr($defaulticon, ',', 0, -1)
    If $count > 0 Then
        $count = StringLen($defaulticon) - $count
        $ret[0] = StringStripWS(StringTrimRight($defaulticon, $count + 1), 3)
        If $count > 0 Then
            $ret[1] = StringStripWS(StringRight($defaulticon, $count), 8)
        EndIf
    Else
        $ret[0] = StringStripWS(StringTrimRight($defaulticon, $count), 3)
    EndIf     
    ConsoleWrite($ret[0] & @CRLF)
    If StringLeft($ret[0], 1) = '%' Then
        $count = DllCall('shell32.dll', 'int', 'ExtractIcon', 'int', 0, 'str', $Path, 'int', -1)
        If $count[0] = 0 Then
            $ret[0] = $DF_NAME
            If StringLower($ext) = 'exe' Then
                $ret[1] = 2
            Else 
                $ret[1] = 0
            EndIf
        Else
            $ret[0] = StringStripWS($Path, 3)
            $ret[1] = 0
        EndIf
    Else
        If (StringLen($ret[0]) > 0) and (StringInStr($ret[0], '\', 0) = 0) Then
            $ret[0] = @SystemDir & '\' & $ret[0]
        EndIf
    EndIf
    If Not FileExists($ret[0]) Then
        $ret[0] = $DF_NAME
        $ret[1] = $DF_INDEX
    EndIf

    Return $ret 
EndFunc; _GetRegDefIcon

It doesn't suppose to fix anything, just to hopefully not crash. You can read the link I've posted before, you might find it useful and easier in most of the cases.

Edited by Authenticity
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...