title: How to add an icon to an existing exe file (without an icon by default)?
body: <p>When I was searching on Google I found a useful class which let us change the icon of any <code>.exe</code> file using the following line of code : </p>
  
  <pre><code>WindowsFormsApplication1.IconInjector.InjectIcon("myfile.exe", "myicon.ico", 200, 1);
  </code></pre>
  
  <p>Where 200 and 1 are respectively icon <code>GroupID</code> and icon <code>BaseID</code> which I can determine using Resource Hacker. In this case the file's icon changes successfully without corrupting the file.</p>
  
  <p>So i planned to use this class on my program which is a SFX / Software protector, the output file always hasn't an icon, all what I can see on Resource hacker is the below :</p>
  
  <p><img src="https://i.stack.imgur.com/GfPFa.png" alt="Resource Hacker view of PuTTY_Protected.exe prechange"></p>
  
  <p>i can't see  icon group id nor the base id, anyway, (I don't know what to put instead of 200 and 1 in this case)  So I tried to change the icon using the same line of code mentioned above, I used the following line of code (same as above):</p>
  
  <pre><code>WindowsFormsApplication1.IconInjector.InjectIcon("myfile.exe", "myicon.ico", 200, 1);
  </code></pre>
  
  <p>The file icon was successfully changed but the file doesn't work anymore! </p>
  
  <p>When I tried to reopen the file using ResourceHacker, I found the below:</p>
  
  <p><img src="https://i.stack.imgur.com/pQwZA.png" alt="Resource Hacker view of PuTTY_Protected.exe after change"></p>
  
  <p>It seems that the icon resources were successfully added, but i can't figure out why the file doesn't work anymore, it seems that is corrupted.</p>
  
  <p><img src="https://i.stack.imgur.com/aNbOn.png" alt="PuTTY_protected.exe has stopped working"></p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Note : I tried using this class with unprotected file and it works like a charm! </p>
  
  <p>The class I am using is the below: </p>
  
  <pre><code>// IconInjector.cs
  using System;
  using System.Runtime.InteropServices;
  using System.Diagnostics;
  
  namespace WindowsFormsApplication1
  {
  
  /// &lt;summary&gt;
  /// IconInjectorクラスの定義
  /// &lt;/summary&gt;
  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);
  
      /// &lt;summary&gt;
      /// アプリケーションのメイン エントリ ポイントです。
      /// &lt;/summary&gt;
      [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 &lt; iconFile.GetImageCount(); i++)
          {
              byte[] image = iconFile.GetImageData(i);
              UpdateResource(hUpdate, RT_ICON, (uint)(iconBaseID + i), 0, image, (uint)image.Length);
          }
  
          // リソースの更新終了
          EndUpdateResource(hUpdate, false);
      }
  }
  }
  </code></pre>
  
  <p>Any help or suggestion on adding the icon to the protected file without corrupting it?</p>