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 !!

shabbir 20Aug2006 16:54

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Quote:

Originally Posted by jonason
how to change the default icon with an icon file i want to know !!

Refer to this code.

jonason 21Aug2006 08:49

Re: Change Icon of EXE file through code extracting it from other EXE file
 
int szFile;
BYTE *lpBuffer;
DWORD dwRead;
HANDLE hResource;
HANDLE hFile=CreateFile("E:\\aa.ico", GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_N ORMAL,NULL);
if (hFile==INVALID_HANDLE_VALUE)
return;
szFile=int(GetFileSize(hFile,NULL));
lpBuffer = new BYTE[szFile];
ReadFile(hFile,lpBuffer,sizeof(lpBuffer),&dwRead,N ULL);
hSrcExe = LoadLibraryEx("E:\\a.exe",NULL, LOAD_LIBRARY_AS_DATAFILE);
if(hSrcExe == NULL)
return;
hRes = FindResource(hSrcExe, MAKEINTRESOURCE(1), RT_ICON);
if (hRes == NULL)
return;
FreeLibrary(hSrcExe);
hResource = BeginUpdateResource("E:\\a.exe", FALSE);
if (hResource == NULL) return ;
result = UpdateResource(hResource,RT_ICON,MAKEINTRESOURCE(1 ),MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),(LPVOID)lpBuffer,sizeof(lpBuff er
if (result == FALSE) return ;
EndUpdateResource(hResource, FALSE);
CloseHandle(hFile);
delete[] lpBuffer;

it can replace the defaulte icon but can't display the aa.ico in the EXE file ,i don't know the reason
can you tell me what's wrong with above codes and give a complete code for this?
thanks!

shabbir 21Aug2006 10:45

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Please use the bbcode for codes so that its easy to see and analyze the code.

Also you dont have the correct syntax for the UpdateResource function and line is truncated.

Also can you alrify a bit more on what you mean by it can replace the defaulte icon but can't display the aa.ico in the EXE file?

pngpas 25Oct2006 02:11

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Hi,

I tried following code, but it is just not working :(. Can anybody help me out here?

Thanks.
Code:


HMODULE hSrc = LoadLibrary(L"C:\\GPSamples\\vmplayer.exe");
if (hSrc == NULL)
{
        return 1;
}

HRSRC hRes, hResLoad;
char *lpResLock;

hRes = FindResource(hSrc, MAKEINTRESOURCE(1), RT_ICON);
if (hRes == NULL)
{
        return 1;
}


hResLoad = (HRSRC)LoadResource(hSrc, hRes);
if (hResLoad == NULL)
return 1;

lpResLock = (char *)LockResource(hResLoad);
if (lpResLock == NULL)
return 1;

HANDLE hUpdateRes;
hUpdateRes = BeginUpdateResource(L"C:\\GPSamples\\vmnetcfg.exe", FALSE);
if (hUpdateRes == NULL)
{
       
        return 1;
}

if (!UpdateResource(hUpdateRes,
        RT_ICON,
        MAKEINTRESOURCE(1),
        MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
        lpResLock,
        SizeofResource(hSrc, hRes)))
{
       
        return 1;
}

if (!EndUpdateResource(hUpdateRes, FALSE))
{
        return 1;
}

FreeLibrary(hSrc);


pngpas 25Oct2006 02:15

Re: Change Icon of EXE file through code extracting it from other EXE file
 
BTW, I tried the exe Shabbir posted, not working for me either.

pngpas 25Oct2006 09:04

Re: Change Icon of EXE file through code extracting it from other EXE file
 
I tired following code, doesn't work either :(
Code:

int main(int argc, char* argv[])
{
       
        HANDLE hIcon = CreateFile("D:\\Samples\\xvid.ico",
                GENERIC_READ,
                0,
                NULL,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                NULL);
        if (!hIcon)
        {
                return 1;
        }
       
        LPBYTE lpBuf;
        DWORD dwFileSize, dwBytesRead;
       
        dwFileSize = GetFileSize(hIcon, NULL);
        lpBuf = (LPBYTE)malloc(dwFileSize);
        if (!lpBuf)
        {
                CloseHandle(hIcon);
                return 1;
        }
       
        ReadFile(hIcon, lpBuf, dwFileSize, &dwBytesRead, NULL);
        if (dwBytesRead != dwFileSize)
        {
                free(lpBuf);
                CloseHandle(hIcon);
                return 1;
        }
       
        CloseHandle(hIcon);
       
       
        HANDLE hUpdateRes;
        hUpdateRes = BeginUpdateResource("D:\\Samples\\iexplore.exe", FALSE);
        if (hUpdateRes == NULL)
        {
                free(lpBuf);
                return 1;
        }
       
        if (!UpdateResource(hUpdateRes,
                RT_ICON,
                MAKEINTRESOURCE(32528),
                MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
                lpBuf, dwBytesRead))
        {
                free(lpBuf);
                return 1;
        }
       
        if (!EndUpdateResource(hUpdateRes, FALSE))
        {
                free(lpBuf);
                return 1;
        }
       
        free(lpBuf);
       
        printf("Hello World!\n");
        return 0;
}


shabbir 25Oct2006 10:38

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Quote:

Originally Posted by pngpas
BTW, I tried the exe Shabbir posted, not working for me either.

Did you try downloading the sample source code as well.

Also in the code sample you have posted you are not using the FindResource function for all the id's but just for 1. That may not be present in the destination executable. Try using my sample for some other executable as I have looped only till 10 for just a sample.

pngpas 25Oct2006 19:39

Re: Change Icon of EXE file through code extracting it from other EXE file
 
actually, I tried that already, didn't work either. How about the code I posted that reads an icon file? Can you tell what is wrong with it?

shabbir 25Oct2006 19:52

Re: Change Icon of EXE file through code extracting it from other EXE file
 
You are specifying 32528 as the id which may not exist in the final destination. internet explorer it does not exist.

JohnnyH 13Dec2006 01:56

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Hi Shabbir, I have implemented some code that does something similar to your example above except that it copies Version information from one executable's resource information into another executable's resource information. It works as long as both the source executable and destination executable have resource files compiled into them. But the problem I am having is that if the destination executable (the one I want to copy the resources into) does not already have a resource file compiled into it the copy action appears to work (all functions return successfully) but the target executable is no longer runable and the version information that I tried to copy into it is not retrievable. Do you know if it is possible to copy an executable's resources into another executable if the destination executable doesn't have a resource file compiled into it?

Thanks,
John

shabbir 13Dec2006 11:21

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Quote:

Originally Posted by JohnnyH
I have implemented some code that does something similar to your example above except that it copies Version information from one executable's resource information into another executable's resource information. It works as long as both the source executable and destination executable have resource files compiled into them. But the problem I am having is that if the destination executable (the one I want to copy the resources into) does not already have a resource file compiled into it the copy action appears to work (all functions return successfully) but the target executable is no longer runable and the version information that I tried to copy into it is not retrievable. Do you know if it is possible to copy an executable's resources into another executable if the destination executable doesn't have a resource file compiled into it?

It occurs when there is not enough buffer in the target executable.

For this you can refer to Updating version information at run-time at codeproject

JohnnyH 13Dec2006 21:29

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Hi shabbir, thanks for responding. I have seen the reference you pointed me to, but it's not quite the same scenario. The example at codeguru is updating the version number (at runtime) in a Version resource that is already compiled into the executable. In my case, I am trying to copy a version resource from one executable into another executable that doesn't have any resources compiled into it at all. I am wondering if you know of a way to do that or if it is possible. What I am trying to do works if the destination executable has a resource (any resource ie. icon, dialog etc.) compiled into it already, but it doesn't work if the destination executable doesn't have a resource compiled into it already.

Thanks,
John

shabbir 14Dec2006 06:16

Re: Change Icon of EXE file through code extracting it from other EXE file
 
JohnnyH, can you share your code so that we can have a look at it. I would suggest using a new thread in the Queries and Discussion forum.

jackl007 11Aug2007 00:12

Re: Change Icon of EXE file through code extracting it from other EXE file
 
what happend with the complete proyect.. i want to changer the icon from a EXE using my EXE.

shabbir 11Aug2007 10:11

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Quote:

Originally Posted by jackl007
what happend with the complete proyect.. i want to changer the icon from a EXE using my EXE.

I could not get you? Can you please explain a bit more.

ibraheempindi 23Aug2007 01:58

Re: Change Icon of EXE file through code extracting it from other EXE file
 
hy! hope you'll be fine, thanks for the code but could you please explain about the limitations, i mean which sort of ico on which it will work, coz i tried several exe's from my program files but didnt work on any of them.....may be coz of different sizes, but could you please also attach any sample exe's on which the code is working properly, thanks again once again for sharing such a valuable thing...have a nice day
best regards
Ibraheem

shabbir 23Aug2007 08:22

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Quote:

Originally Posted by ibraheempindi
hy! hope you'll be fine, thanks for the code but could you please explain about the limitations, i mean which sort of ico on which it will work, coz i tried several exe's from my program files but didnt work on any of them.

It will work on any exe but will work on the first Icon that will be replaced but if any exe has 10 blank icons resource it will not work which is commented as well. See Just to prevent from infinite comment.

Quote:

Originally Posted by ibraheempindi
could you please also attach any sample exe's on which the code is working properly,

Working sample is already there for you to see and run.

ibraheempindi 23Aug2007 13:27

Re: Change Icon of EXE file through code extracting it from other EXE file
 
thanks ....but i wsn't still able to change icon of any exe, could you please send me any two exe's just for example, i didn't find any sample exe's to use as source and destination exe's with in your attachments on which it will work......waiting anxiously for your reply.

shabbir 23Aug2007 17:12

Re: Change Icon of EXE file through code extracting it from other EXE file
 
There is a "Attached Files" section after the article where you should find the sample attachment as well.

ibraheempindi 23Aug2007 17:18

Re: Change Icon of EXE file through code extracting it from other EXE file
 
i have both of them, one contained the project source while other contains the binary, but there is no sample exe's to use as source and dest on which it will work...... :rolleyes:

shabbir 23Aug2007 17:57

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Quote:

Originally Posted by ibraheempindi
i have both of them, one contained the project source while other contains the binary, but there is no sample exe's to use as source and dest on which it will work...... :rolleyes:

You can keep the exe and its copy and then run the application with the source file as some notepad and the destination one as the copy exe and then it will update the copy exe correctly. It will work on exe which has only one Icon embedded.

ibraheempindi 24Aug2007 13:01

Re: Change Icon of EXE file through code extracting it from other EXE file
 
yeah...that's why may be its not working with mine ones...but in prof products its rare to hve one icon, and each icon have several images, i'm trying to update one, and playing around with it, but it merges some images of an ico file from destination and some from source in the same ico file.....is it posb to make it work for multiple icon embeded application and to update an ico resource with multiple images in it....thanks for your time

shabbir 24Aug2007 17:24

Re: Change Icon of EXE file through code extracting it from other EXE file
 
The code can always be updated to change all the icons. I have given here how to do it (a prototype) and it can always be extended. Isn't it?

ibraheempindi 24Aug2007 19:56

Re: Change Icon of EXE file through code extracting it from other EXE file
 
hmmm..i think soo....by changing these two params,
one is the 3rd param of updateresource and other is FindResource 2nd param in source exe, you can browse arount the images placed in that ico file, and replace as you wish them to be, so its working now, but could you give me some hints that instead of source exe, if i have to load an *.ico image file and then replace the resource in the desitnation exe...how it would be...thanks and have a nice day :)

shabbir 24Aug2007 22:11

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Read through the comments and you will find that as well.

mudasir 31Oct2007 11:10

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Dear Shabir
i have run ur code to change the icon of an exe file .But i change the icon of an exe file only once.next time u run this code nothing happens(until i restart my computer).in fact in output window a message is displayed which is

The therad 'win32 Thread' has exited with code 0 (0*0)
The program '[3628]' Exeupdate.exe Native ' has exited with code 0 (0*0)

will u plz guide me about this problem.

shabbir 31Oct2007 11:30

Re: Change Icon of EXE file through code extracting it from other EXE file
 
I am unable to re-create your problem but it looks like the exe file you are trying to change is locked by some process and it may be the ExeUpdater which is used for first time. Check if that is the case.

mudasir 1Nov2007 12:02

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Let me explain
when i run this code then the icon of my exe file flushes once to new ion then again becoms the original icon.(e.g i want to change of msn.exe Icon with yahoo.exe Icon but msn icon again changes to msn Icon).

shabbir 1Nov2007 17:14

Re: Change Icon of EXE file through code extracting it from other EXE file
 
Quote:

Originally Posted by mudasir
Let me explain
when i run this code then the icon of my exe file flushes once to new ion then again becoms the original icon.(e.g i want to change of msn.exe Icon with yahoo.exe Icon but msn icon again changes to msn Icon).

Try seeing in resource hacker and see if it gets replaced by some icon / icon group and then MSN flushes and shows some other icon group in explorer.

parvez.yu 6Mar2008 14:48

Re: Change Icon of EXE file through code extracting it from other EXE file
 
i tried it is so nice


All times are GMT +5.5. The time now is 22:27.