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

Reading and Using Resources in an Executable file

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

Introduction

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):
            m_Name(name),m_Type(type),Type_Type(0){};
    __ResInfo():m_Type(_T("")),Type_Type(0){};
    __ResInfo(const __ResInfo &info){*this = info;};
// assignment
    __ResInfo& operator= (const __ResInfo& info)
};
// Class for reading ,listing reasource
class CRes  
{
public:
// construction and destruction
    CRes();
    virtual ~CRes();
public:
// 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;
};

Usage

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.

License

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

Share

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 :zhou_cn123@sina.com Software Engineering and CAD is my mainly research program.
 
You also can reach me on msn: zhoujohnson@hotmail.com

Comments and Discussions

 
GeneralLink PinmemberHammerfall24-Feb-05 5:22 

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
Web01 | 2.8.141022.2 | Last Updated 25 Oct 2002
Article Copyright 2002 by Johnson Zhou
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid