I need to extract all icons from an EXE and save them as disk files, but I cannot use the simplest solution (ExtractIconEx) because I need to extract the ICO files in a way
Here is a working example on Delphi Praxis. It reads the default RT_GROUP_ICON and associated RT_ICON resources for a specified executable and saves them as as a complete multi-image .ICO file.
It seems to be confused by 256 pixel images (saves with invalid format), at least on XP, so it might need a little tweaking.
The PrivateExtractIcons function can help you. You can specify the size of the icon which you want to extract:
{$IFDEF UNICODE}
function PrivateExtractIcons(lpszFile: PChar; nIconIndex, cxIcon, cyIcon: integer; phicon: PHANDLE; piconid: PDWORD; nicon, flags: DWORD): DWORD; stdcall ; external 'user32.dll' name 'PrivateExtractIconsW';
{$ELSE}
function PrivateExtractIcons(lpszFile: PChar; nIconIndex, cxIcon, cyIcon: integer; phicon: PHANDLE; piconid: PDWORD; nicon, flags: DWORD): DWORD; stdcall ; external 'user32.dll' name 'PrivateExtractIconsA';
{$ENDIF}
procedure TMainForm.Button1Click(Sender: TObject);
var
hIcon : THandle;
nIconId : DWORD;
Icon : TIcon;
begin
//Extract a 128x128 icon
if PrivateExtractIcons ('C:\Users\Public\Documents\RAD Studio\Projects\2010\Aero Colorizer\AeroColorizer.exe', 0, 128, 128, @hIcon, @nIconId, 1, LR_LOADFROMFILE) <>0 then
try
Icon:=TIcon.Create;
try
Icon.Handle:=hIcon;
Icon.SaveToFile(ExtractFilePath(ParamStr(0))+'Aicon.ico');
finally
Icon.Free;
end;
finally
DestroyIcon (hIcon);
end;
end ;