Click here to Skip to main content
13,346,397 members (63,759 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 24 Oct 2002

Reading and Using Resources in an Executable file

, 24 Oct 2002
Rate this:
Please Sign up or sign in to vote.
A class for dynamic reading of resouce information from an executable file.


I encountered a very interesting requirement where I have to read out the resources from  executable files and display the dialog using a dialog template in that execute file. I searched on the web , but I could not find a full implementation of such a requirement. I read the MSDN for serveral days and wrote several classes from scratch. The classes has been used in my projects. So the classes are well tested. I wrote my project in a codepage different from English. I do not provide an example in English. But the souce code is here.

About resources in Windows

The resource is compiled to *.res files while we are writing a program. After the executable is generated, the resource is a part of executable files. Fortunately , Windows has provided serveral API to read resource from executable file. These functions are divided into several groups . One group is to look at all resources in the files. We call them enumeration functions.

  • EnumResourceTypes
  • EnumResourceNames
  • EnumResourceLanguages

Once all resources have been enumerated from files, we can change the resource according to our request. In order to change the resource compiled in the file we shall follow the following rulues.

  • Use the LoadLibrary function to load the executable file Hand.exe.
  • Use the FindResource and LoadResource functions to locate and load the dialog box resource.
  • Use the LockResource function to retrieve a pointer to the dialog box resource data.
  • Use the BeginUpdateResource function to open an update handle to Foot.exe.
  • Use the UpdateResource function to copy the dialog box resource from Hand.exe to Foot.exe.
  • Use the EndUpdateResource function to complete the update.

There is a simple sample code for us in MSDN to show the steps.

According to the previous discussion , we can develop a class to read all resource information from executable files. That is - CRes anc _ResInfo. The later class is a helper class for CRes. It contain the basic inforation of a resource ,such as resource name , resource types .

struct __ResInfo
    int Type_Type;                   //0 = NUMBER,other string
    CString m_Name;                  // The name of resource
    CString    m_Type;              // type of resource
    CString m_ReadibleName;        // The readible name for user 
// Construction for resource information
    __ResInfo(const CString &name, const CString& type):
    __ResInfo(const __ResInfo &info){*this = info;};
// assignment
    __ResInfo& operator= (const __ResInfo& info)
// Class for reading ,listing reasource
class CRes  
// construction and destruction
    virtual ~CRes();
// Lock the resource for furthre usage. It return all data
// of specified resource.
    LPSTR LockRes();
// Get the loaded resource’s handle 
    HRSRC GetLoadedRes();
// Load all resource’s list from executable files.
    BOOL LoadAllResource();
// static function for name in the current executable file.
    static BOOL EnumNames( HANDLE hModule, LPCTSTR lpType, 
             LPTSTR lpName, LONG lParam);
// static function to enumerate all types in the current executable file.
    static BOOL EnumTypesFunc( HANDLE hModule, LPSTR lpType, LONG lParam);
// Load  currenct resource
    BOOL LoadResource();
// Find the given specified resource from executable files
    BOOL FindResource( int res,LPSTR type);
// Free the executable files locked while reading resource from it .
    void FreeIt();
// Load the executable files, return TRUE if is successfully
// reading the executable file
    BOOL LoadExe();
// The path to store the executable file
    CString m_Path;
// List of resource in the current executable file
    CArray<__ResInfo,__ResInfo&> m_Infos;


The following code section shall show you how to use this class. Read all resource from executable files.

m_Res.m_Path = name;
m_Res.LoadExe ();
m_Res.LoadAllResource ();

int n = m_Res.m_Infos .GetSize ();
for ( int i =0 ;i< n ;i++)

The following code section is to show how to read lock the resource for further usage.

if (!m_Res.FindResource (atoi(strRes),RT_DIALOG))
        return ;
    if (!m_Res.LoadResource ())
        return ;

I think this class is not well constructed for lack of time. The updated version will be coming soon. Welcome your ideas.


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


About the Author

Johnson Zhou
Software Developer (Senior)
China China
I'm write program from 1990. My research field is CAG,CAD and Image processing. I select C/C++, ASP, Java, XML as my usaully developing tools. Occasional , write code in Delphi and VB. I'm using Visual C++ from 1996. If you have anything unclear, e-mail to Software Engineering and CAD is my mainly research program.

You also can reach me on msn:

You may also be interested in...


Comments and Discussions

GeneralReading 64 bit files from a 32 bit application (and vice versa) Pin
oren.shnitzer25-May-08 23:00
memberoren.shnitzer25-May-08 23:00 
GeneralLink Pin
Hammerfall24-Feb-05 6:22
memberHammerfall24-Feb-05 6:22 
GeneralExecute exe files Pin
adrianuswiedewanus30-Nov-04 9:35
memberadrianuswiedewanus30-Nov-04 9:35 
GeneralRe: Execute exe files Pin
haha6242-Dec-05 19:55
memberhaha6242-Dec-05 19:55 
Generala question Pin
yugalee6-Jun-04 18:52
memberyugalee6-Jun-04 18:52 
Generalreadable name Pin
awinter9-May-03 9:39
memberawinter9-May-03 9:39 
GeneralThanks!!! Pin
Paul Belikian2-Nov-02 20:29
memberPaul Belikian2-Nov-02 20:29 
GeneralNifty - Thanks Pin
Codin' Carlos25-Oct-02 3:37
memberCodin' Carlos25-Oct-02 3:37 
GeneralRe: Nifty - Thanks Pin
Uwe Keim26-Oct-02 4:43
sitebuilderUwe Keim26-Oct-02 4:43 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 25 Oct 2002
Article Copyright 2002 by Johnson Zhou
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid