Here's part of my program where I extract an icon from an .exe or .dll file. I have no problem showing the icon. The problem is when I try to save it. The WriteFile is wrong somewhere or I need some other code. Anayone know how to save the icon to an .ico file?

GetIconFromInstance PROC hInst1:HINSTANCE
LOCAL hRsrc :DWORD
LOCAL hGlobal :HGLOBAL
LOCAL lpRes :DWORD
LOCAL nID :DWORD
LOCAL nIndex :DWORD
LOCAL hFileZ :DWORD
LOCAL cbData :DWORD
LOCAL CntX :DWORD

szText szzBuff,"MyIcon.ico",0


mov eax,NumOut ;this calculate the icon ID
shl eax,2 ;I want to extract
mov eax, ; " "
mov nIndex,eax ; " "

invoke FindResource,hInst1, nIndex, RT_GROUP_ICON
mov hRsrc,eax
invoke LoadResource, hInst1, hRsrc
mov hGlobal,eax
invoke LockResource,hGlobal
mov lpRes,eax

invoke LookupIconIdFromDirectory, lpRes, TRUE ;,32,32,LR_LOADREALSIZE
mov nID,eax

invoke FindResource, hInst1, nID, RT_ICON
mov hRsrc,eax
invoke LoadResource, hInst1, hRsrc
mov hGlobal,eax
invoke SizeofResource,hInst1,hRsrc
mov cbData,eax
invoke LockResource,hGlobal
mov lpRes,eax

.if lpRes != 0

invoke CreateIconFromResourceEx,lpRes, 1024, TRUE, 30000h,32,32, LR_DEFAULTCOLOR
mov hIco,eax
invoke StaticIcon,NULL,hWnd,473,57,32, 32,65534
mov hStatIcon, eax
invoke SendMessage,hStatIcon,STM_SETIMAGE,IMAGE_ICON,hIco
.else
invoke StaticIcon,NULL,hWnd,473,57,32, 32,65534
mov hStatIcon, eax
invoke SendMessage,hStatIcon,STM_SETIMAGE,IMAGE_ICON,hIcon
.endif

INVOKE CreateFile, addr szzBuff,\
GENERIC_READ or GENERIC_WRITE,\
FILE_SHARE_READ or FILE_SHARE_WRITE,\
0, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0
mov hFileZ, eax

INVOKE WriteFile, hFileZ,lpRes ,cbData, addr CntX, NULL
INVOKE CloseHandle, hFile

ret
GetIconFromInstance endp


Thanks
Guy
Posted on 2001-07-22 17:17:02 by Guy
I cant say I've been down this road, but im getting the feeling you need to write a funtion that will write to file the needed format of an Icon...

Check out this site (its quite handy) : http://www.wotsit.org/

Or more specifically on this page has The ICO format...

Hope this helps..

:alright:
NaN
Posted on 2001-07-22 17:48:12 by NaN
the resource data from the exe contains only the ICONIMAGE
data.

you should append before an ICONDIR and an ICONDIRENTRY
structure, like in the description posted by Nan.

if you name your saved file .bmp, I think you will be able to open it as BITMAP file.
Posted on 2001-07-23 04:15:03 by beaster
Here you'll find the info that you need:

http://www.anticracking.sk/EliCZ/import/msdn_icons.htm

I attach a sample source code. It extracts the main icon from a
PE file and dumps it to the hard disk.
Posted on 2001-07-24 12:06:50 by n u M I T_o r