Character Strings as Resources


Resources and Languages

Programs written to modern specifications should not include strings embedded within the code. Many of the samples up until now have indeed included such strings within the source code. That was because it was simpler to do this than begin the book with a discussion of resource based programs. Once the topic of resources has been covered, and in particular string resources, these embedded strings are relegated to a separate file - the resource file. The text of the program may then be changed without touching the code. If all visual aspects of a program - including bitmaps, icons, menus, dialogs and strings are coded within a resource file, these entities may be modified without recoding or recompiling the C++ source - the resources may be modified, compiled and rebound to the existing executable.

String Resources

Character strings may be included in a resource script as shown below.

#define identity1  100
#define identity2  101

stringtable
{
 identity1, "this is a resource string"
 identity2, "this is also a resource string"
}

Only 1 string table may be declared within a resource script. Each string within the table may not exceed 255 characters in length. The C style control character \t (tab) is the only control sequence recognised by the resource compiler. However, strings may contain octal constants, several examples of which are shown below.

Description Octal
Tab \011
Line Feed \012
Carriage Return \015

Once defined, resource strings may be loaded as shown below.

character buffer[256];

load_string(module,
           identity,
           buffer,
           sizeof(buffer));

The parameter 'identity' is the identity that the string was given when being declared in the string table.

User Defined Resources

A user defined resource may be used to attach arbitrary data to an executable file (.exe). The data can be of any format - text or binary. A pointer to such data may be obtained within the application to which it is attached. The example of this section includes a poem that is attached as a user defined resource. In its resource file, a user defined resource is given as:

PoemIdentity PoePoem poem.txt

where PoePoem is defined in the header as:

#define PoePoem  256

Note that the new resource type 'PoePoem' is defined and associated with the file poem.txt - which contains the poem in text format. The resource name for the poem is 'PoemIdentity'. The resource type may be specified as a character string or as a numeric identity (as in this case). To avoid conflicting with system resources, when the resource type is numeric, its value should be greater than 255 (and less than 65,536).

Once a resource has been defined in this way, it may be loaded as shown below.

ResourceHandle = load_resource(module_handle, 
                              find_resource(module_handle,
                                           (const character*)PoemIdentity,
                                           (const character*)PoePoem));

character* Text = (character*)lock_resource(ResourceHandle);

Finding a resource followed by loading it is how functions like load_icon and load_cursor actually obtain the resource. When the resource is no longer required, it may be freed via a call to the function free_resource.

A snapshot of the poem application is shown below.