Windows Programming/Resource Scripts
The Windows platform SDK includes a special utility called rc.exe, the Resource Script Compiler. Resource scripts are a powerful part of Windows programming that any good programmer should know how to utilize effectively.
For more information about specific resources, see the Resource Script Reference.
What is a Resource Script
The resource compiler compiles a special type of file known as a Resource Script. Resource scripts contain GUI data, and, when compiled, can be linked into a program. The program then can access the data contained in the resource script. Also interesting to note is that the Windows operating system accesses a program's resources for various purposes. For instance, right-click on a program and click "Properties". If available, click on the "Version" tab. This tab will contain a number of different text strings that describe the program. These text strings are all included from a resource script.
Also, people are all familiar with the fact that usually each program has a distinct icon. If you want your program executable to use a special icon you must include it in a resource script.
There are many different types of resources that can be stored in a program via a resource script. Resource scripts are not the only way to store this information in a program, but they are much easier to write than hard C or VB code to store and access them.
Types of resources
Here is a list of common resources:
- Drop-down Menus
- Popup Menus
- Text Strings
- Keyboard Accelerators (keypress combinations, such as [Ctrl]+[C] to copy text)
- Bitmap Images
- Dialog Boxes
- Version information
- Mouse Cursors
Making a Resource Script
The syntax for writing resource scripts is similar to C. For instance, the resource script compiler uses the standard C preprocessor. This is important because you must include the header file <afxres.h> to make a resource script. Also, most resource scripts contain macro values, so programmers will frequently store the related macro definitions in a header file "resource.h". This way, the same macros can be included and used in the main program files.
We will talk more about resources in the following chapters; there's also an appendix where all common resource types are listed including information on how to use them. This appendix is located at Windows Programming/Resource Script Reference.
Using a Resource
Once a resource is stored in your executable there are various methods to access it. These methods differ depending on what type of resource you are trying to access. For instance if you want to access a string resource, you would need to use the LoadString function; correspondingly the LoadIcon function is needed to access an icon.
To access a resource you must have the instance handle of the executable file that contains the resource. This means, that if you obtain an instance handle to another executable (or DLL) file, you can access the resources remotely! Occasionally, programmers will create DLL files that contain nothing but resources for use in other programs.
The MAKEINTRESOURCE keyword that we will occasionally see when dealing with resources is an important keyword, and a program will crash (or not even compile) if it is not used correctly so taking a minute to understand it is well worth the effort.
Resources are all stored with a name which can be a string or a numerical identifier. If it is numerical, the number must be no larger than an unsigned 16-bit integer (65535, max). Resources are all called by name, that is the system is expecting an unicode string with the name of the resource. However if we use a numerical identifier we need to inform the system that we are doing so, so that it doesn't get confused and try to treat your integer as a string. For that we pass the MAKEINTRESOURCE macro to make. The macro takes a single argument - the numerical identifier - and it returns a string pointer suitable for use in the system. We will demonstrate this later.