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
Some more explanation of the code snippets
It loads the executables and locate the ICON from the EXE file to inject it in other executable file.
Load the ICON that we have from the source EXE into the global memory and lock it.
Load the destination exe and find the ICON resource which we need to be updating.
Begin the updating of the destination exe.
Change the EXE to reflect the new ICON.
Commit the changes to the executables and return.
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 ;
}
}
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;
}
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 ;
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;
}
Code: CPP
// Open the file to which you want to add the ICON resource.
hUpdateRes = BeginUpdateResource(lpszFile, FALSE);
if (hUpdateRes == NULL)
return ;
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.
Code: CPP
// Write changes then close it.
if (!EndUpdateResource(hUpdateRes, FALSE))
{
return ;
}
luongsiviet
like this

