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;
CString m_Name;
CString m_Type;
CString m_ReadibleName;
__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;};
__ResInfo& operator= (const __ResInfo& info)
};
class CRes
{
public:
CRes();
virtual ~CRes();
public:
LPSTR LockRes();
HRSRC GetLoadedRes();
BOOL LoadAllResource();
static BOOL EnumNames( HANDLE hModule, LPCTSTR lpType,
LPTSTR lpName, LONG lParam);
static BOOL EnumTypesFunc( HANDLE hModule, LPSTR lpType, LONG lParam);
BOOL LoadResource();
BOOL FindResource( int res,LPSTR type);
void FreeIt();
BOOL LoadExe();
CString m_Path;
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.