Click here to Skip to main content
Click here to Skip to main content
Go to top

Extract icons from EXE or DLL files

, 1 Sep 2008
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

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);

    ...
}

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

 
SuggestionFor WPF users, here is BitmapImage code to add to this project [modified] PinmemberAnarchi29-Oct-13 0:32 
BugAbruptly exits after loading some DLL files PinmemberDwedit_17-Dec-12 15:38 
QuestionJust a note PinmemberAleksei Karimov30-May-12 18:06 
GeneralGood PinmemberCarlos Ernesto Fuentes Rasique17-Dec-11 4:18 
GeneralMy vote of 5 Pinmemberjoehoper20-Nov-11 3:48 
AnswerThanks Pinmemberpahancs13-Oct-11 16:06 
QuestionIconExtractor Program PinmemberScott Caryer26-Jul-11 9:24 
AnswerRe: IconExtractor Program PinmemberAbbydonKrafts31-Jul-11 9:40 
QuestionWould not work with Windows 7 64 bit OS machine Pinmemberelderdo6-Jul-11 13:02 
AnswerRe: Would not work with Windows 7 64 bit OS machine PinmemberAbbydonKrafts31-Jul-11 9:35 
GeneralRe: Would not work with Windows 7 64 bit OS machine Pinmembersolopaul_201219-Dec-12 4:40 
Thanks a lot!
but i met the same problem!when load 64bit exe in 32bit process or 32bit exe in 64bit process,loadlibrary() failed,error 193.then loadlibraryex() called,after that GetModuleFileName() is called,failed with error number 126.
 
LOAD_LIBRARY_AS_DATAFILE
0x00000002
If this value is used, the system maps the file into the calling process's virtual address space as if it were a data file. Nothing is done to execute or prepare to execute the mapped file. Therefore, you cannot call functions like GetModuleFileName, GetModuleHandle or GetProcAddress with this DLL. Using this value causes writes to read-only memory to raise an access violation. Use this flag when you want to load a DLL only to extract messages or resources from it.
This value can be used with LOAD_LIBRARY_AS_IMAGE_RESOURCE. For more information, see Remarks.
 
so I think maybe we can find out the filepath in another way!or even though we can not get the file path,we can still get the icons.
AnswerRe: Would not work with Windows 7 64 bit OS machine PinmemberTsuda Kageyu19-Dec-12 4:48 
GeneralUnable to Convert 256x256 Icons from Microsoft Applications to Bitmap Pinmemberjohndotthinix1-Dec-10 4:34 
QuestionHow to determine if DLL or EXE has Icons? Pinmemberf r i s c h11-Feb-10 22:10 
GeneralThanx! PinmemberMuammar©12-Sep-08 22:09 
GeneralGood work but this way has already been taken... PinmemberGuillaume Leparmentier11-Jun-08 2:45 
GeneralRe: Good work but this way has already been taken... PinmemberTsuda Kageyu11-Jun-08 10:43 
GeneralNice PinmemberSaurabh.Garg10-Jun-08 5:18 
GeneralRe: Nice PinmemberTsuda Kageyu10-Jun-08 10:45 
GeneralNice Pinmemberholy_spirit10-Jun-08 4:12 
AnswerRe: Nice PinmemberTsuda Kageyu10-Jun-08 10:42 

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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 2 Sep 2008
Article Copyright 2008 by Tsuda Kageyu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid