Heaventools

   English English  Deutsch Deutsch

home  products  resource tuner console  sample scripts library

Edit Icons: Replacing Icon Resource with Icon from .ICO File

In this code example, we demonstrate how to effortlessly change icons in EXE and DLL files using Resource Tuner Console.

The script adds or replaces icons in a test EXE file, sourcing icons from .ico files located in the "\Src" folder. Once the modifications are complete, the updated file is saved in the "Release" directory.

The code demonstrates the following icon modifications in executables:

  • Replacing the main icon with new icons from an .ICO file, including the large 256x256 PNG-compressed icon.
  • Sorting out the added icons in the correct order.
  • Adding one more icon to the newly created Icon Group.
  • Outputting the changes in the Resource Tree to the rtc.log file.
  • Restoring the original timestamps on the file after saving.

The script will add or replace icons in "DemoApp1.exe" with icons from .ico files located in the "Src" folder.

Upon execution, the resulting files with updated icons will be saved in the directory named "Release". You can review the changes made in the Resource Tree by checking the rtc.log file.

The script also features a method to get the original file timestamps for demo purposes. It allows you to restore the original timestamps (CreationTime, LastAccess, and LastWrite) on the file after performing any file operations.

For more comprehensive details and guidance, refer to the Step-by-Step Guide To Using Scripts, which provides further insights on using Resource Tuner Console.

Note: when copying out the script code, please make sure there's no line breaks. This is a requirement of VBScript: the entire command must be on one line.

Edit Icons

Sub Main
  PEFileProxy.PostDebugString "PE file header checksum updating is enabled." 
  PEFileProxy.UpdateCheckSum = True 

  PEFileProxy.PostDebugString "The creation of a backup copy is disabled." 
  PEFileProxy.CreateBackUp = False

  '-------- Getting the original file timestamps (for demo purpose) --------
  'To restore the original timestamps on the file after saving,
  'we need to get the timestamps before performing any file operations.

  SRCFILE = ".\src\DemoApp1.exe"
  PEFileProxy.PostDebugString ""
  PEFileProxy.PostDebugString "Getting CreationTime, LastAccessTime and 
LastWriteTime for file '" & SRCFILE & "'..."

  set fso = CreateObject("Scripting.FileSystemObject")
  set f = fso.GetFile(SRCFILE)
  DCR = f.DateCreated
  DLM = f.DateLastModified
  DLA = f.DateLastAccessed
  PEFileProxy.PostDebugString "Created      : " & DCR
  PEFileProxy.PostDebugString "Last Modified: " & DLM
  PEFileProxy.PostDebugString "Last Accessed: " & DLA
  PEFileProxy.PostDebugString ""
  '-------------------------------------------------------------------------

  'Open file
  PEFileProxy.PostDebugString "Opening the file..."
  PEFileProxy.OpenFile SRCFILE
  if (PEFileProxy.Terminated) then
    'Issue a warning in case of error
    PEFileProxy.PostDebugString "Fatal error when opening this file."
  else
    PEFileProxy.PostDebugString "File opened OK."
    if (not PEFileProxy.HasResources) then
      PEFileProxy.PostDebugString "The file contains no resources."
    else
      PEFileProxy.PostDebugString "The file contains resources."

      'This specifies the default language      
      LangID = 0 ' Default

      PEFileProxy.PostDebugString "Change/add the main application icon..."
      ResourcesProxy.ChangeIcon "", LangID, CREATE_IF_NOT_EXIST, 
REPLACE_IF_ITEM_EXISTS_ELSE_ADD, ".\src\vista_iconset.ico"

      PEFileProxy.PostDebugString "Add one more Icon Group with one more icon..."
      ResourcesProxy.ChangeIcon "New Icon Group", LangID, CREATE_IF_NOT_EXIST, 
REPLACE_IF_ITEM_EXISTS_ELSE_ADD, ".\src\icon_3_32x32_8bit.ico"

      'Build and output the Resource Tree to log to show the changes made
      PEFileProxy.PostDebugString ""
      PEFileProxy.PostDebugString "Resource Tree built by RTC:"
      ResourcesProxy.ResourceTreeToLog
      PEFileProxy.PostDebugString ""

      'Save file
      PEFileProxy.PostDebugString "Saving file..."
      DESTFILE = ".\release\DemoApp1.exe"
      PEFileProxy.SaveAsNewImage DESTFILE

  '--------------------------------------------------------------------------
  'Restoring the original timestamps (CreationTime, LastAccess and LastWrite)
      PEFileProxy.PostDebugString ""
      PEFileProxy.PostDebugString "Restore CreationTime, LastAccessTime and 
LastWriteTime for file '" & DESTFILE & "'..."
      if not ScriptUnit.SetFileTime(DESTFILE, DCR, DLA, DLM) then
       'Issue a warning in case of error
       PEFileProxy.PostDebugString "Restoring CreationTime, LastAccessTime and 
LastWriteTime for file '" & DESTFILE & "'  failed."
      else
       PEFileProxy.PostDebugString "CreationTime, LastAccessTime and 
LastWriteTime for file '" & DESTFILE & "' restored OK."
      end if
      PEFileProxy.PostDebugString ""

  '--------------------------------------------------------------------------

    end if
    PEFileProxy.PostDebugString "Closing this file..."
    PEFileProxy.CloseFile
  end if
end Sub

After executing the script, RTC generates a log file displaying the Resource Tree changes made during the process. You can conveniently review these changes in the log file to understand the modifications performed:

Resource Tree built by RTC:
----------------------------------------
 [Cursor (1)]
     1: 32x32 1b, Neutral (0)
     2: 32x32 1b, Neutral (0)
     3: 32x32 1b, Neutral (0)
     4: 32x32 1b, Neutral (0)
     5: 32x32 1b, Neutral (0)
     6: 32x32 1b, Neutral (0)
     7: 32x32 1b, Neutral (0)
 [Bitmap (2)]
     BBABORT: Neutral (0)
     BBALL: Neutral (0)
     BBCANCEL: Neutral (0)
     BBCLOSE: Neutral (0)
     BBHELP: Neutral (0)
     BBIGNORE: Neutral (0)
     BBNO: Neutral (0)
     BBOK: Neutral (0)
     BBRETRY: Neutral (0)
     BBYES: Neutral (0)
     CDROM: Neutral (0)
     CLOSEDFOLDER: Neutral (0)
     CURRENTFOLDER: Neutral (0)
     EXECUTABLE: Neutral (0)
     FLOPPY: Neutral (0)
     HARD: Neutral (0)
     KNOWNFILE: Neutral (0)
     NETWORK: Neutral (0)
     OPENFOLDER: Neutral (0)
     RAM: Neutral (0)
     UNKNOWNFILE: Neutral (0)
     XCLR_BMP: German — Switzerland (2055)
 [Icon (3)]
     * 1: 32x32 4b, Finnish (1035)
     2: 32x32 8b, Finnish (1035)
     + 3: 16x16 4b, Neutral (0)
     + 4: 32x32 8b, Neutral (0)
     + 5: 32x32 4b, Neutral (0)
 [String (6)]
     4080: Neutral (0)
     4081: Neutral (0)
     4082: Neutral (0)
     4083: Neutral (0)
     4084: Neutral (0)
     4085: Neutral (0)
     4086: Neutral (0)
     4087: Neutral (0)
     4088: Neutral (0)
     4089: Neutral (0)
     4090: Neutral (0)
     4091: Neutral (0)
     4092: Neutral (0)
     4093: Neutral (0)
     4094: Neutral (0)
     4095: Neutral (0)
     4096: Neutral (0)
 [RC Data (10)]
     DVCLAL: Neutral (0)
     PACKAGEINFO: Neutral (0)
     TFORM1: Neutral (0)
 [Cursor Group (12)]
     32761: Neutral (0)
     32762: Neutral (0)
     32763: Neutral (0)
     32764: Neutral (0)
     32765: Neutral (0)
     32766: Neutral (0)
     32767: Neutral (0)
 [Icon Group (14)]
     * MAINICON: Korean (1042)
     XCLR_ICON: German — Switzerland (2055)
     + 1: Neutral (0)
 [Version (16)]
     1: English (US) (1033)
---------------------------------------------------------------------
Legend: [Name (Ord)] — Type; "+" — New; "-" — Deleted; "*" — Modified


The language ID is represented by a word integer value that consists of 
a primary language and its corresponding sublanguage, which are defined 
by the Windows operating system. If a resource item is designated as 
"language neutral", the language ID value is set to zero.

This log file serves as a valuable reference, providing insights into the icon replacement and resource tree alterations made by RTC. By leveraging this output, you can ensure the desired changes have been successfully applied, streamlining your icon management tasks with ease.

To see the changes made to the test EXEs, we recommend using Resource Tuner GUI.

After installing Resource Tuner Console, you can access these examples in theDemo Scriptsfolder, located within the installation directory. The Demo Scripts folder contains 12 subdirectories, each containing sample scripts and executable files.

All sample scripts are pre-configured and ready to run. To execute a sample script, simply select one of the .BAT files within the Demo Scripts folders. The script will apply changes to a test EXE file. The modified file will be generated in a directory named Release, which will be located within the same directory containing the script.

 

Have A Question?

Please check our FAQ section for answers to the most commonly asked questions.

If you have any questions about Resource Tuner Console, please contact us. We would love to get your feedback! Your opinion is of great use to us. This helps us focus our time on features people are really interested in.

 

The Complete Illustrated Step-by-Step Guide To Using Scripts

 Download Resource Tuner Console and learn how it can make you more productive.