Page 1 of 1

change exe icon [windows]

Posted: Sun Jul 14, 2019 10:00 am
by CELTIC88
hi all :) ,

code

Code: Select all

Structure ICONDIRENTRY   
  bWidth.a;          // Width, in pixels, of the image
  bHeight.a;         // Height, in pixels, of the image
  bColorCount.a;     // Number of colors in image (0 if >=8bpp)
  bReserved.a  ;       // Reserved ( must be 0)
  wPlanes.u    ;         // Color Planes
  wBitCount.u  ;       // Bits per pixel
  dwBytesInRes.l;    // How many bytes in this resource?
  dwImageOffset.l;   // Where in the file is this image?
EndStructure

Structure ICONDIR 
  idReserved.u;   // Reserved (must be 0)
  idType.u    ;       // Resource Type (1 for icons)
  idCount.u   ;      // How many images?
EndStructure

Structure GroupIconData
  bWidth.a;          // Width, in pixels, of the image
  bHeight.a;         // Height, in pixels, of the image
  bColorCount.a;     // Number of colors in image (0 if >=8bpp)
  bReserved.a  ;       // Reserved ( must be 0)
  wPlanes.u    ;         // Color Planes
  wBitCount.u  ;       // Bits per pixel
  dwBytesInRes.l;    // How many bytes in this resource?
  OrdinalName.u ;   // Where in the file is this image?
EndStructure

Structure IconGroupHeader
  idReserved.u;   // Reserved (must be 0)
  idType.u    ;       // Resource Type (1 for icons)
  idCount.u   ;      // How many images?
  GroupIconData.GroupIconData[0]
EndStructure

EnableExplicit


Procedure SetExeIcone(exepath.s,             ;exe full path
                      *pdata.ICONDIR,        ;icon data
                      *name,                 ;name Resource
                      lang = 0)              ;lang Resource
                                             ;return 1 = successfully.
  Protected Result
  Protected *hgi.IconGroupHeader = AllocateMemory(SizeOf(IconGroupHeader)+
                                                  (SizeOf(GroupIconData) * (*pdata\idCount+1)))
  If *hgi
    CopyStructure(*pdata, *hgi, ICONDIR)
    With *hgi
      Protected hResource = BeginUpdateResource_(exepath, 0);
      If hResource <> 0
        
        Protected ii, *icd.ICONDIRENTRY
        For ii = 0 To *pdata\idCount-1
          
          *icd = *pdata + SizeOf(ICONDIR) + (SizeOf(ICONDIRENTRY) * ii)
          CopyStructure(*icd, \GroupIconData[ii],GroupIconData)
          
          \GroupIconData[ii]\OrdinalName = ii+1
          
          Result = UpdateResource_(hResource, #RT_ICON, \GroupIconData[ii]\OrdinalName,lang,
                                   *pdata + *icd\dwImageOffset, *icd\dwBytesInRes)
          If Result = 0
            Break
          EndIf
          
        Next

        If Result <> 0
          Result = UpdateResource_(hResource, #RT_GROUP_ICON, *name, lang,
                                   *hgi, SizeOf(IconGroupHeader)+(SizeOf(GroupIconData) * (*pdata\idCount)))
        EndIf
        
        EndUpdateResource_(hResource, 0)
      EndWith
    EndIf
    
    FreeMemory(*hgi)
  EndIf
  ProcedureReturn Result
EndProcedure


Define filename.s="test.exe"
Define iconpath.s="2.ico"

Define size =FileSize(iconpath)
If size>0
  Define *mem=AllocateMemory(size)
  ReadFile(0,iconpath)
  ReadData(0,*mem,size)
  CloseFile(0)
EndIf

Debug SetExeIcone(filename,*mem,1,2057)

End 

Re: change exe icon [windows]

Posted: Sun Jul 14, 2019 10:13 am
by RSBasic
ImageImageImage

Re: change exe icon [windows]

Posted: Sun Jul 14, 2019 1:25 pm
by Mijikai
Optional:

Code: Select all

SendMessage_(WindowID(Window),#WM_SETICON,#Null,ImageID(Ico))

Re: change exe icon [windows]

Posted: Sun Jul 14, 2019 3:21 pm
by Kwai chang caine
Hello Celtic88 :D
I have test your nice code, but that not works on W10 X 64 / V5.70 X86 :|
http://erdsjb.free.fr/purestorage/provi ... emples.ico
http://erdsjb.free.fr/purestorage/provi ... iltre7.exe

Re: change exe icon [windows]

Posted: Sun Jul 14, 2019 6:07 pm
by Denis
Salut CELTIC88 & KCC

KCC, try with SetExeIcone(filename,*mem,1, 0) or SetExeIcone(filename,*mem,1, 1033)

last param 0 : #LANG_NEUTRAL, #SUBLANG_NEUTRAL
last param 1033 : #LANG_ENGLISH, #SUBLANG_ENGLISH_US

last param 2057 : #LANG_ENGLISH, #SUBLANG_ENGLISH_UK, but no icon have this language.
language is discriminating.

with the new last param, the group '1' (numerical group ID 1) will be overwrinting with new icon.
So original icon of this group is lost.
The code has to be changed to append icon to an existing group or to had a new group regarding OS restrictions (see API UpdateResourceW etc.)

Modified :
It add the icon too into the group "MAINICON" (alphanumerical group ID 'MAINICON'), see screenshot below using PureIconManager
Icon 16x16 into this group is lost too.

Image

Re: change exe icon [windows]

Posted: Thu Jul 18, 2019 1:37 pm
by Kwai chang caine
Thanks DENIS for your explanation 8)
Always happy to read you :wink: