Page 1 of 1

[madRes] change icon in exe

Posted: Thu Oct 11, 2007 11:56 am
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?

Posted: Fri Oct 12, 2007 1:09 pm
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.

Posted: Fri Oct 12, 2007 7:15 pm
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;

Posted: Sat Oct 13, 2007 7:50 am
by madshi
Good to see you found it yourself... :)