erocarrera / pefile

pefile is a Python module to read and work with PE (Portable Executable) files
MIT License
1.86k stars 519 forks source link

Utility to change the icon #375

Open TrueCat17 opened 1 year ago

TrueCat17 commented 1 year ago

Many cross-platform engines and applications need to set the icon for the *.exe file regardless of the platform. Judging by the fact that almost everywhere Windows or Wine is required to change an icon, there is simply no solution to this problem.

It seems to be a common and not so difficult task, but all over the Internet I found exactly 1 way to do this (and even then - the result is problematic). There is a lot of information about how to extract information (including the icon), how to change the file with third-party utilities or with their partial use, but nothing about how you can simply change the icon without win-api.

The way I found (from the renpy/launcher project) uses your library, which is why I'm referring here. change_icon.py - edited (without internal dependencies) file from renpy. python change_icon.py old.exe icon.ico new.exe

In my case, the resulting file has the desired icon, runs in Wine, but (suddenly) doesn't run in Windows.

Uncommenting the newpe.print_info() line at the very beginning produces the following warning: Invalid relocation information. VirtualAddress outside of Image: 0x688ef2f1

Regarding the same output, pe (original exe) immediately shows that the part with Base relocations (at the bottom of the output) is simply missing. I tried to fix it by adding the last couple of commented lines. After that, the part with relocations reappeared, but now the file does not run anywhere at all.

This is not the first time I spend several days trying to solve this problem, but nothing comes of it, because I am very poorly versed in all these tables, sections, headers, sizes, addresses, offsets, etc.

Perhaps there is a person here who can once and for all solve this problem for the entire Internet? Of course, it would be nice to make a separate python module for this, rather than just posting an answer here, but this is no longer relevant to the current topic. Thanks.

change_icon.zip