Saturday, April 4, 2009

Adding an icon on a executable file with C/C++

Hello everybody,

Many days without post because I was thinking about a diferent idea to publish.

Finally, it showed up.

Every compilers that I've used add the default icon when an executable is created. As a matter of design, I've been looking for a way to add an own icon on the executable.

I got the achievement with C/C++ in Windows.

C/C++ programs can use a resources file. It is a text file that can be written manually by the programmer. Many information can be inserted on executable through it i.e: Description, Company, Version...and the name of the icon file.

Steps:
1-) Icon file must have the .ico extension. (We will use "im.ico")
2-) Create the resources file with .rc extension. (We will use "recTest.rc")
3-) Write the following lines in it:

#include <winver.h>

MEUAPLIC ICON DISCARDABLE "im.ico"

1 VERSIONINFO
FILEVERSION 3,3,0,0
PRODUCTVERSION 3,3,0,0
#ifdef _DEBUG
FILEFLAGS 0xbl
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904B0"
BEGIN
VALUE "CompanyName", "GenericCompany\0"
VALUE "FileDescription", "GenericApplication\0"
VALUE "FileVersion", "1.0\0"
VALUE "InternalName", "1.0\0"
VALUE "LegalCopyright", "Copyright \251 Generic Company. 1997\0"
VALUE "LegalTrademarks", "Generic Trademark.\0"
VALUE "OriginalFilename", "\0"
VALUE "ProductName", "Generic Application.\0"
VALUE "ProductVersion", "1.0\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END

4-) Create the file "recTestLib.rc" with the following content:
#include "recTest.rc"

5-) Use the following arguments to the program windres.exe(installed with Devian) to transform the resources text file to binary.
windres -i recTestLib.rc --input-format=rc -o recTestLib.res -O coff

6-) Compile your source file (g.e: source.c) with gcc using the following arguments:
gcc source.c recTestLib.res -o "source"

Done!

The icon was added and in addition some information was inserted on the executable. Check them by selecting the executable and press with the right mouse button, click in properties and finally click on "Version" tab.



p.s:
1-) If you don't want to do all this job only create the resources file and add it in a Devian project.
2-) I won't explain the windres sintax int this post. To it does not get long. If you want to learn some more click on this link
3-) The resources file has many utilities and one of them is the easy way to add graphical components on a Windows Frame. When I learn more about them I want to publish here.