I'm using this code to take the icon from an exe and replace the icon in another exe, but it doesn't work. Every time lpBuffer is empty after GlobalLock. the hIcon handle is retrieved by ExtractIcon();
What am I doing wrong?
Not sure if this code works because I didn't try it myself, but it seems you have to make your own implementation that converts from HICON to raw data pointer used in a UpdateResource() call.
Also, it seems that LockResource() should be used instead of GlobalLock().
Please find the attachment containing the web page from the link: http://www.crispen.org/src/archive/0005.html
Then again, I think the second link I've posted was better.
It seems that many before had the same problem you're facing.
Probably, many again will, so, it is a good thing for you to post a FAQ when will have a solution.
The links you provided me with show only how to read an icon from an .ico file, which is not what I need.
I found the following code in MSDN (Q104570) on howw to save an HICON to disk and then read it and so gain an pointer to the icon.
int NEAR _pascal SaveMyIcon( HICON hIcon)
int fh, i, iResult;
OFSTRUCT of; if (!hIcon)
return FALSE; dwSize = GlobalSize(hIcon);
lpGMem = GlobalLock(hIcon);
fh = OpenFile ("myicon.bin", &of, OF_WRITE | OF_CREATE); if (fh == -1) // If NOT opened successfully.
MessageBox(NULL, "Unable to create file", NULL, MB_OK );
} uiSize = _lwrite(fh, (LPSTR)lpGMem, (UINT)dwSize);
_lclose(fh); if (uiSize == -1 || uiSize < (UINT)dwSize)
MessageBox(NULL, "Unable to read file", NULL, MB_OK );
else // Everything worked, return hGMem.
The problem is that it doesn't work. The lpGMem pointer is always NULL.
Is it might be because I don't know what type lpGMem is (the article doesn't say). I made it LPTSTR, LPBYTE... and it doesnt work. What sould I do? Is it really not posible to gain a valid pointer from an HICON handle provided by ExtractIcon()?
I found a software doing what you want to do, so it is possible http://www.softboy.net/exeico/
I've seen another sample code using a different function to extract an icon from a file, and maybe you want to try it out:
Since you've said before that drawing the icon obtained with ExtractIcon() works, but LockResource() or GlobalLock() are always returning NULL, makes me believe that ExtractIcon() doesn't fail, so there's no need to change it.
I have another question though:
Do you use LoadLibrary() to load the module from where you need to copy your icon? http://msdn.microsoft.com/library/de...gResources.asp
Originally Posted by MSDN
The pointer returned by LockResource is valid until the module containing the resource is unloaded. It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.
Do not try to lock a resource by using the handle returned by the FindResource or FindResourceEx function. Such a handle points to random data.
These are the remarks on LockResource() function.
I'm so sorry I couldn't make the time to write a fully functional sample code, but I hope these links will lead you to it.