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

Extract icons from EXE or DLL files

By , 1 Sep 2008
 

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)

About the Author

Tsuda Kageyu
Software Developer
Japan Japan
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
BugAbruptly exits after loading some DLL filesmemberDwedit_17 Dec '12 - 15:38 
I'm seeing the program abruptly exit when it loads certain DLL files, such as "cygicons-0.dll" from Cygwin. It's not getting past the first call to LoadLibrary. No exceptions are thrown, it just immediately exits.
QuestionJust a notememberAleksei Karimov30 May '12 - 18:06 
After I gave a try to the code "Module could not be loaded" exception was throwed. After small examination found that on MSDN:
 
The GetModuleFileName function does not retrieve the path for modules that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag.
 
I think that requires a fixture.
Alex KraS

GeneralGoodmemberCarlos Ernesto Fuentes Rasique17 Dec '11 - 4:18 
Good project!Thumbs Up | :thumbsup:
GeneralMy vote of 5memberjoehoper20 Nov '11 - 3:48 
Great article code, thanks.
AnswerThanksmemberpahancs13 Oct '11 - 16:06 
Many thanks Thumbs Up | :thumbsup: !!!
QuestionIconExtractor ProgrammemberScott Caryer26 Jul '11 - 9:24 
How do you start or run this Icon Extractor program that I just downloaded?
AnswerRe: IconExtractor ProgrammemberAbbydonKrafts31 Jul '11 - 9:40 
It's not a program. It's a demo project written in C#. It requires Microsoft Visual Studio[^]or SharpDevelop[^] and the .NET 3.0 Framework[^] to compile.
"I think it's a trollophage and it's the beginning of a viral outbreak." - PerdidoPunk

QuestionWould not work with Windows 7 64 bit OS machinememberelderdo6 Jul '11 - 13:02 
I tried your application on my Windows 7 64 bit OS machine. I keep getting an error from the IconExtractor constructor's invocation of GetModuleFileName:
 
"The specified module could not be found"
 
I'm not sure, but I'm guessing that this error may be caused by the 64 bit OS. It is a great utility. Perhaps someone knows of a work-around for 64 bit OS machines.
AnswerRe: Would not work with Windows 7 64 bit OS machinememberAbbydonKrafts31 Jul '11 - 9:35 
I also have Win7 64-bit.
 
Most likely it's a permission issue. When I run various third-party system utilities, such as those from Sysinternals, I get that error when trying to view the details. This goes for any "protected" path, such as Program Files or any Windows folder. If I run a utility as an Administrator, it works fine.
"I think it's a trollophage and it's the beginning of a viral outbreak." - PerdidoPunk

GeneralRe: Would not work with Windows 7 64 bit OS machinemembersolopaul_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.

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 2 Sep 2008
Article Copyright 2008 by Tsuda Kageyu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid