Creating and Using Resource Scripts

Resources are specified using a special, very simple text file called a resource script or .rc file. You write this file, specifying all the various resources you want to include in your program, and then compile it with a program called a resource complier (for Win32 versions of EGCS and gcc this program is called windres). The resource complier produces an object file containing the resource data objects in a format the linker can include in your programs. (With Microsoft and other compilers the resource compiler produces a special intermediate format called .res, which their linkers can understand but otherwise don't resemble object files).

For an introduction to windres, the resource compiler for the GNU compiler suite, and a simple example of how to use it, see the resource compiler page in the introduction to GNU programming tools.

Resource script files are passed through the C preprocessor so you can use preprocessor commands and C style comments /* ... */ (you can also use C++ style comments \\ ... but some preprocessors for other compilers do not automatically understand those). However, other than that resource scripts are very different from C code. Generally resource script statements are sensitive to where you put line breaks and that sort of thing, so try to follow the given syntax as closely as possible.

Each resource type is described on it's own page, which you can access through the links on the index page for this section.

Including Files for Definitions

Most of the time you should include at least windows.h in your resource file to define all the constants from the Win32 API. You should also include a header file of your own, resources.h for example, that defines all the constant IDs you will use for various resources (that way you can include the resources.h file in any source code that needs to access a resource and you only have to set the ID in one place, which reduces the chances of making a mistake or having mismatched ID numbers). For example this is a fragment of a resource file:

#include <windows.h>
#include "resources.h"

ID_MENU_MAIN MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "&New...", CMD_FILE_NEW
        MENUITEM "&Open...", CMD_FILE_OPEN
        MENUITEM SEPARATOR
        MENUITEM "E&xit", CMD_FILE_EXIT
    END
END

And here is the corresponding resources.h file:

#define ID_MENU_MAIN	100

#define CMD_FILE_NEW	1001
#define CMD_FILE_OPEN	1002
#define CMD_FILE_EXIT	1003

If you do include your own header files in resource scripts make sure to enclose everything that is not a C pre-processor statement in #ifndef RC_INVOKED ... #endif blocks. Resource compilers generally define RC_INVOKED during preprocessing so that you can keep them from reading, and being confused by, C or C++ statements in your header files. For example, you might have something like this in the resources.h header:

#ifndef RC_INVOKED
extern int foo;  /* The resource compiler would not understand this */
#endif

The Win32 headers and the standard C run-time headers should already be set up this way (but if they aren't now you know how to fix them, right!).