change exe icon [windows]

Share your advanced PureBasic knowledge/code with the community.
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

change exe icon [windows]

Post 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 
interested in Cybersecurity..
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: change exe icon [windows]

Post by RSBasic »

ImageImageImage
Image
Image
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: change exe icon [windows]

Post by Mijikai »

Optional:

Code: Select all

SendMessage_(WindowID(Window),#WM_SETICON,#Null,ImageID(Ico))
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: change exe icon [windows]

Post 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
ImageThe happiness is a road...
Not a destination
Denis
Enthusiast
Enthusiast
Posts: 704
Joined: Fri Apr 25, 2003 5:10 pm
Location: Doubs - France

Re: change exe icon [windows]

Post 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
A+
Denis
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: change exe icon [windows]

Post by Kwai chang caine »

Thanks DENIS for your explanation 8)
Always happy to read you :wink:
ImageThe happiness is a road...
Not a destination
Post Reply