Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/articles/mfc-tutorials/)
-   -   Change Icon of EXE file through code extracting it from other EXE file (http://www.go4expert.com/articles/change-icon-exe-file-code-extracting-t643/)

shabbir 18Mar2006 20:14

Change Icon of EXE file through code extracting it from other EXE file
 
2 Attachment(s)
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.

bdmnd06 29Apr2006 21:31

Re: Change Icon of EXE file through code extracting it from other EXE file
 
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 30Apr2006 19:24

Re: Change Icon of EXE file through code extracting it from other EXE file
 
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 4May2006 01:46

Re: Change Icon of EXE file through code extracting it from other EXE file
 
change the default icon in an executable with a .ico file

bdmnd06 4May2006 01:53

Re: Change Icon of EXE file through code extracting it from other EXE file
 
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 4May2006 07:38

Re: Change Icon of EXE file through code extracting it from other EXE file
 
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 27Jul2006 06:04

Re: Change Icon of EXE file through code extracting it from other EXE file
 
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 :D

shabbir 27Jul2006 10:36

Re: Change Icon of EXE file through code extracting it from other EXE file
 
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 27Jul2006 15:31

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Yep. that's correct. thanks for the code again.

jonason 20Aug2006 14:16

Re: Change Icon of EXE file through code extracting it from other EXE file
 
how to change the default icon with an icon file i want to know !!


All times are GMT +5.5. The time now is 20:43.