modula-2 home

  Win32 API  


Using Resources

Before we get any deeper I will cover the topic of resources so that I won't have to re-write it for each section. You don't actually need to compile the stuff in this section, it's as example only.

Resources are pre-defined bits of data stored in binary format inside your executable file. You can create resources manually in a resources script, a file with an extension of ".rc". Commercial development environments, such as Stony Brook M2, allow you to create resources by means of a visual Resource Editor which does not require manual editing of a resource script. The resource editor creates files with extension ".res", which are compiled resource files. All resource files we use in this tutorial are included in the download.

We will not go into detail on how to work with the Resource File editor, but it is important to mention that to every object (menus, menu items, icons, etc.) in the resource file we can assign a unique ID, which we will use in our program to identify the object.

Let's have a look at the resource we will use in the next chapter of this tutorial.

This resource file contains an icon and a menu.

As you can see IDI_MYICON is the identifier of the icon, and IDR_MYMENU is the identifier of the menu.

We've assigned IDI_MYICON the value of 101. We could just forget about the identifier and use 101 wherever we need to reference the icon, but IDI_MYICON is a lot clearer as to what you are refering too, and easier to remember when you have large number of resources.

For this purpose, the identifiers are defined in a Modula-2 definition file named resource. The command "Set Symbol file" in the resource editor is used to link this file to the resource file so the resource editor (who internally uses numbers to identify the objects) can display our identifiers.


    IDR_MYMENU                  =    101;
    IDI_MYICON                  =    102;
    ID_FILE_EXIT                =    40001;
    ID_STUFF_GO                 =    40002;

END resource.

Now lets look at the menu resource:

As you can see we have assigned an identifier ID_FILE_EXIT to the command Exit, as we have done for all commands in this menu.

Notes on using a resource file in our program

Now an example of how to use a resource in our program. The problem at hand is how to load the icon from the resource and how to refer to it in our program.

The WINUSER.DEF file exports the following procedure:

                   lpIconName : ARRAY OF ACHAR) : HICON;

The first parameter of LoadIcon() and many other resource using functions is the handle to the current instance. The second is the identifier of the resource, which as you can see is the name of the resource. Problem: we have identified our icon with a number (an integer), not with a name (string).

The solution is to use the corresponding procedure exported from the WINX module:


It's a good idea to browse through the WINX module. This module provides strongly typed versions of some of the more loosely typed API calls. WINX also contains some strongly typed constants, and some NIL variables for Modula-2 modified parameters such as RECORD and ARRAY OF parameters. These NIL variables exist because Windows often provides optional behaviors on API functions when a NIL pointer is passed. In Modula-2, since these have been mapped to structures and arrays, a clean way was needed to access these optional behaviors. For example, if you wanted to call an API that required a NIL string be passed you would call the function as:

    Api name(WINX.NIL_STR);


Copyright © 1998-2011, Brook Miles. All rights reserved. Adapted for Modula-2 by Frank Schoonjans, with permission. Description of WINX module from Stony Brook documentation.