1

I want to change a formerly generated EXE file's main icon using Delphi. The EXE file was also generated by me using Delphi. But I want the user to be able to change its icon.

I tried using UpdateResource function to change RT_GROUP_ICON and RT_ICON without success:

procedure UpdateExeIcon(Const IconFilename, ExternalExeFilename:string);
var
  Stream     : TFileStream;
  hDestRes   : THANDLE;
  lpData     : Pointer;
  cbData     : DWORD;
begin
  Stream := TFileStream.Create(IconFilename,fmOpenRead or fmShareDenyNone);
  try
    Stream.Seek(0, soFromBeginning);
    cbData:=Stream.Size;
    if cbData>0 then
    begin
      GetMem(lpData,cbData);
      try
        Stream.Read(lpData^, cbData);
        hDestRes:= BeginUpdateResource(PChar(ExternalExeFilename), False);
        if hDestRes <> 0 then
        begin
          //if UpdateResource(hDestRes, RT_ICON,PChar('1'),1033,lpData,cbData) then
          if UpdateResource(hDestRes, RT_GROUP_ICON,PChar('MAINICON'),1033,lpData,cbData) then
          begin
            if not EndUpdateResource(hDestRes,FALSE) then RaiseLastOSError;
          end else RaiseLastOSError;
        end else RaiseLastOSError;
      finally
        FreeMem(lpData);
      end;
    end;
  finally
    Stream.Free;
  end;
end;
5
  • When/where do you conclude it was "without success"? Which other steps are successful?
    – AmigoJack
    Aug 1, 2020 at 12:18
  • @AmigoJack The procedure certainly injects something, but it doesn't change the main icon.
    – Xel Naga
    Aug 1, 2020 at 12:49
  • 1
    The main icon from Windows perspective is the first one. Inspect the executable using a resource editor to see what has happened. As always, the key to solving a problem is to debug it. Aug 1, 2020 at 13:16
  • Does refreshing icon cache help? superuser.com/questions/499078/…
    – W. Chang
    Aug 1, 2020 at 14:59
  • @W.Chang No, it doesn't work. Even tried changing filenames.
    – Xel Naga
    Aug 1, 2020 at 15:16

1 Answer 1

5

There are multiple mistakes:

  1. In UpdateResource(hDestRes, RT_ICON,PChar('1'),1033,lpData,cbData) the lpName parameter works in two ways: PChar('1') will turn out as a text, while MakeIntResource(1) will turn out as a number. You want the latter variant, not the former.
  2. For the resource type RT_ICON you have to provide the actual Icon payload, not an entire Icon file. Right now you do the latter, not the former. Look at how an Icon file looks like, then look at what the resource only has.
  3. The resource type RT_GROUP_ICON is "only" an index describing existing RT_ICON resources and should be patched as per your updated Icon (unless width, height, colors, pixel depth and payload length are all the same). Filling in file contents here makes never sense.

Resource Hacker: icon payload

Above you see Resource Hacker displaying the raw bytes of the resource we want to update. 128 is the length of bytes in hexadecimal.

HxD icon file container

Above you see HxD displaying a whole Icon file - I selected the part where the first icon payload resides: starting at offset 26 with a length of 128 (both hexadecimal). Both byte sequences match.

If you do all that (using MakeIntResource(1) on RT_ICON and provide only the picture data of the Icon file) then you're fine - it worked for me fine: my EXE then displayed the new icon in Windows' Explorer (haven't executed it, tho). Updating the RT_GROUP_ICON should also be done, since I'm not sure up to where it seems to have no effect and when it suddenly becomes a problem. Viewing your files in a hex editor and your EXE in a resource editor will make you understand much better all the actions. Further reading:

6
  • Thank you for your great help. Could you please provide a code sample for #2? I have no idea how I can provide a payload instead of the icon file.
    – Xel Naga
    Aug 2, 2020 at 8:00
  • I can't since I don't know which of the Icons in a file you want to read. Also "how to parse Icon files" should be a new question, not a comment.
    – AmigoJack
    Aug 2, 2020 at 11:12
  • Let's say I only work with 32x32 pixels 32 bit depth icon files (no multi-icon). A sample payload code would be greatly appreciated.
    – Xel Naga
    Aug 2, 2020 at 15:58
  • Ask a new question, stating what you achieved so far and where you fail - there are only advantages instead of treating comments like a chat.
    – AmigoJack
    Aug 2, 2020 at 18:52
  • 6 upvotes, yet @Andrzej won't accept it - was this question a combination of borrowed code and delegated homework after all?
    – AmigoJack
    Aug 12, 2020 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.