How to add an icon to an existing exe file (without a default icon)?

When I searched on Google, I found a useful class that allowed us to change the icon of any .exe using the following line of code:

 WindowsFormsApplication1.IconInjector.InjectIcon("myfile.exe", "myicon.ico", 200, 1); 

If 200 and 1 correspond to the GroupID icon and the BaseID icon, which I can determine with the Resource Hacker. In this case, the file icon changes successfully without damaging the file.

So, I planned to use this class in my program, which is the defender of SFX / Software, the output file always does not have an icon, everything that I can see in the resource hacker is below:

Resource Hacker view of PuTTY_Protected.exe prechange

I can not see the identifier of the group of identifiers and the identifier of the base, in any case (I do not know what to put instead of 200 and 1 in this case). Therefore, I tried to change the icon using the same line of code as above, I used the following line of code (same as above):

 WindowsFormsApplication1.IconInjector.InjectIcon("myfile.exe", "myicon.ico", 200, 1); 

The file icon was successfully changed, but the file no longer works!

When I tried to open the file using ResourceHacker, I found the following:

Resource Hacker view of PuTTY_Protected.exe after change

It seems that icon resources have been successfully added, but I can’t understand why the file no longer works, it seems to be damaged.

PuTTY_protected.exe has stopped working

Any help would be appreciated.

Note. I tried to use this class with an unprotected file, and it works like a charm!

The class I'm using is below:

 // IconInjector.cs using System; using System.Runtime.InteropServices; using System.Diagnostics; namespace WindowsFormsApplication1 { /// <summary> /// IconInjectorクラスの定義/// </summary> public class IconInjector { [DllImport("kernel32.dll", SetLastError = true)] //static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage, IntPtr lpData, uint cbData); static extern int UpdateResource(IntPtr hUpdate, uint lpType, uint lpName, ushort wLanguage, byte[] lpData, uint cbData); [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr BeginUpdateResource(string pFileName, [MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources); [DllImport("kernel32.dll", SetLastError = true)] static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard); /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] public static void InjectIcon(string execFileName, string iconFileName, uint iconGroupID, uint iconBaseID) { const uint RT_ICON = 3; const uint RT_GROUP_ICON = 14; // アイコンファイルの読み込みIconFile iconFile = new IconFile(); iconFile.Load(iconFileName); // リソースの更新開始IntPtr hUpdate = BeginUpdateResource(execFileName, false); Debug.Assert(hUpdate != IntPtr.Zero); // RT_GROUP_ICON 書き込みbyte[] data = iconFile.CreateIconGroupData(iconBaseID); UpdateResource(hUpdate, RT_GROUP_ICON, iconGroupID, 0, data, (uint)data.Length); // RT_ICON書き込みfor (int i = 0; i < iconFile.GetImageCount(); i++) { byte[] image = iconFile.GetImageData(i); UpdateResource(hUpdate, RT_ICON, (uint)(iconBaseID + i), 0, image, (uint)image.Length); } // リソースの更新終了EndUpdateResource(hUpdate, false); } } } 

Any help or suggestion on adding an icon to a protected file without corrupting it?

+4
source share
3 answers

It appears that the security application verifies that the contents of the file are not tampered with. Injecting an icon is definitely a form of fake, and if protection software is not updated to ignore it, it will always fail. Alternatively, if you have protection software, you can update it so as not to break the icons.

+1
source

I just experienced the same problem with 7zip Self-Extractor exe.

Updating the 7zS.sfx icon (instead of exe) before creating ex-ex-excractor does the trick, but exe is not corrupted.

0
source

The icon of your application can be added to this executable using a tool such as Resource Hacker. and visit http://georezo.net/jparis/MI_Enviro/Icons/adding_w_RH.htm

-1
source

Source: https://habr.com/ru/post/1402725/


All Articles