Introducing Resources

When we talk about resources in Win32 programming what we usually mean is a special kind of data used by the Win32 API to create pieces of the user interface. For example, to include a menu bar on the top of a window you are creating with the CreateWindow function you can include a menu handle (type HMENU) as the hMenu parameter (the ninth parameter in case you're interested). To get a menu handle you can call the LoadMenu function, like this for example:

hInstance = GetModuleInstance (NULL);
hWnd = CreateWindow (
           "WindowClassName", "Window Title", dwStyle,
           x, y, nWidth, nHeight, NULL, 
           LoadMenu (hInstance, "MenuName"),
           hInstance, 0);

In this example there must be a menu resource named "MenuName" linked into the program (you can load resources in other files by loading the file with LoadLibrary and using the instance handle returned by that call as hInstance for LoadMenu or some other resource loading function). Creating the resource data, making it part of your program, and making use of the resource data inside your program is what the rest of the pages in this section are about.

You can define resources for your program using a very simple language (a resource script), something like a programming language restricted to defining variables and structures. You can use graphics programs to draw bitmaps, icons and cursors, and then include them in your resource script by name. There are also programs for designing dialog boxes and menus interactively, and integrated resource editing packages that combine all these functions. Unfortunately most of these programs cost money and are not open source. I would be interested if anyone could point me to some good open source alternatives.

Resource IDs

Every resource in a resource script has an ID. A resource ID can be either a name enclosed in quotes (e.g. "MyResource") or a number. Generally I use numbers because they are slightly easier to manage and take less memory than text names. Usually you will use the C preprocessor to define constants for the IDs of all your resources and put them in a header file so that you can include the header in both your source code and the resource script.

The Win32 API functions that load resources all take two arguments, one is the instance handle, which I will discuss below, and the other is the resource ID. If you are using text names you could simply use a pointer to the name to load the resource:

    hIcon = LoadIcon (hInstance, "HappyFaceIcon");

But if you are using numbers you need to use the MAKEINTRESOURCE macro defined in windows.h to put the ID in a form that the resource loading functions will recognize:

    hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(ID_ICO_HAPPYFACE));

Instance Handles

The other piece of information needed for loading a resource is an instance handle. An instance handle is a number assigned by the Windows operating system to each executable file (.exe or .dll for example) that is loaded by an application. Normal programs consist of a single .exe file, along with several .dll files loaded in automatically. To access resources you have added to a program you need the instance handle of the .exe file. You can obtain this instance handle either by saving the handle that is passed as an argument to WinMain, or you can call GetModuleHandle like this:

    HINSTANCE hInstance = (HINSTANCE) GetModuleHandle (NULL);

You can also load other files and use their resources using LoadLibrary like this:

    HINSTANCE hInstanceFoo = LoadLibrary ("foo.dll");
    HICON hIcon = LoadIcon (hInstanceFoo, MAKEINTRESOURCE(ID_ICON_FOO));

That example loads the DLL foo.dll and then loads the icon identified by the ID constant ID_ICON_FOO from the DLL.