Click here to Skip to main content
14,666,916 members
Articles » General Programming » DLLs & Assemblies » General
Posted 2 Nov 2008


55 bookmarked

Replacing ICON resources in EXE and DLL files

Rate this:
4.86 (22 votes)
Please Sign up or sign in to vote.
4.86 (22 votes)
13 Nov 2008GPL3
An easy example of how to read a *.ico file and how to replace an icon resource.


The purpose of this article is to explain the structures associated with an ICO file and an icon resource. The code examples will demonstrate the use of those structures and how to embed the structures in a DLL file.


I started to work on my own resource hacker because I needed to automate string and icon replacement in a DLL file. I started reading on MSDN about working with libraries and the basic functions for working with resources.

I learned that enumerating resources and opening resources isn't so difficult, but the problem arises when you actually want to do something with that resource, like modifying or replacing it.

The most useful article was: Binary resource formats. Here, I found most of what I needed to know about working with the array of bytes that the OpenResource function returned.

To learn how different functions that I used work, you should check out MSDN: Resource Reference.

Using the code

I will start by explaining the structures associated with an ICO file and with an icon resource. The ICO file is an array of bytes arranged after some simple rules. The ICO file consist of an ICONDIR structure that is placed at the beginning of the file:

typedef struct 
    WORD            idReserved;   // Reserved
    WORD            idType;       // resource type (1 for icons)
    WORD            idCount;      // how many images?
    LPICONDIRENTRY    idEntries; // the entries for each image
  • idReserved is always 0.
  • idType is 1 for icon files.
  • idCount is the number of images that the ICO file contains.
  • idEntries is a pointer to idCount structures describing the images in the file (the structures are written in the file immediately after ICONDIR). The type of the structures is ICONDIRENTRY.
typedef struct
    BYTE    bWidth;               // Width of the image
    BYTE    bHeight;              // Height of the image (times 2)
    BYTE    bColorCount;          // Number of colors in image (0 if >=8bpp)
    BYTE    bReserved;            // Reserved
    WORD    wPlanes;              // Color Planes
    WORD    wBitCount;            // Bits per pixel
    DWORD    dwBytesInRes;         // how many bytes in this resource?
    DWORD    dwImageOffset;        // where in the file is this image
  • dwBytesInRes represents the total size of the image, that is the total size of the ICONIMAGE structure that represents the actual image.
  • dwImageOffset represents the offset within the ICO file where the ICONIMAGE structure for that image can be found.

I haven't actually tried to modify the way an image looks by playing with the ICONIMAGE fields, so I can only say that an image has the following structure:

RGBQUAD icColors[1];
BYTE icXOR[1];
BYTE icAND[1];

and the BITMAPINFOHEADER has the following fields, but only the Size, Width, Height, Planes, BitCount, and SizeImage are used. The rest are 0:

typedef struct tagBITMAPINFOHEADER{
  DWORD  biSize; 
  LONG   biWidth; 
  LONG   biHeight; 
  WORD   biPlanes; 
  WORD   biBitCount; 
  DWORD  biCompression; 
  DWORD  biSizeImage; 
  LONG   biXPelsPerMeter; 
  LONG   biYPelsPerMeter; 
  DWORD  biClrUsed; 
  DWORD  biClrImportant; 

In my program, I defined the following structure:

typedef struct
    UINT            Width, Height, Colors; // Width, Height and bpp
    LPBYTE            lpBits;                // ptr to DIB bits
    DWORD            dwNumBytes;            // how many bytes?
    LPBITMAPINFO    lpbi;                  // ptr to header
    LPBYTE            lpXOR;                 // ptr to XOR image bits
    LPBYTE            lpAND;                 // ptr to AND image bits

To learn more about how to use this structure, you should check out this source code: ICONS.c.

So now, let's summarize how to read the ICO file:

First, read the ICONDIR structure:

ReadFile( hFile1, &(pIconDir->idReserved), sizeof( WORD ), &dwBytesRead, NULL );
ReadFile( hFile1, &(pIconDir->idType), sizeof( WORD ), &dwBytesRead, NULL );
ReadFile( hFile1, &(pIconDir->idCount), sizeof( WORD ), &dwBytesRead, NULL );

Next, read the idCount and ICONDIRENTRY structures:

  ReadFile( hFile1, &pIconDir->idEntries[i], sizeof(ICONDIRENTRY), 
                    &dwBytesRead, NULL );

Now, for each ICONDIRENTRY, find the associated ICONIMAGE structure that represents the actual image (the nice colored pixels) in the ICO file, and read it.

  pIconImage = (LPICONIMAGE)malloc( pIconDir->idEntries[i].dwBytesInRes );
  SetFilePointer( hFile1, pIconDir->idEntries[i].dwImageOffset,NULL, FILE_BEGIN );
  ReadFile( hFile1, pIconImage, pIconDir->idEntries[i].dwBytesInRes,
                        &dwBytesRead, NULL);
  arrayIconImage[i]=(LPICONIMAGE)malloc( pIconDir->idEntries[i].dwBytesInRes );
  memcpy( arrayIconImage[i],pIconImage, pIconDir->idEntries[i].dwBytesInRes );

Now, you have all the structures that represent the ICO file.

The structures used in a DLL file are almost the same as the ones I described above. So, now that you know how the ICO file is structured, modifying the icon resources in the DLL will be pretty easy.

Let's see the structures I use for updating the DLL. I will only point out the difference between this and the structures above:

typedef struct 
    WORD            idReserved;   // Reserved
    WORD            idType;       // resource type (1 for icons)
    WORD            idCount;      // how many images?
    LPMEMICONDIRENTRY    idEntries; // the entries for each image

MEMICONDIR is the same as ICONDIR.

typedef struct
    BYTE    bWidth;               // Width of the image
    BYTE    bHeight;              // Height of the image (times 2)
    BYTE    bColorCount;          // Number of colors in image (0 if >=8bpp)
    BYTE    bReserved;            // Reserved
    WORD    wPlanes;              // Color Planes
    WORD    wBitCount;            // Bits per pixel
    DWORD    dwBytesInRes;         // how many bytes in this resource?
    WORD    nID;                  // the ID

As you can see, for a resource entry, we don't need the dwImageOffset field anymore. The reason is that we can find or update an image resource in a DLL using the bundle ID (nID). The FindResource and UpdateResource need to know the type of the resource (lbType), the language (langId), and the bundle id (lpName).

The way an ICO resource is saved in a DLL is as follows:

The RT_GROUP_ICON resource contains the MEMICONDIR and MEMICONDIRENTRY structures. The RT_GROUP_ICON is what the Resource Editor in VS uses to show you the icons. If these resources don't exist, you can't see the icon resources in the Resource Editor.

For each MEMICONDIRENTRY, there is an RT_ICON resource with the bundle ID identified by the nID field in the MEMICONDIRENTRY structure.

Now, we can replace the icon resource (we know the bundle ID (lpName), the language (langID), and the type RT_GROUP_ICON of the resource we want to replace) in the DLL using the structures that we found in the ICO file:

hUi = LoadLibraryExA(lpFileName,NULL,
HRSRC hRsrc = FindResourceEx(hUi, RT_GROUP_ICON, lpName,langId);

HGLOBAL hGlobal = LoadResource( hUi, hRsrc );
    test1 =(BYTE*) LockResource( hGlobal );

First, we read the resource that exists in the DLL using test1. We build the lpGrpIconDir to save the RT_ICON bundle IDs that are initially used.

test=new BYTE[cbRes];

lpGrpIconDir->idEntries=new MEMICONDIRENTRY[lpGrpIconDir->idCount];
    if(i<lpInitGrpIconDir->idCount) //nu am depasit numarul initial de RT_ICON

We built the array of bytes that we'll need to update the resource. The UpdateResource function has to receive a pointer to an array that is built exactly as described above. If the new icon resource contains more images than the initial resource, we'll attribute new bundle IDs. If it contains less images, we'll delete the RT_ICON resources that aren't used anymore.

hUpdate = BeginUpdateResourceA(lpFileName, FALSE);


                       langId,pIconImage[i], lpGrpIconDir->idEntries[i].dwBytesInRes);

If there are RT_ICON resources that aren't referenced by the updated RT_GROUP_ICON, we delete them:


Here are some functions that we'll have to read about on MSDN and the function I used in my program:


This is used to get a handle to the DLL file. (My example works only if the DLL isn't opened by the resource editor in VS.)

HRSRC hRsrc = FindResourceEx(hUi, RT_GROUP_ICON, lpName,langId);
HGLOBAL hGlobal = LoadResource( hUi, hRsrc );
LockResource( hGlobal );

These are used to load a specific resource in the memory as a pointer to an array of bytes.

hUpdate = BeginUpdateResourceA(lpFileName, FALSE);

You need thes functions to update a specific resource. Remember that UpdateResources must be given an array of bytes that has the format described in the Binary Resource Formats article.


This function enumerates all the resource types in the DLL. EnumTypesFunc is my callback function that gets the actual resource types and calls the EnumNamesFunc().


This function enumerates all the resource bundle names. My callback function is:

BOOL EnumNamesFunc(HANDLE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)

The callback function will receive the type of the resource and the name. The name represents the bundle ID. Each resource type can have one or more resources identified by a unique bundle ID.


This function enumerates all the languages available for a resource identified by its type (lpType) and bundle ID (lpName):

LPICONIMAGE* ExtractIcoFromFile(LPSTR filename,LPICONDIR pIconDir) 

This function opens a *.ico file and reads the data in the ICONDIR and ICONIMAGE structures:

BOOL ReplaceIconResource(LPSTR lpFileName, LPCTSTR lpName,  
                         UINT langId, LPICONDIR pIconDir,LPICONIMAGE* pIconImage) 

This function uses the ICONDIR and ICONIMAGE structures to replace an icon resource identified by the bundle ID (lpName) and the language ID (langId).


The program that I gave as an example must receive the following parameters: the ID of the RT_GROUP_ICON (try 105 for test.dll) resource you want to replace the name of in the DLL file, and the name of the ICO file (remember to give the correct path if the files are not in your projects folder).

Other Projects

Another project I'm working on consists of a blog and website about Web Interfaces. Currently, the articles appear only in Romanian, but pretty soon, there will be more articles and tutorials about Web technologies and Web interface design, also in English.

Please visit my blog at Interfete Web.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Maria Nadejde
Software Developer
Romania Romania
I am in my final year at Polytechnic University of Bucharest.
I am working on developing my own resource hacker.
In the near future i will be working on my diploma that would be about parallel computing. I will work on Linux platforms using mpi and openmp.

In my free time I write articles and tutorial on and this blog

Comments and Discussions

GeneralMy vote of 5 Pin
alexbnc25-Feb-12 11:44
Memberalexbnc25-Feb-12 11:44 
Generalicon by name - icon by ID Pin
kpirkl28-Mar-11 7:08
Memberkpirkl28-Mar-11 7:08 
QuestionHow to run the source ?? Pin
gupta.gourav217-Jan-10 19:06
Membergupta.gourav217-Jan-10 19:06 
AnswerRe: How to run the source ?? Pin
Maria Nadejde9-Jan-10 3:53
MemberMaria Nadejde9-Jan-10 3:53 
GeneralResourceLib: more general approach Pin
dB.5-Aug-09 3:53
MemberdB.5-Aug-09 3:53 
GeneralNicely Copied from MSDN Pin
ankit vc15-Apr-09 8:30
Memberankit vc15-Apr-09 8:30 
QuestionDoes this code handle Vista's "PNG-compressed" icons? Pin
Curtis J17-Nov-08 22:07
MemberCurtis J17-Nov-08 22:07 
AnswerRe: Does this code handle Vista's "PNG-compressed" icons? Pin
Maria Nadejde18-Nov-08 1:26
MemberMaria Nadejde18-Nov-08 1:26 
GeneralGreat Article - A Free tool is also available. Pin
dpminusa17-Nov-08 18:06
Memberdpminusa17-Nov-08 18:06 
GeneralChange icon in own exe Pin
JimmyO17-Nov-08 13:06
MemberJimmyO17-Nov-08 13:06 
GeneralRe: Change icon in own exe Pin
Maria Nadejde17-Nov-08 22:31
MemberMaria Nadejde17-Nov-08 22:31 
GeneralGood, but too long... Pin
Victor Nijegorodov10-Nov-08 23:21
MemberVictor Nijegorodov10-Nov-08 23:21 
GeneralRe: Good, but too long... Pin
Maria Nadejde11-Nov-08 0:58
MemberMaria Nadejde11-Nov-08 0:58 
GeneralSimilar Approach Pin
Sergey Alexander Gynech3-Nov-08 9:21
MemberSergey Alexander Gynech3-Nov-08 9:21 
GeneralRe: Similar Approach Pin
gupta.gourav217-Jan-10 4:09
Membergupta.gourav217-Jan-10 4:09 
GeneralGood Pin
Jose Menendez P3-Nov-08 5:07
MemberJose Menendez P3-Nov-08 5:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.