Link to home
Create AccountLog in
Avatar of winexec
winexec

asked on

Icon extraction from EXE

I need to extract the icons from an executable, into a one .ico file. The problem is the icons can have different color depths and sizes, so I cannot simply use a function like ExtractIcon().

I think I have to enumerate the resources, find RT_GROUP_ICON and RT_ICON types, extract the data and combine the bits into a .ico file.

I need that to work on XP/Vista.

Thank you.
Avatar of dlan75
dlan75
Flag of France image

Hi,
Have you tryed using ressource hacker?
Avatar of winexec
winexec

ASKER

I'm interested to see a Delphi solution.
Avatar of winexec

ASKER

@ciuly:
Thanks, but I think Madshi's components aren't free.
Hi

Try the following
var
  FIcon : TIcon;
  FApp : String;
begin
   FApp := 'Application.EXE';
   FIcon := TIcon.Create;
   FIcon.Handle := ExtractIcon(Handle, PChar(FApp), 0);
   ImageList1.AddIcon(FIcon);
end;
they are free for non-commercial use.
in your case, all you need is madbasic which is toitally free: http://help.madshi.net/madBasic.htm

a good alternativ: http://www.wilsonc.demon.co.uk/d10resourceeditor.htm
Avatar of winexec

ASKER

atul_parmar, really, did you read my question? "The problem is the icons can have different color depths and sizes, so I cannot simply use a function like ExtractIcon()."
Sorry I really didn't read it first. You can use ExtractIconEx; it takes care of size and color depths.

https://www.experts-exchange.com/questions/20956969/Getting-48X48-Icons.html
and this too
http://wall.riscom.net/books/delphi/del_faqs/1339.html
ASKER CERTIFIED SOLUTION
Avatar of bernani
bernani
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi winexec,

If you use the units in my previous post, you can use the following sample procedure to save a particular icon from a multi-cion image.
As you're Advanced on this subjec, I believe you don't need more explanation.


procedure TIconViewer.SaveSelectedImageIconClick(Sender: TObject);
Var
  Stream : TMemoryStream;
  ImgNr: integer;
  Header : TIconHeader;
  ResInfo : TFileIconResInfo;
  StartPos,StreamPos : DWord;
 
begin
  With ICOListBox DO begin
    IF (ItemIndex<0)
    OR (ItemIndex>Icon.Images.Count)
    OR (Icon.Images.Count<= 0{ 1}) Then exit;
    ImgNr:= IcoListbox.ItemIndex;

  With Header do begin
    wReserved:=0;
    wType    :=1;
    wCount   :=1; // only the selected icon
  end;

    Stream:=TMemoryStream.Create;
    try
    With Stream do begin
    StartPos:= Position;
    Write(Header,SizeOf(Header));
    ResInfo.ResInfo:= Icon.Images.Image[ItemIndex].Info;
    ResInfo.dwImageOffset:= ImgNr; // selected Ico
    Write(ResInfo,SizeOf(ResInfo));
    StreamPos:= Position-StartPos;
    Position:=SizeOf(Header)+(SizeOf(ResInfo))- SizeOf(DWord);
    Write(StreamPos,SizeOf(StreamPos));
    Position:=StreamPos;
    Icon.Images.Image[ItemIndex].WriteIconDataToStream(Stream);
    if Stream.Size > 0 then Stream.SaveToFile('New.ico');
    end
    finally
    Stream.Free;
    end;
  end;
end;


Hope this can be helpful for others too.

Avatar of winexec

ASKER

@ciuly:
I've installed Madsi's components and tried your sample. I don't know the reason, but SaveIconGroupResourceW fails to save the .ico file ('saved_main.ico' in your sample). The result is true, but the file isn't saved. I've also tried with a full path for the ico file, it didn't save it. I didn't find the time to debug it more.

@bernani:
I've downloaded your demo, it works fine. Thanks.


I just retested the demo using the steps outlined on the page and it works just fine. dunno what to say. it could be the language, though if you don't change anything in the project (no files, no nothing, just compile) it should maintain the romanian language as it is currently hardocded in the demo.
I cannot fix something I cannot reproduce so I'll just assume something was different in your test then in mine.
Avatar of winexec

ASKER

ciuly:
You're right. I've re-tested your app and it works fine (I've tested it even with icons with different color depths and sizes). I'm not sure why it didn't work initially, when I've changed the MainIcon, it was my fault.

But I will use the solution proposed by Bernani, I don't like to use other components (even if they are free).