6

I'm trying to change the icon of external executable programmatically. I've googled and found much information about this problem using C++. Basically, I need to use BeginUpdateResource, UpdateResource and EndUpdateResource. The problem is - I don't know what to pass to UpdateResource in C#.

Here's the code I have so far:

class IconChanger
{
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr BeginUpdateResource(string pFileName,
        [MarshalAs(UnmanagedType.Bool)]bool bDeleteExistingResources);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool UpdateResource(IntPtr hUpdate, string lpType, string lpName, ushort wLanguage,
        IntPtr lpData, uint cbData);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool EndUpdateResource(IntPtr hUpdate, bool fDiscard);

    public enum ICResult
    {
        Success,
        FailBegin,
        FailUpdate,
        FailEnd
    }

    public ICResult ChangeIcon(string exeFilePath, byte[] iconData)
    {
        // Load executable
        IntPtr handleExe = BeginUpdateResource(exeFilePath, false);

        if (handleExe == null)
            return ICResult.FailBegin;

        // Get language identifier
        CultureInfo currentCulture = CultureInfo.CurrentCulture;
        int pid = ((ushort)currentCulture.LCID) & 0x3ff;
        int sid = ((ushort)currentCulture.LCID) >> 10;
        ushort languageID = (ushort)((((ushort)pid) << 10) | ((ushort)sid));

        // Get pointer to data
        GCHandle iconHandle = GCHandle.Alloc(iconData, GCHandleType.Pinned);

        // Replace the icon
        if (UpdateResource(handleExe, "#3", "#1", languageID, iconHandle.AddrOfPinnedObject(), (uint)iconData.Length))
        {
            if (EndUpdateResource(handleExe, false))
                return ICResult.Success;
            else
                return ICResult.FailEnd;
        }
        else
            return ICResult.FailUpdate;
    }
}

Regarding lpType - in C++, you pass RT_ICON (or RT_GROUP_ICON). What value should I pass in C#? The same question goes for lpName parameter. I'm not sure about language identifier (I found this on Internet) since I cannot test it. I'm also not sure whether I'm providing appropriate icon data. Currently, iconData contains the bytes from .ico file.

Can anybody point me to right direction?

Thank you very much.

Nikola Malešević
  • 1,738
  • 6
  • 21
  • 44

4 Answers4

7

Just some pointers, this is quite hard to get right. Pass an RT_ICON by lying about the lpType argument. Change it from string to IntPtr and pass (IntPtr)3.

The lpData argument is quite tricky. You need to pass the data the way it is compiled by the resource compiler (rc.exe). I have no idea if it mangles the raw data of the .ico file. The only reasonable thing to try is to read the data from the .ico file with FileStream into a byte[], you already seem to be doing this. I think the function was really designed to copy a resource from one binary image to another. Odds of your approach working are not zero.

You are also ignoring another potential problem, the resource ID of the icon of the program isn't necessarily 1. It often is not, 100 tends to be a popular choice, but anything goes. EnumResourceNames would be required to make it reliable. The rule is that the lowest numbered ID sets the icon for the file. I'm not actually sure if that really means that the resource compiler puts the lowest number first, something that the API probably doesn't do.

A very small failure mode is that UpdateResource can only updated numbered resource items, not named ones. Using names instead of numbers is not uncommon but the vast majority of images use numbers for icons.

And of course, the odds that this will work without a UAC manifest are zero. You are hacking files that you don't normally have write access to.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you for your thorough answer, Hans. I will be working with a single executable and thus will know what identifier the icon holds. I opened the executable in VS, and it displays Icon node, inside which is the existing icon. It has 32512 identifier. I tried to "lie" about the parameter, but it doesn't work, nothing gets updated. If I use "Icon" or "RT_ICON" as a parameter, the icon is added as binary data, but not in Icon node. Instead, a new node is added and binary data is inside it. – Nikola Malešević Nov 08 '10 at 22:34
  • I managed to get it right! If it wasn't for your pointers and the lying workaround, I wouldn't be able to do it. I'll update my question in a bit. Thank you very much once again, Hans! – Nikola Malešević Nov 08 '10 at 22:49
  • Well, that worked out better than I thought. Congrats. Your snippet will be the reference to make this work for others. – Hans Passant Nov 08 '10 at 22:52
  • Hey Witchunter, how about that snippet, please? – Spooky Jun 01 '12 at 05:34
  • @NikolaMalešević I second Spooky's comment. A working snippet for changing the application icon (not the form Icon) would be wonderful. – Rainbolt Feb 17 '14 at 21:23
  • So sorry for taking me this long to write the solution down, but I had to wait because of a special NDA. Now that I'm able to share the code, I'm writing it down here. @Spooky, you've probably come up with an even better solution by now. – Nikola Malešević Mar 23 '14 at 21:06
  • @Rusher, hopefully my answer will be of some help to you. – Nikola Malešević Mar 23 '14 at 21:06
  • Is it not possible to pass `lpType` a string to update an existing resource like Bitmap? I am trying to update a Bitmap. I have tried passing `#2`, `BITMAP`, `RT_BITMAP`, it just creates a new resource type, doesn't update the existing Bitmap resource. – Elmo May 16 '14 at 21:05
4

I managed to get this working in pure C# using ResourceHacker and this posting as an example. Just use a regular .ico as input. In ResourceHacker (http://www.angusj.com/resourcehacker/) you will see the icon identifier (in my case 1) and the language identifier (in my case 1043):

enter image description here

I used this code:

internal class IconChanger
{

    #region IconReader
    public class Icons : List<Icon>
    {
        public byte[] ToGroupData(int startindex = 1)
        {
            using (var ms = new MemoryStream())
            using (var writer = new BinaryWriter(ms))
            {
                var i = 0;

                writer.Write((ushort)0);  //reserved, must be 0
                writer.Write((ushort)1);  // type is 1 for icons
                writer.Write((ushort)this.Count);  // number of icons in structure(1)

                foreach (var icon in this)
                {

                    writer.Write(icon.Width);
                    writer.Write(icon.Height);
                    writer.Write(icon.Colors);

                    writer.Write((byte)0); // reserved, must be 0
                    writer.Write(icon.ColorPlanes);

                    writer.Write(icon.BitsPerPixel);

                    writer.Write(icon.Size);

                    writer.Write((ushort)(startindex + i));

                    i++;

                }
                ms.Position = 0;

                return ms.ToArray();
            }
        }
    }

    public class Icon
    {

        public byte Width { get; set; }
        public byte Height { get; set; }
        public byte Colors { get; set; }

        public uint Size { get; set; }

        public uint Offset { get; set; }

        public ushort ColorPlanes { get; set; }

        public ushort BitsPerPixel { get; set; }

        public byte[] Data { get; set; }

    }

    public class IconReader
    {

        public Icons Icons = new Icons();

        public IconReader(Stream input)
        {
            using (BinaryReader reader = new BinaryReader(input))
            {
                reader.ReadUInt16(); // ignore. Should be 0
                var type = reader.ReadUInt16();
                if (type != 1)
                {
                    throw new Exception("Invalid type. The stream is not an icon file");
                }
                var num_of_images = reader.ReadUInt16();

                for (var i = 0; i < num_of_images; i++)
                {
                    var width = reader.ReadByte();
                    var height = reader.ReadByte();
                    var colors = reader.ReadByte();
                    reader.ReadByte(); // ignore. Should be 0

                    var color_planes = reader.ReadUInt16(); // should be 0 or 1

                    var bits_per_pixel = reader.ReadUInt16();

                    var size = reader.ReadUInt32();

                    var offset = reader.ReadUInt32();

                    this.Icons.Add(new Icon()
                    {
                        Colors = colors,
                        Height = height,
                        Width = width,
                        Offset = offset,
                        Size = size,
                        ColorPlanes = color_planes,
                        BitsPerPixel = bits_per_pixel
                    });
                }

                // now get the Data
                foreach (var icon in Icons)
                {
                    if (reader.BaseStream.Position < icon.Offset)
                    {
                        var dummy_bytes_to_read = (int)(icon.Offset - reader.BaseStream.Position);
                        reader.ReadBytes(dummy_bytes_to_read);
                    }

                    var data = reader.ReadBytes((int)icon.Size);

                    icon.Data = data;
                }

            }
        }

    }
    #endregion

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern int UpdateResource(IntPtr hUpdate, uint lpType, ushort 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);

    public enum ICResult
    {
        Success,
        FailBegin,
        FailUpdate,
        FailEnd
    }

    const uint RT_ICON = 3;
    const uint RT_GROUP_ICON = 14;

    public ICResult ChangeIcon(string exeFilePath, string iconFilePath)
    {
        using (FileStream fs = new FileStream(iconFilePath, FileMode.Open, FileAccess.Read))
        {
            var reader = new IconReader(fs);

            var iconChanger = new IconChanger();
            return iconChanger.ChangeIcon(exeFilePath, reader.Icons);
        }
    }

    public ICResult ChangeIcon(string exeFilePath, Icons icons)
    {
        // Load executable
        IntPtr handleExe = BeginUpdateResource(exeFilePath, false);

        if (handleExe == null) return ICResult.FailBegin;

        ushort startindex = 1;
        ushort index = startindex;
        ICResult result = ICResult.Success;

        var ret = 1;

        foreach (var icon in icons)
        {
            // Replace the icon
            // todo :Improve the return value handling of UpdateResource
            ret = UpdateResource(handleExe, RT_ICON, index, 0, icon.Data, icon.Size);

            index++;
        }

        var groupdata = icons.ToGroupData();

        // todo :Improve the return value handling of UpdateResource
        ret = UpdateResource(handleExe, RT_GROUP_ICON, startindex, 0, groupdata, (uint)groupdata.Length);
        if (ret == 1)
        {
            if (EndUpdateResource(handleExe, false))
                result = ICResult.Success;
            else
                result = ICResult.FailEnd;
        }
        else
            result = ICResult.FailUpdate;

        return result;
    }
}
Ernstjan Freriks
  • 622
  • 6
  • 12
  • Great! I think it's better to update icon group with a name "MAINICON" to make it more robust. I learned that from http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c12873/Hacking-Icon-Resources.htm – deerchao May 13 '16 at 08:23
  • Worked great, I had to clear icon cache to see some of the sizes update in File Explorer: https://superuser.com/questions/499078/refresh-icon-cache-without-rebooting – robertburke Oct 09 '17 at 22:11
3

This is the solution that worked for me. I was not able to write it in .NET, but have managed to write C++ DLL which I am referencing in my C# application.

C++ DLL

The contents of C++ solution I am building the DLL from:

#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <windows.h>

extern "C"
{
    #pragma pack(push, 2)
    typedef struct {
        WORD Reserved1;       // reserved, must be 0
        WORD ResourceType;    // type is 1 for icons
        WORD ImageCount;      // number of icons in structure (1)
        BYTE Width;           // icon width (32)
        BYTE Height;          // icon height (32)
        BYTE Colors;          // colors (0 means more than 8 bits per pixel)
        BYTE Reserved2;       // reserved, must be 0
        WORD Planes;          // color planes
        WORD BitsPerPixel;    // bit depth
        DWORD ImageSize;      // size of structure
        WORD ResourceID;      // resource ID
    } GROUPICON;
    #pragma pack(pop)

    __declspec(dllexport) void __stdcall ChangeIcon(char *executableFile, char *iconFile, INT16 imageCount)
    {
        int len = strlen(executableFile) + 1;
        wchar_t *executableFileEx = new wchar_t[len];
        memset(executableFileEx, 0, len);
        ::MultiByteToWideChar(CP_ACP, NULL, executableFile, -1, executableFileEx, len);

        len = strlen("MAINICON") + 1;
        wchar_t *mainIconEx = new wchar_t[len];
        memset(mainIconEx, 0, len);
        ::MultiByteToWideChar(CP_ACP, NULL, "MAINICON", -1, mainIconEx, len);

        HANDLE hWhere = BeginUpdateResource(executableFileEx, FALSE);

        char *buffer;    // Buffer to store raw icon data
        long buffersize; // Length of buffer
        int hFile;       // File handle

        hFile = open(iconFile, O_RDONLY | O_BINARY);
        if (hFile == -1)
            return; // If file doesn't exist, can't be opened etc. 

        // Calculate buffer length and load file into buffer
        buffersize = filelength(hFile);
        buffer = (char *)malloc(buffersize);
        read(hFile, buffer, buffersize);
        close(hFile);

        // Calculate header size
        int headerSize = 6 + imageCount * 16;

        UpdateResource(
            hWhere,  // Handle to executable
            RT_ICON, // Resource type - icon
            MAKEINTRESOURCE(1), // Make the id 1
            MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), // Default language
            buffer + headerSize, // Skip the header bytes
            buffersize - headerSize  // Length of buffer
        );


        GROUPICON grData;

        grData.Reserved1 = 0;     // reserved, must be 0
        grData.ResourceType = 1;  // type is 1 for icons
        grData.ImageCount = 1;    // number of icons in structure (1)

        grData.Width = 32;        // icon width (32)
        grData.Height = 32;       // icon height (32)
        grData.Colors = 0;        // colors (256)
        grData.Reserved2 = 0;     // reserved, must be 0
        grData.Planes = 2;        // color planes
        grData.BitsPerPixel = 32; // bit depth
        grData.ImageSize = buffersize - 22; // size of image
        grData.ResourceID = 1;       // resource ID is 1

        UpdateResource(
            hWhere,
            RT_GROUP_ICON,
            // RT_GROUP_ICON resources contain information
            // about stored icons
            mainIconEx,
            // MAINICON contains information about the
            // application's displayed icon
            MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
            &grData,
            // Pointer to this structure
            sizeof(GROUPICON)
        );

        delete buffer; // free memory

        // Perform the update, don't discard changes
        EndUpdateResource(hWhere, FALSE);
    }
}

C# code

This is C# code which I'm using to import ChangeIcon function from previously written DLL:

[DllImport("IconChanger.dll")]
static extern void ChangeIcon(String executableFile, String iconFile, short imageCount);

/// <summary>
/// Changes the executable's icon
/// </summary>
/// <param name="exeFilePath">Path to executable file</param>
/// <param name="iconFilePath">Path to icon file</param>
static public void ChangeIcon(string exeFilePath, string iconFilePath)
{
    short imageCount = 0;

    using (StreamReader sReader = new StreamReader(iconFilePath))
    {
        using (BinaryReader bReader = new BinaryReader(sReader.BaseStream))
        {
            // Retrieve icon count inside icon file
            bReader.ReadInt16();
            bReader.ReadInt16();
            imageCount = bReader.ReadInt16();
        }
    }

    // Change the executable's icon
    ChangeIcon(exeFilePath, iconFilePath, imageCount);
}

Hope at least somebody will find this useful.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nikola Malešević
  • 1,738
  • 6
  • 21
  • 44
  • Thanks very much Nikola for sharing this. I am having some trouble with it. I have a ico file at `C:\Users\noi\Desktop\fox.ico`, this ico contains 4 images, 16, 32, 48, and 256. When I apply it using your technique its not having any affect, may you please advise? Here is my code (it is js-ctypes) just in case it helps you advise me - https://github.com/Noitidart/Profilist/blob/master/modules/workers/MainWorker.js#L3193-L3267 - the code goes through without error, however the icon doesn't update :( – Noitidart Feb 19 '16 at 04:08
  • 1
    I don't have experience with JS, so I cannot help you on this one, unfortunately. As I said, without the help of C++, I was not able to make it as well. – Nikola Malešević Feb 19 '16 at 11:33
  • Thanks for the reply @Nikola, one question though, code language independent. In your C++ method `ChangeIcon` for the argument is it possible to pass a a ICO file that contains multiple sizes of that image inside it? If there are multiple sizes inside it does that mean `iconCount` should be greater then 1? – Noitidart Feb 19 '16 at 18:10
  • 1
    Looking at my code now, I would say that `grData.ImageCount = 1` should be changed to `grData.ImageCount = imageCount`. I probably never worked with multiple sized icons, so never encountered a bug with this. So I would say that, yes, it is possible, but I don't have access to the project anymore, so cannot be 100% positive about it. – Nikola Malešević Feb 20 '16 at 11:46
  • Thanks for that input! :) – Noitidart Feb 20 '16 at 19:28
0

C# declaration for UpdateResource:

    /// <summary>
    /// Adds, deletes, or replaces a resource in a portable executable (PE) file. There are some restrictions on resource updates in files that contain Resource Configuration (RC Config) data: language-neutral (LN) files and language-specific resource (.mui) files.
    /// </summary>
    /// <param name="hUpdate">A module handle returned by the BeginUpdateResource function, referencing the file to be updated. </param>
    /// <param name="lpType">The resource type to be updated. Alternatively, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is an integer value representing a predefined resource type. If the first character of the string is a pound sign (#), then the remaining characters represent a decimal number that specifies the integer identifier of the resource type. For example, the string "#258" represents the identifier 258. For a list of predefined resource types, see Resource Types. </param>
    /// <param name="lpName">The name of the resource to be updated. Alternatively, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is a resource ID. When creating a new resource do not use a string that begins with a '#' character for this parameter.</param>
    /// <param name="wLanguage">The language identifier of the resource to be updated. For a list of the primary language identifiers and sublanguage identifiers that make up a language identifier, see the MAKELANGID macro. </param>
    /// <param name="lpData">The resource data to be inserted into the file indicated by hUpdate. If the resource is one of the predefined types, the data must be valid and properly aligned. Note that this is the raw binary data to be stored in the file indicated by hUpdate, not the data provided by LoadIcon, LoadString, or other resource-specific load functions. All data containing strings or text must be in Unicode format. lpData must not point to ANSI data. If lpData is NULL and cbData is 0, the specified resource is deleted from the file indicated by hUpdate. Prior to Windows 7: If lpData is NULL and cbData is nonzero, the specified resource is NOT deleted and an exception is thrown.</param>
    /// <param name="cbData">The size, in bytes, of the resource data at lpData. </param>
    /// <returns>Returns TRUE if successful or FALSE otherwise. To get extended error information, call GetLastError.</returns>
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, PreserveSig = true, SetLastError = true, ExactSpelling = true)]
    public static extern Int32 UpdateResourceW(void* hUpdate, char* lpType, char* lpName, UInt16 wLanguage, [CanBeNull] void* lpData, UInt32 cbData);

For a string resource type or name, you just pass the string. For system-predefined types like RT_ICON and int IDs like IDI_APPLICATION, you pass that integer value reinterpret-casting it to a pointer, like (char*)3 for RT_ICON.

hypersw
  • 475
  • 3
  • 9