[madRes] change icon in exe

delphi package - madRes, madTools, madStrings, ...
Post Reply
ciuly
Posts: 65
Joined: Mon Apr 30, 2007 1:16 pm
Location: Romania

[madRes] change icon in exe

Post by ciuly »

Hello,

I've been trying to help somebody on EE to use madRes to change the icon from an exe. the following code works fine if the icon group is something text, but if it's a number, then the icon group is duplicated.

bogus, input, app

Code: Select all

program Project1;

{$R *.res}
{$R icons.res}

begin
end.
icons.rc

Code: Select all

LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
128 ICON "green.ico" 
TEST ICON "green.ico"
actual test program

Code: Select all

program Project2;

uses
  windows, madres;

{$R *.res}

function MAKELANGID(sPrimaryLanguage : Word; 
                    sSubLanguage : Word) : Word; 
begin 
  result := (sSubLanguage shl 10) or 
             sPrimaryLanguage; 
end;

function UpdateExeIcon(exeFile, iconGroup, icoFile: string; language: word) : boolean;
var resUpdateHandle : dword;
begin
  resUpdateHandle := BeginUpdateResourceW(PWideChar(wideString(exeFile)), false);
  if resUpdateHandle <> 0 then begin
    result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, PWideChar(wideString(icoFile)));
    result := EndUpdateResourceW(resUpdateHandle, false) and result;
  end else
    result := false;
end;

begin
  UpdateExeIcon('test.exe', '128', 'red.ico', makelangid(LANG_ENGLISH,SUBLANG_ENGLISH_US));
end.
if you use
UpdateExeIcon('test.exe', 'TEST', 'red.ico', makelangid(LANG_ENGLISH,SUBLANG_ENGLISH_US));
then the icon changes from green to red: no problems. but if you use it with '128', then the problem is reproducing.

using // madRes.pas version: 1.0j · date: 2005-07-15

any ideas?
ciuly
Posts: 65
Joined: Mon Apr 30, 2007 1:16 pm
Location: Romania

Post by ciuly »

after debugging madres, I found the solution

Code: Select all

program Project2;

uses
  windows,
  madres,
  sysutils;

{$R *.res}

function MAKELANGID(sPrimaryLanguage : Word;
                    sSubLanguage : Word) : Word; 
begin 
  result := (sSubLanguage shl 10) or 
             sPrimaryLanguage; 
end;

function UpdateExeIcon(exeFile, iconGroup, icoFile: string; language: word) : boolean;
var resUpdateHandle : dword;
    c:TPIconGroup;
begin
  resUpdateHandle := BeginUpdateResourceW(PWideChar(wideString(exeFile)), false);
  if resUpdateHandle <> 0 then
  begin
    if GetIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, c) then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, PWideChar(wideString(icoFile)))
                                                                                             else
    if StrToIntDef(iconGroup, -1)>-1 then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(pointer(strtoint(iconGroup))), language, PWideChar(wideString(icoFile)))
    else
      result := false;
    result := EndUpdateResourceW(resUpdateHandle, false) and result;
  end else
    result := false;
end;

begin
  UpdateExeIcon('test.exe', '128', 'red.ico', makelangid(LANG_ENGLISH,SUBLANG_ENGLISH_US));
end.
ciuly
Posts: 65
Joined: Mon Apr 30, 2007 1:16 pm
Location: Romania

Post by ciuly »

and an update to support mainicon changin as well. provide proper language, of course.

Code: Select all

function UpdateExeIcon(exeFile, iconGroup, icoFile: string; language: word) : boolean;
var resUpdateHandle : dword;
    c:TPIconGroup;
begin
  resUpdateHandle := BeginUpdateResourceW(PWideChar(wideString(exeFile)), false);
  if resUpdateHandle <> 0 then
  begin
    if (icongroup='MAINICON') or GetIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, c) then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(wideString(iconGroup)), language, PWideChar(wideString(icoFile)))
                                                                                             else
    if StrToIntDef(iconGroup, -1)>-1 then
      result := LoadIconGroupResourceW(resUpdateHandle, PWideChar(pointer(strtoint(iconGroup))), language, PWideChar(wideString(icoFile)))
    else
      result := false;
    result := EndUpdateResourceW(resUpdateHandle, false) and result;
  end else
    result := false;
end;
madshi
Site Admin
Posts: 10338
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Good to see you found it yourself... :)
Post Reply