Change Icon of Executable

I have two executable files, file A and file B. I want to have a piece of code in C or C++ that reads the icon of file A and change the icon of file B to that icon. (I'm not using MFC, nor .NET, just pure MS Visual C++ Windows API's)
LVL 12
Mortaza DoulatyAsked:
Who is Participating?
JimBeveridgeConnect With a Mentor Commented:
If you read my blog entry I mentioned, then you know the question you are asking is not simple. I'm an expert in Windows SDK development. To write the routine you want, including researching it, writing unit tests, packaging it for reuse, and documenting it is easily a day of work. Anything less than that will probably fail when put into use.

Even the requirements are not entirely straightforward... Does it have to support PNG icons? 64-bit? 16-bit?

I'm obviously not going to do that much work for 500 points. If the information I've provided you isn't enough, then you should hire a C++/SDK expert to do the development work for you.

The article is titled "Reading and Using", but there is a discussion of writing resources in the text. The related Windows API functions for writing are BeginUpdateResource, UpdateResource, and EndUpdateResource.

I realize you asked for code, but if you only need to do this a few times, you can use a tool like "Resource Hacker".
Mortaza DoulatyAuthor Commented:
Thanks for your quick reply. I've already used "Resource Hacker", but I need a piece of code. I'm not sure what to do with resources, in case of using and changing icons.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

lucky_jamesConnect With a Mentor Commented:
PMString ucPath("C:\\path\\to\\file.extension");
SHFILEINFO info;    

::SHGetFileInfo(ucPath.GrabTString(), FILE_ATTRIBUTE_NORMAL, &info, sizeof(info),
ICONINFO stIconInfo;
GetIconInfo(s_sfi.hIcon, &stIconInfo);
HBITMAP hBmp = stIconInfo.hbmColor;
DestroyIcon(s_sfi.hIcon) ;

Your solution and the articles you referenced will not work. SHGetFileIcon throws away all sizes and formats except one and you lose the information about the format of the resource itself.

See my recent blog entry as well as the blog entry by Raymond Chen referenced in the comments of my post:
Mortaza DoulatyAuthor Commented:
Thanks for your reply. Could you please describe more about s_sfi
What is this, where it is declared and initialized. It seems that you've copied and pasted the code from somewhere and you forget to include a reference to that variable. Thanks.

I've read them, so what to do next?
Mortaza DoulatyAuthor Commented:
Thanks for your quick reply. I've already read your blog entry. I've used ResourceHacker before, I thought I could find some piece of source code to do that job for me easily.
About requirements, I'm not sure yet, ResourceHacker works just fine for me, I think it supports PNG, 64-bit and 16 bit altogether.

I'll wait for other experts to post their comments, after some days, I'll finalize this question.

Thanks for your support!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.