Change Icon of EXE file through code extracting it from other EXE file

shabbir's Avatar author of Change Icon of EXE file through code extracting it from other EXE file
This is an article on Change Icon of EXE file through code extracting it from other EXE file in MFC.
Rated 5.00 By 1 users
I was a FAN of Resource hacker software and always wanted to create a software like them but one day I just thought lets make a mini version of resource hacker and so wrote a program to change the ICON of an exe or dll file. The idea came to me when I saw programs that can show you the ICONS in an executable files and so I thought if we can see them then definitely we can edit them as well. If we can edit them then there is a way to give them a different ICON to be displayed in the explorer.

Here is the code for the same
Code: CPP
void OnButtonChangeIcon()
{
    CString             lpszFile;
    CString             lpszSourceFile;

    //In the sample it takes the value from the text box the URL to the source and destination file
    m_SrcPath.GetWindowText(lpszSourceFile);
    m_DestPath.GetWindowText(lpszFile);

    HRSRC hRes;         // handle/ptr. to res. info. in hExe
    HANDLE hUpdateRes;  // update resource handle
    char *lpResLock;    // pointer to resource data
    HRSRC hResLoad;     // handle to loaded resource
    BOOL result;
    HMODULE hSrcExe,hDestExe;
    int iLoop;

    //Load the source exe from where we need the icon
    hSrcExe = LoadLibrary(lpszSourceFile);
    if(hSrcExe == NULL)
        return;

    // Locate the ICON resource in the .EXE file.
    for(iLoop = 1;;iLoop++)
    {
        CString str;
        str.Format("#%d",iLoop);
        hRes = FindResource(hSrcExe, str, RT_ICON);
        if (hRes == NULL)
            continue ;
        else if(iLoop == 10)
            return;
        else
            break;
    }

    // Load the ICON into global memory.
    hResLoad = (HRSRC)LoadResource(hSrcExe, hRes);
    if (hResLoad == NULL)
        return ;

    // Lock the ICON into global memory.
    lpResLock = (char*)LockResource(hResLoad);
    if (lpResLock == NULL)
        return ;

    hDestExe = LoadLibrary(lpszFile);
    if(hDestExe == NULL)
        return;
    // Locate the ICON resource in the .EXE file.
    for(iLoop = 1;;iLoop++)
    {
        CString str;
        str.Format("#%d",iLoop);
        if (FindResource(hDestExe, str, RT_ICON) == NULL)
            continue ;
        else if(iLoop == 10)
            break;
        else
            break;
    }
    FreeLibrary(hDestExe);

    // Open the file to which you want to add the ICON resource.
    hUpdateRes = BeginUpdateResource(lpszFile, FALSE);
    if (hUpdateRes == NULL)
        return ;

    result = UpdateResource(hUpdateRes,       // update resource handle
        RT_ICON,                   // change dialog box resource
        MAKEINTRESOURCE(1),
        MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)// neutral language
        lpResLock,                   // ptr to resource info
        SizeofResource(hSrcExe, hRes)); // size of resource info.

    if (result == FALSE)
        return ;

    // Write changes then close it.
    if (!EndUpdateResource(hUpdateRes, FALSE))
    {
        return ;
    }
}
Some more explanation of the code snippets
Code: CPP
//Load the source exe from where we need the icon
hSrcExe = LoadLibrary(lpszSourceFile);
if(hSrcExe == NULL)
    return;

// Locate the ICON resource in the .EXE file.
for(iLoop = 1;;iLoop++)
{
    CString str;
    str.Format("#%d",iLoop);
    hRes = FindResource(hSrcExe, str, RT_ICON);
    if (hRes == NULL)
        continue ;
    else if(iLoop == 10) // Just to prevent from infinite loop
        return;
    else
        break;
}
It loads the executables and locate the ICON from the EXE file to inject it in other executable file.
Code: CPP
// Load the ICON into global memory.
hResLoad = (HRSRC)LoadResource(hSrcExe, hRes);
if (hResLoad == NULL)
    return ;

// Lock the ICON into global memory.
lpResLock = (char*)LockResource(hResLoad);
if (lpResLock == NULL)
    return ;
Load the ICON that we have from the source EXE into the global memory and lock it.
Code: CPP
hDestExe = LoadLibrary(lpszFile);
if(hDestExe == NULL)
    return;
// Locate the ICON resource in the .EXE file.
for(iLoop = 1;;iLoop++)
{
    CString str;
    str.Format("#%d",iLoop);
    if (FindResource(hDestExe, str, RT_ICON) == NULL)
        continue ;
    else if(iLoop == 10)
        break;
    else
        break;
}
Load the destination exe and find the ICON resource which we need to be updating.
Code: CPP
// Open the file to which you want to add the ICON resource.
hUpdateRes = BeginUpdateResource(lpszFile, FALSE);
if (hUpdateRes == NULL)
    return ;
Begin the updating of the destination exe.
Code: CPP
result = UpdateResource(hUpdateRes,       // update resource handle
    RT_ICON,                   // change dialog box resource
    MAKEINTRESOURCE(1),
    MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)// neutral language
    lpResLock,                   // ptr to resource info
    SizeofResource(hSrcExe, hRes)); // size of resource info.
 
Change the EXE to reflect the new ICON.
Code: CPP
// Write changes then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
    return ;
}
Commit the changes to the executables and return.
Attached Files
File Type: zip ExeIconUpdater_Sample.zip (13.0 KB, 958 views)
File Type: zip ExeIconUpdater_WorkingExample.zip (5.1 KB, 873 views)
luongsiviet like this
bdmnd06's Avatar, Join Date: Apr 2006
Newbie Member
Hey, I saw your example on how to change the app icon from extracting it from another executable. Do you have any idea how to change the app icon from an icon.ico file?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
What do you mean by icon.ico file. Do you mean loading a different icon than the default provided or you want to edit the default icon of the application.
bdmnd06's Avatar, Join Date: Apr 2006
Newbie Member
change the default icon in an executable with a .ico file
bdmnd06's Avatar, Join Date: Apr 2006
Newbie Member
Instead of taking the icon resource out of one executable and putting it in the other, I am trying to find a way to make an application that opens a .ico file and replace the icon resource information in the executable with the .ico file. The program resourcehacker from the link below accomplishes this very well

http://www.angusj.com/resourcehacker/
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
This can be done probably by reading the ico file using the
Code: CPP
//Create the file
hFile = CreateFile(L"PathToIconFile", GENERIC_READ,
                   0,
                   NULL,
                   OPEN_EXISTING,
                   FILE_ATTRIBUTE_NORMAL,
                   NULL);
// Read the bunary data of the file
ReadFile(hFile, lpBuffer, dwFileSize, &dwBytesRead, NULL)
then you can update the destination EXE as follow
Code: CPP
hResource = BeginUpdateResource(L"C:\\...\\t3.exe", FALSE);
if (NULL != hResource)
{
    if (UpdateResource(hResource,
        RT_ICON,
        MAKEINTRESOURCE(1),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPVOID) lpBuffer,
        dwFileSize) != FALSE)
    {
        EndUpdateResource(hResource, FALSE);
    }
}
Note this is not working code but just providing hints as how this can be done. If you find something better do share with us.
cyberpunkpor's Avatar
Newbie Member
works fine, and is good work

but if you change the icon view on windows to large icon, stays the same

only works with small icon
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Thats because it changes only one ICON and normally exe have single icon and if you have more than one in the destination exe you have to replace all sizes icon to the new one.
cyberpunkpor's Avatar
Newbie Member
Yep. that's correct. thanks for the code again.
jonason's Avatar
Newbie Member
how to change the default icon with an icon file i want to know !!