Page 1 of 1

problem with APIs (BeginUpdateResource, UpdateRe.......etc)

Posted: Thu Jun 29, 2006 9:15 am
by beginner
hi

i wrote this little code to change the icon of my program, but it didn't work, i don't know where is the problem, is it in this APIs or in my program, or in my code..


this is the code:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
var
  hFile, hIcon Thandle;
  p: Pointer;
  dwReadBytes: DWORD;

begin
  hFile:= BeginUpdateResource(Pchar('MyFile.exe'), TRUE);
  hIcon:= CreateFile(Pchar('AnyIcon.ico'), GENERIC_READ, FILE_SHARE_READ, NIL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

  If hIcon <> INVALID_HANDLE_VALUE then
  GetMem(p, GetFileSize(hIcon, nil));
  ReadFile(hIcon, p^, GetFileSize(hIcon, nil), dwReadBytes, nil);

  UpdateResource(hFile, RT_GROUP_ICON, Pchar('ICON'), 0, p, GetFileSize(hFile, nil));
  UpdateResource(hFile, RT_ICON, Pchar('1'), 0, p, GetFileSize(hFile, nil));

  EndUpdateResource(hFile, false);
  CloseHandle(hFile);
  CloseHandle(hIcon);
end;

btw, i can chage the icon of my program in (Resource Hacker)...
and after this code the file size is increased by the size of the icon



could you please correct this code, or give me the simplest code to change the icon ... " i use windows XP "

Thank you alot in advance....

Posted: Thu Jun 29, 2006 9:29 am
by madshi
First of all, you're using the win32 API. The madRes API is only available as "BeginUpdateResourceW". I'd suggest that you put "madRes." in front of all the APIs, so that you can be sure that you're really using the madRes APIs and not the OS APIs.

Next, use LoadIconGroupResourceW instead of CreateFile+UpdateResource.

Posted: Thu Jun 29, 2006 9:19 pm
by beginner
thank you madshi for your reply..
First of all, you're using the win32 API.
yes, i know, and i ask about win32 API :)

i tested your "madRes", and it works just perfect ;)

but i want to know how to change the icon without using your (greate) unit ;)

i want to know how to use these APIs (of resources)

i hope u understand my question.

i asked here, because i know that you (and maybe other members here) have a good experience with this APIs .

and sorry if i posted this question in a wrong place.

btw, i read all the help about these APIs, and then tried to implement it in this code, but as you see the code didn't work, maybe theres a mistake or more, maybe theres lost steps....

any help, and any hint is very appreciated..

thank you in advance.

Posted: Fri Jun 30, 2006 6:33 am
by madshi
The source code of madRes is freely available. So you can check yourself how LoadIconGroupResourceW works inside.