Jump to content

Change Autoit EXE icon from external script


Terenz
 Share

Recommended Posts

Hello,

My goal is change an autoit compiled icon to another one, after i have compiled

After some search i have found this example:

https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_BeginUpdateResource.htm

Which seems the good one for my goal but if fail on ReadFile ( return 0, but the handle is present ). I have try with several icon, same result. For example this script work:

https://pastebin.com/5ru8H0cN

But is too overcomplex with several function. I just want to set two variable, the EXE and the ICON. I don't have any interest to other resource, or the information, or the GUI etc. something very simple without using ReasHack or other tools since it works just using autoit

Thanks

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

I tried to pull out functions in the script to eliminate the gui, but I couldn't get it to work. It's a complicated script. Basically, you need to modify the following functions:

Replace()

_ResRep

_ResourceGetAsRaw

_ResourceEnumerate

_LittleEndianBinaryToInt

If you are able to get it to work, it will still be a large and complicated script.

Link to comment
Share on other sites

Aww heck, the _WinAPI_BeginUpdateResource example script you mentioned works. I modified the script a little and here it is. Just change the name of your exe and the icon and there you go. It's still kind of long, but if you wanted, you could convert some of the code into a #include file to have super short and simple code.

#include <WinAPIRes.au3>

Global Const $g_sExe = @ScriptDir & '\photofiltre7.exe' ; this is the name of the executable

Global Const $tagICONRESDIR = 'byte Width;byte Height;byte ColorCount;byte Reserved;ushort Planes;ushort BitCount;dword BytesInRes;ushort IconId;'
Global Const $tagNEWHEADER = 'ushort Reserved;ushort ResType;ushort ResCount;' ; & $tagICONRESDIR[ResCount]

; Select icon to update resource
Local $sIcon = @ScriptDir & "\icon.ico" ; this is the name of your icon
If Not $sIcon Then
    Exit
EndIf

Local $iError = 1
Do
    ; Begin update resources
    Local $hUpdate = _WinAPI_BeginUpdateResource($g_sExe)
    If @error Then
        ExitLoop
    EndIf
    ; Read .ico file as raw binary data into the structure
    Local $tIcon = DllStructCreate('ushort Reserved;ushort Type;ushort Count;byte[' & (FileGetSize($sIcon) - 6) & ']')
    Local $hFile = _WinAPI_CreateFile($sIcon, 2, 2)
    If Not $hFile Then
        ExitLoop
    EndIf
    Local $iBytes = 0
    _WinAPI_ReadFile($hFile, $tIcon, DllStructGetSize($tIcon), $iBytes)
    _WinAPI_CloseHandle($hFile)
    If Not $iBytes Then
        ExitLoop
    EndIf
    ; Add all icons from .ico file into the RT_ICON resources identified as 400, 401, etc., and fill group icons structure
    Local $iCount = DllStructGetData($tIcon, 'Count')
    Local $tDir = DllStructCreate($tagNEWHEADER & 'byte[' & (14 * $iCount) & ']')
    Local $pDir = DllStructGetPtr($tDir)
    DllStructSetData($tDir, 'Reserved', 0)
    DllStructSetData($tDir, 'ResType', 1)
    DllStructSetData($tDir, 'ResCount', $iCount)
    Local $tInfo, $iSize, $tData, $iID = 400
    Local $pIcon = DllStructGetPtr($tIcon)
    For $i = 1 To $iCount
        $tInfo = DllStructCreate('byte Width;byte Heigth;byte Colors;byte Reserved;ushort Planes;ushort BPP;dword Size;dword Offset', $pIcon + 6 + 16 * ($i - 1))
        $iSize = DllStructGetData($tInfo, 'Size')
        If Not _WinAPI_UpdateResource($hUpdate, $RT_ICON, $iID, 0, $pIcon + DllStructGetData($tInfo, 'Offset'), $iSize) Then
            ExitLoop 2
        EndIf
        $tData = DllStructCreate($tagICONRESDIR, $pDir + 6 + 14 * ($i - 1))
        DllStructSetData($tData, 'Width', DllStructGetData($tInfo, 'Width'))
        DllStructSetData($tData, 'Height', DllStructGetData($tInfo, 'Heigth'))
        DllStructSetData($tData, 'ColorCount', DllStructGetData($tInfo, 'Colors'))
        DllStructSetData($tData, 'Reserved', 0)
        DllStructSetData($tData, 'Planes', DllStructGetData($tInfo, 'Planes'))
        DllStructSetData($tData, 'BitCount', DllStructGetData($tInfo, 'BPP'))
        DllStructSetData($tData, 'BytesInRes', $iSize)
        DllStructSetData($tData, 'IconId', $iID)
        $iID += 1
    Next
    ; Add new RT_GROUP_ICON resource named as "MAINICON"
    If Not _WinAPI_UpdateResource($hUpdate, $RT_GROUP_ICON, 'MAINICON', 0, $pDir, DllStructGetSize($tDir)) Then
        ExitLoop
    EndIf
    $iError = 0
Until 1

; Save or discard changes of the resources within an executable file
If Not _WinAPI_EndUpdateResource($hUpdate, $iError) Then
    $iError = 1
EndIf

; Show message if an error occurred
If $iError Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Unable to change resources.')
EndIf

 

Link to comment
Share on other sites

Ok, I'm feeling generous today. I made the large portion of code into an #include. Here's and example of how you would script with it:

#include <WinAPIRes.au3>
#include <ChangeIcon.au3>

$sIcon = @ScriptDir & "\icon.ico" ; this is the name of your icon
$sExe = @ScriptDir & '\photofiltre7.exe' ; this is the name of the executable

_ChangeIcon($sIcon, $sExe) ; in this case, you must specify the icon first then the exe

The function to change the icon is called _ChangeIcon. Don't forget to download the #include attachment otherwise it won't work.

ChangeIcon.au3

Link to comment
Share on other sites

Thanks but maybe i wasn't clear...

Which seems the good one for my goal but if fail on ReadFile (return 0, but the handle is present )

an autoit compiled icon to another one

I have try with with UPX or not, Always fail

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Link to comment
Share on other sites

I think he's having trouble expressing exactly what the problem is. I think he's trying to change out icons on multiple executables and is getting an error if one is missing. I am confused because he is sometimes using the word icon where he meant exe.

Terenz, can you try to explain it by using an example or a list of your files?

Link to comment
Share on other sites

A list of what? I'll take a script with MsgBox, i'll compile, i'll try to change the icon. Fail on ReadFile funciton, return 0. There is nothing wrong on my side, i'm using a fresh VM

 

@Jos if i'm using this:

 

 

The resource will be updated but the exe doesn't work anymore

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

  • Developers
23 minutes ago, Terenz said:

A list of what? I'll take a script with MsgBox, i'll compile, i'll try to change the icon. Fail on RedFile funciton, return 0. There is nothing wrong on my side, i'm using a fresh VM

That sounds like the right attitude to get your issue sorted. ;)

Good luck with that.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@Jos can you please check it out my previous post? Your script work on change the icon but corrupt in some way the EXE, with or without UPX. It doesn' start anymore. I don't have any AV on the machine. Thanks

P.S. I don't have understand the "attitude", english isn't my motehr language

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Now I understand. I thought my script was giving you an error. I just created a msgbox and compiled as x86 and x64. Then I changed the icon. Both msgbox work fine. All I can say is try it on a different computer. Also, you might want to disable your antivirus while testing. Good luck.

Link to comment
Share on other sites

  • Developers

Don't use an 11 years old script and expect that to work. Autoit3wrapper has quite a bit evolved since then.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...