17

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.

Matt
  • 482
  • 1
  • 6
  • 15
  • 1
    Resource.h contains the IDs of resources defined in your project's .rc file. It allows your windows app to reference its various resources: dialog layouts, icons, bitmaps and localized text strings. – Michaël Roy Jul 24 '17 at 15:44
  • So what happens if it is then removed? Any lasting impact? None of the defined macros (the ones starting _APS) are mentioned in my .rc file – Matt Jul 24 '17 at 15:46
  • 1
    @Matt They are used (read and written to) by the RC editor - mess with them and break your project as the RC editor will not know the next value to use when creating resources. – Richard Critten Jul 24 '17 at 15:51
  • 2
    Resource.h is a vital component of your source file set. DO NOT delete! Removing the _APS_* macros will affect the visual studio resource manager, which includes the dialog editor, the text resource editor, etc... It will make your life miserable if you need to update the looks of your app. – Michaël Roy Jul 24 '17 at 15:52
  • My resource file is merely to give the version of a DLL when its properties are looked at. Do your earlier comments about "DO NOT DELETE" still stand? – Matt Jul 24 '17 at 15:57

2 Answers2

15

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()
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 3
    This link is very helpful because typing those into google gave me virtually nothing. What about the other questions? – Matt Jul 24 '17 at 15:39
  • Your question is a bit broad, so it's hard to know specifically what you're asking. I tried to expand my answer to illustrate an example of how you'd use resources in general. If your current question is answered and you have a follow-up question, feel free to create a new post. Stack Overflow is better suited to single-question single-answer style posts. – Cory Kramer Jul 24 '17 at 15:53
  • I am close to marking this as an answer but my main questions is really about are they essential or not for a resource file to give the version of a DLL? – Matt Jul 24 '17 at 15:58
  • They are very important, but not to versioning, per se. As mentioned they are important to make sure your `.rc` files are consistent and correct. As you add new resources such as text strings, buttons, etc the `resource.h` file helps make sure all the resource IDs are unique. – Cory Kramer Jul 24 '17 at 15:59
  • So if I was not to add anymore, it would make sense to remove it? – Matt Jul 24 '17 at 16:01
  • 2
    @Matt conciser these 2 points: (1) They are just #define(s) and not used in your code so nothing ends up in your project. (2) They are vital to the correct functioning of the Resource Editor and if you delete them you will most likely corrupt your project. Given (1) & (2) why mess with them. – Richard Critten Jul 24 '17 at 16:42
5

Those macros are just for the IDE. The code itself doesn't reference them anywhere.

To create minimal resource files, you can skip using the IDE to create your resources and instead write them from scratch (as plain text files). Indeed, this is how it was done before the IDEs enabled all this automation. The resource file format is documented in MSDN.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175