I'm trying to copy all icon resources from one executable to another
executable (containing no icon resources initially) from code. Doing
this must be possible since InstallShield Developer does a similar
thing when repackaging executables and storing the icons into a newly
created executable, which is then loaded inside the icon table within
a Windows Installer package.
Anyway, all around the newsgroups you can read that the
BeginUpdateResource, updateresource and EndUpdateResource should be
used.
I tried this, using the RT_GROUP_ICON resource type and using the
EnumResourceNames function, which results in an executable having the
correct number of icons, but the icons themselves are empty!...
As this was a small succes :), a next step would be to not only update
the RT_GROUP_ICON resource types but also the RT_ICON type.. but no
results...
Here's the code...
void ReplaceIconFromOneExeToAnother(const CString & strSourceExe,
const CString & strDestExe)
{
HINSTANCE hLibrary;
HRSRC hRes;
HGLOBAL hResLoad;
void* lpResLock ;
BOOL bResult;
int nIcons = ExtractIconEx(strSourceExe, -1, NULL, NULL, 0);
if( (hLibrary = LoadLibraryEx( strSourceExe, NULL,
LOAD_LIBRARY_AS_DATAFILE )) == NULL )
{
return ;
}
ghResourceToBeUpdated = BeginUpdateResource(
strDestExe, TRUE);
// get the source resource...
for (int i = 1; i <= nIcons; i++)
{
CString strResourceName; strResourceName.Format("#%ld", i);
hRes = FindResource(hLibrary, strResourceName, RT_GROUP_ICON);
ASSERT(hRes);
hResLoad = LoadResource(hLibrary, hRes);
lpResLock = LockResource(hResLoad);
bResult = UpdateResource (ghResourceToBeUpdated, RT_GROUP_ICON,
strResourceName,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), lpResLock,
SizeofResource(hLibrary, hRes));
UnlockResource(lpResLock);
//
hRes = FindResource(hLibrary, strResourceName, RT_ICON);
ASSERT(hRes);
hResLoad = LoadResource(hLibrary, hRes);
lpResLock = LockResource(hResLoad);
bResult = UpdateResource (ghResourceToBeUpdated, RT_ICON,
MAKEINTRESOURCE(1),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), lpResLock,
SizeofResource(hLibrary, hRes));
UnlockResource(lpResLock);
}
// update the resource...
EndUpdateResource(ghResourceToBeUpdated, FALSE);
FreeLibrary(hLibrary);
}
BTW, All pointers and handles are valid. In another newsgroup article
(which can be found on google by specifying 'beginupdateresource icon'
in the groups search) it was stated that this was indeed tricky, since
the icon headerinfo is separated from the actual image data (which
seems to be the problem here also..
Furthermore I'm stunned about the lack of documentation on this from
Microsoft.
So the question is... where's the catch and where do I need to adjust
the code ?
I've seen some delphi code which inspects the PE header info and
replaces existing icon info, but this only works if the icon file
already exists in the destination file and has the same size and color
attributes...
If some of the Microsoft guys (or other gurus) could post some code
which does the trick I would be grateful...
Cheers,
Hans Stevens
hans__...@hotmail.com (hans__...@delthis.hotmail.com) spake the secret code
<8c878311.02013...@posting.google.com> thusly:
>If some of the Microsoft guys (or other gurus) could post some code
>which does the trick I would be grateful...
I haven't ever done this myself, but you might get a response from
someone who has if you post to a Win32 programming newsgroup. I would
try microsoft.public.win32.programmer.gdi.
--
Ask me about my upcoming book on Direct3D from Addison-Wesley!
Direct3D Book http://www.xmission.com/~legalize/book/
Don't Support Spammers! Boycott Fractal Painter 7!
http://www.xmission.com/~legalize/spammers.html