Click here to Skip to main content
Click here to Skip to main content

Extract icons from EXE or DLL files

, 29 Oct 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Extract all the variations of an icon, including the ones ExtractIconEx() can't extract.

IconExtractorApp

 

Next version coming soon!
Recently I found that the sample code of this article is downloaded frequently. Honestly, I am a bit ashamed. The article discuss almost nothing, the code is dirty, buggy, inefficient, blah blah blah... So I decided to rewrite the overall article and the sample code. Though the article is not yet completed, the sample code is available at my GitHub repository. If you have an interest in this article, feel free to check it out. I added a brief instruction at "Using the code" section. Thanks!

Introduction

The class I introduce in this article is for extracting an icon from an EXE or DLL file. Though it's common to use the ExtractIconEx() Win32 API function for this purpose, ExtractIconEx() can't extract all the variations of an icon.

I had faced the problem, and made a class 'IconExtractor' as the solution to the problem.

Using the code

To use this class, instantiate it first with the filename from which you want to extract the icons. You should dispose it after use.

Then, call the GetIcon() method with the index of the icon you want, to extract the specified icon. The index is same as the nIconIndex for ExtractIconEx(). This method returns a System.Drawing.Icon object containing all the variations in size and color depth. You can use it for Form.Icon, NotifyIcon.Icon, etc. The proper variation will be selected by the system.

Use the SplitIcon() static method if you want to split the variations into individual instances. This method returns an array of icons, each element containing one of the variations.

using TKageyu.Utils;

...

using (IconExtractor ie = new IconExtractor(@"D:\sample.exe")) 
{
    Icon icon0 = ie.GetIcon(0);
    Icon icon1 = ie.GetIcon(1);

    ...
    
    Icon[] splitIcons = IconExtractor.SplitIcon(icon0);

    ...
}

Here, let me explain about the next version of IconExtractor. The significant updates are:

  • Can load 64-bit DLL/EXE from 32-bit process, and vice versa
  • More memory efficient
  • Well-known and permissive 2-clause BSD license
  • No longer implements IDisposable
  • Some utility methods moved to IconUtil class

The usage is like this:

using TsudaKageyu;

...

IconExtractor ie = new IconExtractor(@"D:\sample.exe");
Icon icon0 = ie.GetIcon(0);
Icon icon1 = ie.GetIcon(1);

...

Icon[] splitIcons = IconUtil.SplitIcon(icon0);

...

How it works

The icons and other materials are embedded in the executable file in binary form. Those pieces of binary data are called 'resources'. They can't be read directly with .NET classes, because they are different from the managed resources that the .NET Framework can handle. They should be read with Win32 API functions such as FindResource(), LoadResource() etc.

This class reads binary data by using the functions in the GetResourceData() private method. The parameters given to the method are collected when the class is instantiated. The binary data is in similar form to an .ico file, and can be converted easily to be equivalent to an .ico file. It is given to the icon constructor after conversion, and will be parsed as read from an .ico file. The conversion is done in the CreateIcon() method.

License

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

Share

About the Author

Tsuda Kageyu
Software Developer
Japan Japan
In 1985, I got my first computer Casio MX-10, the cheapest one of the MSX computers. Then I began programming in BASIC and assembly language, and have experienced over ten languages from that time on.
Now, my primary languages are C++ and C#. Working for a small company in my home town, the countryside of Japan.
 
I'm an official member of WTL Team and in charge of the maintenance of the NuGet package.

Comments and Discussions

 
QuestionJust a note PinmemberAleksei Karimov30-May-12 18:06 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 29 Oct 2014
Article Copyright 2008 by Tsuda Kageyu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid