Using RC.EXE

A brief overview of using the Resource Compiler (RC.EXE) to build resources

Resources

This article provides a short look at how to use the Resource Compiler (RC.EXE) to create resource files (.RES) for Visual Basic applications. Using RC.EXE directly is often better than using the wizards provided with VB5 and 6 as you have more control over including arbitrary data; in addition the wizard versions do not encode icons correctly.

About the Resource Compiler

The Resource Compiler compiles resource definition files (.RC files) and the actual resource to be included (binary files such as icon, bitmap, and cursor files) into a binary resource (.RES) file. This .RES file can then be included in any VB project, and once the VB project is compiled, the resources are accessible to Win32 API functions as well as the standard VB LoadResString, LoadResPicture and LoadResData functions.

The Resource Compiler itself consists of two files:

  • RC.EXE
  • RCDLL.DLL

These files come in the \Tools directory of Enterprise Editions of VB; if you don't have that then you can download the files by looking for the VB5 Resource Add-In at MSDN

Resources can be divided into two groups:

  1. String resources (text strings such as "vbAccelerator")
  2. Binary resources (icons, bitmaps, cursors, sounds, video, and so forth)

1. String Resources

String resources are stored in a string table in the resource definition file.

Syntax

STRINGTABLE [load-option] [mem-option]
BEGIN
	stringID string
	.
    	.
    	.
END

The STRINGTABLE statement defines one or more string resources for an application. String resources are simply null-terminated ASCII strings that can be loaded when needed from the executable file, using the LoadResString function.

Parameters

  1. load-option.

    Specifies when the resource is to be loaded. This optional parameter must be one of the following options:

    • PRELOAD
      Resource is loaded immediately.
    • LOADONCALL
      (Default) Resource is loaded when called.
  2. mem-option

    Specifies whether the resource is fixed or can be moved and whether or not can be discarded. This optional parameter can be one of the following options:

    • FIXED
      Resource remains at a fixed memory location.
    • MOVEABLE
      Resource can be moved if necessary in order to compact memory.
    • DISCARDABLE
      Resource can be discarded if no longer needed.
  3. stringID.

    Specifies an integer value that identifies the resource.

  4. string.

    Specifies one or more ASCII strings, enclosed in double quotation marks. The string must be no longer than 255 characters and must occupy a single line in the source file. Grouping strings in separate segments allows all related strings to be read once in a single reading and discarded together. When possible, you should be able to move and discard the table. The Resource Compiler allocates 16 strings per segment and uses the identifier value to determine which segment will contain the string. Strings with the same upper-12 bits in their identifiers are placed in the same segment.

Example

The following example demonstrates the STRINGTABLE statement:

#define IDS_HELLO    1
#define IDS_GOODBYE  2

STRINGTABLE
BEGIN
    IDS_HELLO,   "Hello"
    IDS_GOODBYE, "Goodbye"
END 

2. Binary Resources

Binary resources are not stored in the resource definition file. The resource definition file includes only a pointer to the files containing the binary resources, for example, icon (.ICO), bitmap (.BMP), cursor (.CUR), sound (.WAV), and video (.AVI) files.

This pointer is called a Single-Line Statement in the resource definition file.

Syntax

nameID keyword [load-option] [mem-option] filename

Parameters

  1. nameID

    Specifies either a name or an integer value identifying the resource. This ID has to be unique for every category specified by the keyword. In the category ICON the ID 0 is reserved for the Visual Basic icon. Therefore you'll have to start ID for ICONS at 1.

  2. keyword

    Specifies the type of file. This can either be one of the following standard options or your own customised type:

    • BITMAP
      Defines a bitmap (.BMP)
    • CURSOR
      Defines a cursor (.CUR)
    • ICON
      Defines an icon (.ICO)
    • SOUND
      Defines a wave file (.WAV)
    • VIDEO
      Defines a video file (.AVI)
  3. load-option

    Specifies when the resource is to be loaded. The parameter must be one of the following options:

    • PRELOAD
      Resource is loaded immediately.
    • LOADONCALL
      (Default) Resource is loaded when called.
  4. mem-option

    Specifies whether the resource is fixed or can be moved and whether it can be discarded. The parameter must be one of the following options:

    • FIXED
      Resource remains at a fixed memory location.
    • MOVEABLE
      Resource can be moved if necessary in order to compact memory.
    • DISCARDABLE
      Resource can be discarded if no longer needed.

    The default for binary resources is MOVEABLE.

  5. filename

    Specifies the name of the file that contains the resource. The name must be a valid filename; it must be a full path if the file is not in the current working directory. The path can be either a quoted or non-quoted string.

Example

The following example specifies two bitmap resources:

disk1   BITMAP disk.bmp
12      BITMAP PRELOAD diskette.bmp

RC.EXE Options

To start the Resource Compiler, use the rc command.

Syntax

rc /r [options] definition-file

Parameters

  • /r

    This parameter specifies that the .RC file will only be compiled, not linked to any executable (required for use with VB)

  • options

    You can use the following options with the rc command:

    • /?
      Displays a list of rc command-line options.
    • /fo newname
      Uses newname for the name of the .RES file.
  • definition-file

    The definition-file parameter specifies the name of the resource definition file (.RC) that contains the names, types, filenames, and descriptions of the resources to be compiled.

Example

RC /r /fo TEST32.RES TEST.RC

Note if the .RES file you re-create is part of a Visual Basic project, you will not be able to create it if the Visual Basic project is open.

Conclusion

This article gives a brief overview of using RC.EXE to create a Resource file for use with Visual Basic. You can see this technique used in many examples on the site; for some specific ones try these articles: