English English  Deutsch Deutsch

Edit Icons: Replacing an Icon Resource With an Icon From an .ICO File

This code shows the way you can change icons in EXE and DLL files using Resource Tuner Console.

The script adds or replaces icons in the test EXE file with icons from .ico files in the "\Demo\Src" folder. After that, it outputs the resulting resource tree to a log file and/or the console screen. The modified file is saved to the Destination folder.

This code shows how to modify icons in executables:
— Replace the main icon with the new icons from an .ICO file (including the large 256x256 PNG-compressed Vista icon);
— Sort out the added icons in the right order;
— Add one more icon to the newly created Icon Group;
— Output the changes in the Resource Tree to a log file;
— Restore the original timestamps on the file after saving.

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

The resulting files will be created in the directory named "Release"
Check the log file rtc.log to see the Resource Tree changed.

For greater details, see the Step-by-Step Guide To Using Scripts

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."
    PEFileProxy.PostDebugString "File opened OK."
    if (not PEFileProxy.HasResources) then
      PEFileProxy.PostDebugString "The file contains no resources."
      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:"
      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."
       PEFileProxy.PostDebugString "CreationTime, LastAccessTime and 
LastWriteTime for file '" & DESTFILE & "' restored OK."
      end if
      PEFileProxy.PostDebugString ""


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

Once you run the script, RTC outputs the Resource Tree to a log file, so you can see the changes made:

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

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

Once installed Resorce Tuner Console, you will find the Demo folder nested in the directory where RTC has been installed. Within this Demo folder, there are 12 subdirectories that contain sample scripts and sample executable files.

All sample scripts are ready to run. Select one of the .BAT files located in the Demo folders to execute the sample script. The script will make changes in the test EXE file. The resulting file will be created in the directory named "Release" under the directory containing the script.

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