Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C++
Article

Icon extraction made easy

Rate me:
Please Sign up or sign in to vote.
3.44/5 (8 votes)
11 Apr 2005CPOL2 min read 68.7K   1.5K   24   17
Application icons of Win32 executable files can be extracted progamaticallly.

Sample Image - IconExtraction.jpg

Introduction

Some times you might want to extract the application icon of a Win32 executable file and save it to an icon file. If so, you are at the right place. I have been searching for an article that does this but I couldn't find the one I required. What the article is trying to do is extract the application icon in raw format from a Portable Executable (PE) file and save it to an icon file.

An icon in an executable file is stored in a different format than that in an icon file as you might expect. So it requires a special treatment.

Most of the code in this article is taken from the SDK example named Iconpro. But the Iconpro example lacks a callable interface. This article just did it.

Using the code

You can use the class named CIconExtractor in your projects. For that, just include the header file ExtractResource.h. The class contains one and only method i.e.:

DWORD ExtractIconFromExe(LPCSTR SourceEXE,
                         LPCSTR TargetICON,UINT IconIndex);

The parameter SourceEXE is the name of the executable file whose icon is to be extracted, the TargetICON is the name of the new icon file. The parameter IconIndex needs a bit of explanation. An executable file can contain more than one icon. The icon that appears first in the resource directory of the executable file will be taken as the application icon by the Explorer. To extract the application icon, give IconIndex a value 0. If you use a value greater than 0, make sure that the value is less than the constant MAX_ICONS. You can change the value of the constant MAX_ICONS to a desired one.

Usually, an executable file will have resources other than icons, such as bitmaps, strings, cursers etc. This code is looking only for icons.

That's all, enjoy it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Shooting for the Moon.

Comments and Discussions

 
QuestionHow use this code in Console App? Pin
mat.cpp11-May-13 1:06
mat.cpp11-May-13 1:06 
Hi dear,
I wanna use this code in console app and use your class in my app.
I make a main method like this:
C#
int main()
{
    CIconExtractor c1;
    c1.ExtractIconFromExe("Sourcepath.exe","Iconpath.ico",0);
    return 0;
}


when i run it,it's runned correctly but do not create icon.ico file in it's path.
what's my problem?what should i do to resolve it?
tnx.

modified 11-May-13 8:00am.

QuestionSize of icon? Pin
kiranin26-May-09 20:01
kiranin26-May-09 20:01 
GeneralReplace icon of EXE file Pin
babyleung12321-Feb-06 5:45
babyleung12321-Feb-06 5:45 
GeneralRe: Replace icon of EXE file Pin
Nisamudheen20-Mar-06 18:49
Nisamudheen20-Mar-06 18:49 
GeneralRe: Replace icon of EXE file Pin
Om Rudi1-Jun-07 7:03
Om Rudi1-Jun-07 7:03 
GeneralMe, again! :-P Pin
Lord Kixdemp3-Nov-05 10:27
Lord Kixdemp3-Nov-05 10:27 
GeneralRe: Me, again! :-P Pin
Nisamudheen11-Nov-05 22:04
Nisamudheen11-Nov-05 22:04 
GeneralHaving a little trouble... :-S Pin
Lord Kixdemp13-Oct-05 12:44
Lord Kixdemp13-Oct-05 12:44 
GeneralRe: Having a little trouble... :-S Pin
Nisamudheen13-Oct-05 22:26
Nisamudheen13-Oct-05 22:26 
GeneralRe: Having a little trouble... :-S Pin
Lord Kixdemp14-Oct-05 5:05
Lord Kixdemp14-Oct-05 5:05 
GeneralRe: Having a little trouble... :-S Pin
Nisamudheen14-Oct-05 18:44
Nisamudheen14-Oct-05 18:44 
General:) Pin
IBone©12-Apr-05 4:28
IBone©12-Apr-05 4:28 
GeneralRe: :) Pin
Nisamudheen12-Apr-05 18:01
Nisamudheen12-Apr-05 18:01 
QuestionDLLs ? Pin
Pazu12-Apr-05 1:19
Pazu12-Apr-05 1:19 
AnswerRe: DLLs ? Pin
Nisamudheen12-Apr-05 18:05
Nisamudheen12-Apr-05 18:05 
GeneralRe: DLLs ? Pin
Pazu13-Apr-05 6:42
Pazu13-Apr-05 6:42 
GeneralRe: DLLs ? Pin
Nisamudheen15-Apr-05 18:30
Nisamudheen15-Apr-05 18:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.