What are the macros in resource.h used for?

Matt picture Matt · Jul 24, 2017 · Viewed 7.9k times · Source

When a resource file is created in visual studio, the IDE automatically generates a header file called resource.h with this text:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by BackupRestore.rc

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        101
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1001
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

As you can see it comments and says that they are "default values for new objects". What exactly does this mean? Are they referenced anywhere else by default and if not where would they be used? I am trying to create a project with the minimum amount of code and files and have tested removing the resource.h file and the solution seems to build fine without it so I am wondering whether or not it is essential or if removing it will cause future issues.

In summary: What is the resource.h file and its contents used for? Are the defined macros used elsewhere by default? When might a programmer reference them/use them in code, if at all? Are they essential and will removing them create future issues?

Thanks in advance- please be aware I am very novice in C++ and macros.

Answer

Cory Kramer picture Cory Kramer · Jul 24, 2017

From the documentation

_APS_NEXT_RESOURCE_VALUE is the next symbol value that will be used for a dialog resource, menu resource, and so on. The valid range for resource symbol values is 1 to 0x6FFF.

_APS_NEXT_COMMAND_VALUE is the next symbol value that will be used for a command identification. The valid range for command symbol values is 0x8000 to 0xDFFF.

_APS_NEXT_CONTROL_VALUE is the next symbol value that will be used for a dialog control. The valid range for dialog control symbol values is 8 to 0xDFFF.

_APS_NEXT_SYMED_VALUE is the next symbol value that will be issued when you manually assign a symbol value using the New command in the Symbol Browser.

So essentially if you are in the actual dialog editor, when you click a new button down (for example) that's how it keeps track of the next available resource ID. The resource IDs in general are to keep track of things like static text (e.g. for field labels), bindings, etc.

If you had defined a resource ID it would have to be a smaller value the _APS_NEXT whatever. For example in your resource.h you may have

#define IDC_SOME_RADIO_BUTTON    1056

Then later you'd have to update

#define _APS_NEXT_CONTROL_VALUE       1057

Again this is so the next time you click down a button it gets a unique ID. They must be unique because they are just pre-processor macros that will be substituted when you try to use that resource ID for something. For example

void HandleRadioButtion()
{
    // do something important
}

Then you can use your resource ID to bind it to a function

BEGIN_MESSAGE_MAP(SomeDlg, CDialog)
    ON_BN_CLICKED(IDC_SOME_RADIO_BUTTON, HandleRadioButton)
END_MESSAGE_MAP()