Home   Index   About
Ultimate Pack


Custom Search
Creating a Resource List

The following example creates a list of every resource in the HAND.EXE file. The list is written to the RESINFO.TXT file.

The code demonstrates how to load the executable file, create a file in which to write resource information, and call the EnumResourceTypes function to send each resource type found in the module to the application-defined callback function EnumTypesFunc. See EnumResTypeProc for information on callback functions of this type. This callback function uses the EnumResourceNames function to pass the name of every resource within the specified type to another application-defined callback function, EnumNamesFunc. See EnumResNameProc for information on callback functions of this type. EnumNamesFunc uses the EnumResourceLanguages function to pass the language of every resource of the specified type and name to a third callback function, EnumLangsFunc. See EnumResLangProc for information on callback functions of this type. EnumLangsFunc writes information about the resource of the specified type, name, and language to the RESINFO.TXT file.

char szBuffer[80]; // print buffer for EnumResourceTypes

DWORD cbWritten; // number of bytes written to res. info. file

int cbString; // length of string in sprintf

// Declare callback functions.

BOOL EnumTypesFunc(

HANDLE hModule,

LPTSTR lpType,

LONG lParam);

BOOL EnumNamesFunc(

HANDLE hModule,

LPCTSTR lpType,

LPTSTR lpName,

LONG lParam);

BOOL EnumLangsFunc(

HANDLE hModule,

LPCTSTR lpType,

LPCTSTR lpName,

WORD wLang,

LONG lParam);

// Load the .EXE whose resources you want to list.

hExe = LoadLibrary("hand.exe");

if (hExe == NULL) {

ErrorHandler("Could not load .EXE.");

}

// Create a file to contain the resource info.

hFile = CreateFile("resinfo.txt", // name of file

GENERIC_READ | GENERIC_WRITE, // access mode

0, // share mode

(LPSECURITY_ATTRIBUTES) NULL, // no security

CREATE_ALWAYS, // create flags

FILE_ATTRIBUTE_NORMAL, // file attributes

(HANDLE) NULL); // no template

if (hFile == INVALID_HANDLE_VALUE) {

ErrorHandler("Could not open file.");

}

// Find all of the loaded file's resources.

cbString = sprintf(szBuffer,

"The file contains the following resources:\n\n");

WriteFile(hFile, // file to hold resource info.

szBuffer, // what to write to the file

(DWORD) cbString, // number of bytes in szBuffer

&cbWritten, // number of bytes written

NULL); // no overlapped I/O

EnumResourceTypes(hExe, // module handle

(ENUMRESTYPEPROC)EnumTypesFunc, // callback function

0); // extra parameter

// Unload the executable file whose resources were

// enumerated and close the file created to contain

// the resource information.

FreeLibrary(hExe);

CloseHandle(hFile);

// FUNCTION: EnumTypesFunc(HANDLE, LPSTR, LONG)

//

// PURPOSE: Resource type callback

BOOL EnumTypesFunc(

HANDLE hModule, // module handle

LPTSTR lpType, // address of resource type

LONG lParam) // extra parameter, could be

// used for error checking

{

int cbString;

// Write the resource type to a resource information file.

// The type may be a string or an unsigned decimal

// integer, so test before printing.

if ((ULONG)lpType & 0xFFFF0000) {

cbString = sprintf(szBuffer, "Type: %s\n", lpType);

}

else {

cbString = sprintf(szBuffer, "Type: %u\n", (USHORT)lpType);

}

WriteFile(hFile, szBuffer, (DWORD) cbString,

&cbWritten, NULL);

// Find the names of all resources of type lpType.

EnumResourceNames(hModule,

lpType,

(ENUMRESNAMEPROC)EnumNamesFunc,

0);

return TRUE;

}

// FUNCTION: EnumNamesFunc(HANDLE, LPSTR, LPSTR, LONG)

//

// PURPOSE: Resource name callback

BOOL EnumNamesFunc(

HANDLE hModule, // module handle

LPCTSTR lpType, // address of resource type

LPTSTR lpName, // address of resource name

LONG lParam) // extra parameter, could be

// used for error checking

{

int cbString;

// Write the resource name to a resource information file.

// The name may be a string or an unsigned decimal

// integer, so test before printing.

if ((ULONG)lpName & 0xFFFF0000) {

cbString = sprintf(szBuffer, "\tName: %s\n", lpName);

}

else {

cbString = sprintf(szBuffer, "\tName: %u\n",

(USHORT)lpName);

}

WriteFile(hFile, szBuffer, (DWORD) cbString,

&cbWritten, NULL);

// Find the languages of all resources of type

// lpType and name lpName.

EnumResourceLanguages(hModule,

lpType,

lpName,

(ENUMRESLANGPROC)EnumLangsFunc,

0);

return TRUE;

}

// FUNCTION: EnumLangsFunc(HANDLE, LPSTR, LPSTR, WORD, LONG)

//

// PURPOSE: Resource language callback

BOOL EnumLangsFunc(

HANDLE hModule, // module handle

LPCTSTR lpType, // address of resource type

LPCTSTR lpName, // address of resource name

WORD wLang, // resource language

LONG lParam) // extra parameter, could be

used for error checking

{

HANDLE hResInfo;

char szBuffer[80];

int cbString = 0;

hResInfo = FindResourceEx(hModule, lpType, lpName, wLang);

// Write the resource language to the resource information file.

cbString = sprintf(szBuffer, "\t\tLanguage: %u\n",

(USHORT)wLang);

WriteFile(hFile, szBuffer, (DWORD) cbString,

&cbWritten, NULL);

// Write the resource handle and size to buffer.

cbString = sprintf(szBuffer,

"\t\thResInfo == %lx, Size == %lu\n\n",

hResInfo,

SizeofResource(hModule, hResInfo));

WriteFile(hFile, szBuffer, (DWORD) cbString,

&cbWritten, NULL);

return TRUE;

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun