#include <WinAPI.au3>
#include <File.au3>
#include <WinAPIIcons.au3>
MsgBox(0, 0, _TEST())
Exit
Func _TEST()
If StringInStr(@OSArch, '86') Then
$usek = "HKCU"
Else
$usek = "HKCU64"
EndIf
$USERKEY = $usek & "\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
$sIn = @SystemDir & '\shell32.dll'
$dOt = RegRead($USERKEY, 'Desktop') & '\test.ico'
$index = -15
If _TEST_ExtractIconToFile($sIn, $index, $dOt) Then
Return 0 ; No error
Else
Return @error ; Error occurred
EndIf
EndFunc ;==>_TEST
Func _TEST_ExtractIconToFile($sFile, $iIndex, $sDest)
Local $hIcon = _MyExtractIconEx($sFile, $iIndex)
If $hIcon Then
Local $bResult = _MySaveIcon($hIcon, $sDest)
_WinAPI_DestroyIcon($hIcon)
Return $bResult
EndIf
Return SetError(1, 0, False) ; Set an error if the icon could not be extracted.
EndFunc
Func _MyExtractIconEx($sFilePath, $iIndex)
Local $aResult = DllCall('shell32.dll', 'uint', 'ExtractIconExW', 'wstr', $sFilePath, 'int', $iIndex, 'ptr', 0, 'ptr', 0, 'int', 1)
If @error Or Not $aResult[4] Then
Return SetError(@error, 0, 0)
EndIf
Return $aResult[4]
EndFunc
Func _MySaveIcon($hIcon, $sDest)
Local $aIconData = DllCall('user32.dll', 'uint', 'CopyImage', 'ptr', $hIcon, 'uint', 0, 'int', 0, 'int', 0, 'uint', 0)
If @error Or Not $aIconData[0] Then
Return SetError(@error, 0, False)
EndIf
Local $aIconBinary = DllCall('user32.dll', 'uint', 'GetIconInfo', 'ptr', $aIconData[0])
If @error Or Not $aIconBinary[0] Then
Return SetError(@error, 0, False)
EndIf
Local $hFile = FileOpen($sDest, $FO_OVERWRITE + $FO_CREATEPATH)
If $hFile = -1 Then
Return SetError(1, 0, False)
EndIf
Local $bResult = FileWrite($hFile, $aIconBinary[0])
FileClose($hFile)
If Not $bResult Then
Return SetError(2, 0, False)
EndIf
Return True
EndFunc