Jump to content
Sign in to follow this  
KaFu

_FileGetIcon()

Recommended Posts

KaFu

Yeah, another version ^_^... did a lot of tinkering on this one

#578091

by Holger Kotsch and Kastout (mrRevoked) (kudos for your work).

My goal was to get as close to the windows shell as possible.

The code is still quiet dirty and full of comments and debug lines, because this is still WIP, maybe anyone of you has a clue to help me :huh2:.

I appended a zip with some file extensions (some zero size files and a compiled version of this script as .exe), on my system the unpack without problem, hope your packer doesn't skip the 0 byte files :)...

Now here what's still left (miiiinor). PHP files are associated with wordpad.exe on my computer (if it's not the case on your's, I guess you won't be able to reproduce the issue). The icon in au3 seems to be correct, except that in explorer the icon is much smaller and within a blank document icon. Browsed through the reg without any luck, and now I'm tired and hungry :)...

; http://msfn.org/board/lofiversion/index.php/t76486.html
; http://msdn.microsoft.com/en-us/library/bb776870.aspx
; http://msdn.microsoft.com/en-us/library/bb776871(VS.85).aspx

; HKEY_CLASSES_ROOT\SystemFileAssociations
; HKEY_CLASSES_ROOT\IrfanView.JPG
; HKEY_CLASSES_ROOT\IrfanView.PSD



#cs
http://msdn.microsoft.com/en-us/library/cc144150(VS.85).aspx
Whenever the Shell needs to query for file association attributes for a file type, it creates an array of registry keys. The Shell checks the following registry keys, in order of decreasing priority.

   1. User Customized Key. This key is used only if the user has customized a file extension's association to override the default. Typically this is either an application key or another ProgID.
   2. ProgID Key. This key is specified as the default value of the extension key.
   3. SystemFileAssociations Key. See below.
   4. SystemFileAssociations PerceivedType Key. See below.
   5. Base Class Key. For files, this is the ASTERISK(*) key.
   6. AllFileSystemObjects Key. All files and file folders.
#ce


#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <ButtonConstants.au3>
#include <File.au3>
#include <GuiListView.au3>
;#include <array.au3>
;#include <winapi.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("AForm1", 633, 454, 193, 115)
$ListView1 = GUICtrlCreateListView("Filename |Size|Filetype ", 20, 22, 540, 373)
$select_button = GUICtrlCreateButton('Select',570,30,40)
;~ GUICtrlSetImage($ListView1, "shell32.dll", 3)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
;loadfiles(@ScriptDir & '\file_extensions')
;loadfiles(@DesktopDir)
;loadfiles(@DesktopCommonDir)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $select_button
            $var = FileSelectFolder('Select Target','',0,@ScriptDir)
            if $var then 
                _GUICtrlListView_DeleteAllItems($ListView1)
                loadfiles($var)
            EndIf
    EndSwitch
WEnd

Func loadfiles($folder)
    $files = _FileListToArray($folder, "*.*", 1)
    Local $szIconFile1, $nIcon1 = 0, $ListViewItem[UBound($files)]
    For $i = 1 To UBound($files) - 1
        $ListViewItem[$i] = GUICtrlCreateListViewItem( _
        $files[$i] & "|" & _
        Int(FileGetSize($folder & '\' & $files[$i])) & "|" & _
        StringTrimLeft($files[$i], StringInStr($files[$i], ".", 0, -1)) _
        , $ListView1)
       
        Local $sFile = $files[$i]
       
        $azShortInfo = FileGetShortcut($sFile)
        If IsArray($azShortInfo) Then $sFile = $azShortInfo[0]
       
        If GUICtrlSetImage($ListViewItem[$i], $sFile) = 0 Then
            Local $sIconFile, $nIcon = 0
            If _FileGetIcon($sIconFile, $nIcon, $sFile, $folder) Then
                ;ConsoleWrite($sFile &"="& $sIconFile &","& $nIcon & @lf & @lf)
                $nIcon = GUICtrlSetImage($ListViewItem[$i], $sIconFile, $nIcon)
                If $nIcon = 0 Then GUICtrlSetImage($ListViewItem[$i], @SystemDir & '\shell32.dll', 0) ; whatever icon you want
            Else
                GUICtrlSetImage($ListViewItem[$i], @SystemDir & '\shell32.dll', 0)
            EndIf
        EndIf
       
    Next
EndFunc   ;==>loadfiles


;Author - Holger Kotsch, modifed by mrRevoked, heavily modified by KaFu
Func _FileGetIcon(ByRef $szIconFile, ByRef $nIcon, $szFile, $szFolder)
    Dim $szRegDefault = "", $szDefIcon = "", $adetails = ""
    
    ;$executable = _WinAPI_FindExecutable($szFolder & '\' & $szFile)
    ;$executable_icons = _WinAPI_ExtractIconEx($executable, -1, 0, 0, 0)
    ;ConsoleWrite ($executable & ' => ' & $executable_icons & @crlf)
    
    $nIcon = 0
    $szExt = StringTrimLeft($szFile, StringInStr($szFile, ".", 0, -1))
    
    if $szExt = "lnk" Then
        $adetails = FileGetShortcut($szFolder & '\' & $szFile)
        if not @error Then
            $szDefIcon = $adetails[0] & ',' & $adetails[5]
        Else
            $szDefIcon = ""
        EndIf
    Else
        $szRegDefault = RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." & $szExt, "ProgID")
        
        If $szRegDefault = "" Then $szRegDefault = RegRead("HKCR\." & $szExt & "\DefaultIcon", "")
        If $szRegDefault = "" Then $szRegDefault = RegRead("HKCR\." & $szExt, "")
        
        If $szRegDefault <> "" Then 
            $szDefIcon = RegRead("HKCR\" & $szRegDefault & "\DefaultIcon", "")  
            if $szDefIcon = "" then $szDefIcon = RegRead("HKCR\." & $szExt & "\DefaultIcon", "")    
            ;ConsoleWrite($szExt & '  |  ' & $szRegDefault & '  |  ' & $szDefIcon & @crlf)
        Else
            $szRegDefault = RegRead("HKCR\." & $szExt, "PerceivedType")
            If $szRegDefault <> "" Then 
                $szRegDefault = RegRead("HKCR\SystemFileAssociations\" & $szRegDefault & "\DefaultIcon","")
                $szDefIcon = StringReplace($szRegDefault,'%SystemRoot%\system32',@SystemDir)
            endif
        EndIf
        
        if $szDefIcon = "" and $szRegDefault <> "" Then
            $szDefIcon = RegRead("HKCR\" & $szRegDefault & "\CurVer","") ; ADOBE PERVERSION....
            if $szDefIcon then
                $szDefIcon = RegRead("HKCR\" & $szDefIcon & "\DefaultIcon","")
            Else
                $szDefIcon = RegRead("HKCR\" & $szRegDefault & "\shell\open\command","")
                $szDefIcon = StringReplace($szDefIcon,'%ProgramFiles%',@ProgramFilesDir)
                $szDefIcon = StringReplace($szDefIcon,' "%1"','')
            endif
        EndIf
    endif

    ConsoleWrite($szExt & '  |  ' & $szRegDefault & '  |  ' & $szDefIcon & @crlf)
    
    If $szDefIcon = "" Then
        $szIconFile = "shell32.dll"
    ElseIf $szDefIcon = "%1" Then
        $szIconFile = $szFolder & '\' & $szFile
        $nIcon = 1
        ;ConsoleWrite($szIconFile & @tab & $nIcon)
    Else
        $arSplit = StringSplit($szDefIcon, ",")
        If IsArray($arSplit) Then
            $szIconFile = FileGetLongName(StringReplace($arSplit[1], '"', ''))
            ;ConsoleWrite($szExt & @tab & $szRegDefault & @tab & $szIconFile & @crlf)   
            If $arSplit[0] > 1 Then $nIcon = $arSplit[2]
            If StringInStr($arSplit[1], "shell32.dll") Then 
                $nIcon += 81
            Else
                $nIcon = - $nIcon - 1
            EndIf
            
        Else
            Return 0
        EndIf
    EndIf
    Return 1
EndFunc   ;==>FileGetIcon
Edited by KaFu

Share this post


Link to post
Share on other sites
UEZ

Line 37: loadfiles(@ScriptDir & '\file_extensions') -> make problemes if it doesn't exits :)

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
KaFu

Extract the appended file to

@scriptdir & '\file_extensions'

... this is about function, not a fully working program :), GUI and other nonsense only provided for testing...

Edited by KaFu

Share this post


Link to post
Share on other sites
UEZ

Extract the appended file to

@scriptdir & '\file_extensions'

... this is about function, not a fully working program ^_^ , GUI and other nonsense only provided for testing...

Didn't saw the attachment :):)

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
KaFu

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
Sign in to follow this  

×