Select the icon file to replace the EXE icon

Source: Internet
Author: User
Tags readfile

How to use an icon file to modify the EXE file icon is an interesting problem, and no mature code is found online for free.

This is because the code for modifying the EXE icon is circulating on the network. Although the code has been copied many times, there are obvious shortcomings. The modified icon can be displayed in the EXE file, but its storage structure is faulty. For example, if you use vs2005 to open this modified EXE resource, you cannot view the modified icon information, and the vs2005 will be shut down abnormally. Now, I have fixed this problem. With this code, you can smoothly modify the icons in your EXE resources, and it is free of charge :)

 

Files used in the Code include. H files and. cpp files. What I listed here is the key part of my code, which is actually all the code for this issue.

Main content of. h file:

Typedef struct tagheader <br/>{< br/> word idreserved; <br/> word idtype; <br/> word idcount; <br/>}header, * lpheader; </P> <p> typedef struct tagicondirentry <br/> {<br/> byte bwidth; <br/> byte bheight; <br/> byte bcolorcount; <br/> byte breserved; <br/> word wplanes; <br/> word wbitcount; <br/> DWORD dwbytesinres; <br/> DWORD dwimageoffset; <br/>} icondirentry, * lpicondirentry; </P> <p> # pragma pack (push) <br/> # pragma pack (2) <br/> typedef struct taggrpicondirentry <br/> {<br/> byte bwidth; <br/> byte bheight; <br/> byte bcolorcount; <br/> byte breserved; <br/> word wplanes; <br/> word wbitcount; <br/> DWORD dwbytesinres; <br/> word NID; <br/>} grpicondirentry, * lpgrpicondirentry ;; </P> <p> typedef struct taggrpicondir <br/> {<br/> word idreserved; <br/> word idtype; <br/> word idcount; <br/> grpicondirentry identries [1]; <br/>} grpicondir, * lpgrpicondir; <br/> # pragma pack (POP)

The main code in the. cpp file:

Void changedexeicon (maid) <br/>{< br/> lpicondirentry picondirentry (null); <br/> lpgrpicondir pgrpicondir (null ); <br/> header; <br/> lpbyte piconbytes (null); <br/> handle hiconfile (null); <br/> DWORD dwret (0 ), nsize (0), ngsize (0), dwreserved (0); <br/> handle hupdate (null); <br/> bool RET (false ); <br/> word I (0); </P> <p> // open the icon file <br/> hiconfile = createfile (lpiconfile, G Eneric_read, null, null, open_existing, file_attribute_normal, null); <br/> If (hiconfile = invalid_handle_value) <br/>{< br/> return; <br/>}< br/> // Read File Header Information <br/> ret = readfile (hiconfile, & header, sizeof (header), & dwreserved, null ); <br/> If (! RET) <br/>{< br/> closehandle (hiconfile); <br/> return; <br/>}< br/> // create a directory information storage area for each icon <br/> picondirentry = (lpicondirentry) New byte [header. idcount * sizeof (icondirentry)]; <br/> If (picondirentry = NULL) <br/>{< br/> closehandle (hiconfile); <br/> return; <br/>}< br/> // read the directory information of each icon from the icon file <br/> ret = readfile (hiconfile, picondirentry, header. idcount * sizeof (icondirentry), & dwreserved, null); <br/> If (! RET) <br/>{< br/> Delete [] picondirentry; <br/> closehandle (hiconfile); <br/> return; <br/>}< br/> // create the data structure storage area required by rt_group_icon In the EXE file <br/> ngsize = sizeof (grpicondir) + header. idcount * sizeof (icondirentry); <br/> pgrpicondir = (lpgrpicondir) New byte [ngsize]; <br/> // fill in the information, this is equivalent to a conversion process <br/> pgrpicondir-> idreserved = header. idreserved; <br/> pgrpicondir-> idtype = header. idtype; <br/> pgrpicondir-> idcount = header. idcount; <Br/> // copy the information and set the ID of each icon. ID is the location index number <br/> for (I = 0; I <peader. idcount; I ++) <br/>{< br/> pgrpicondir-> identries [I]. bwidth = picondirentry [I]. bwidth; <br/> pgrpicondir-> identries [I]. bheight = picondirentry [I]. bheight; <br/> pgrpicondir-> identries [I]. bcolorcount = picondirentry [I]. bcolorcount; <br/> pgrpicondir-> identries [I]. breserved = picondirentry [I]. breserved; <br/> pgrpicondir-> identries [I]. wplanes = picondirentry [I]. wplanes; <br/> pgrpicondi R-> identries [I]. wbitcount = picondirentry [I]. wbitcount; <br/> pgrpicondir-> identries [I]. dwbytesinres = picondirentry [I]. dwbytesinres; <br/> pgrpicondir-> identries [I]. nid = I; <br/>}< br/> // start to update the icon resource in the exe. The ID is set to 0 at the minimum. If the original 0 ID icon exists, it is replaced with the new one. <Br/> hupdate = beginupdateresource (lpexename, false); <br/> If (hupdate! = NULL) <br/>{< br/> // first update the rt_group_icon Information <br/> ret = updateresource (hupdate, rt_group_icon, makeintresource (0), makelangid (lang_chinese, sublang_sys_default), (lpvoid) pgrpicondir, ngsize); <br/> If (! RET) <br/>{< br/> Delete [] picondirentry; <br/> Delete [] pgrpicondir; <br/> closehandle (hiconfile); <br/> return; <br/>}< br/> // The information of each icon is stored. <br/> for (I = 0; I <peader. idcount; I ++) <br/>{< br/> // Number of icon bytes <br/> nsize = picondirentry [I]. dwbytesinres; <br/> // pointer of the Offset file to the start of the current icon <br/> dwret = setfilepointer (hiconfile, picondirentry [I]. dwimageoffset, null, file_begin); <br/> If (dwret = invalid_set_file_pointer) <br/ >{< Br/> break; <br/>}< br/> // prepare piconbytes to store the byte information in the file for updating to the exe. <Br/> Delete [] piconbytes; <br/> piconbytes = new byte [nsize]; <br/> ret = readfile (hiconfile, (lpvoid) piconbytes, nsize, & dwreserved, null); <br/> If (! RET) <br/>{< br/> break; <br/>}< br/> // update the rt_icon information corresponding to each ID <br/> ret = updateresource (hupdate, rt_icon, makeintresource (pgrpicondir-> identries [I]. NID), makelangid (lang_chinese, sublang_sys_default), (lpvoid) piconbytes, nsize); <br/> If (! RET) <br/>{< br/> break; <br/>}< br/> // ends the update operation on the EXE resource. <br/> If (piconbytes! = NULL) <br/>{< br/> Delete [] piconbytes; <br/>}< br/> endupdateresource (hupdate, false ); <br/>}< br/> // clear the resource and close the icon file. The update operation is complete! <Br/> Delete [] pgrpicondir; <br/> Delete [] picondirentry; <br/> closehandle (hiconfile); <br/>}

Now, you can use the code to modify the EXE icon.

 

The level is limited. please correct me!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.