RC2104 error on #ifdef

D

David W

Hello,

In a C++ MFC application I need to conditionally #include one of two additional
resource files in my main resource file because different forms of the
application have different names. I would also like to edit the main resource
file with the IDE's resource editor and save it without losing the conditional
#includes of the name-specific resource files. So, the main resource file
includes the following:

3 TEXTINCLUDE
BEGIN
"#include ""apptype.h""
"#ifdef APP_A" // <---- error
"#include ""app_a.rc""
"#else"
"#include ""app_b.rc""
"#endif"
// the other #includes
"\0"
END

However, RC is producing an RC2104 error on the marked line with the message
"Undefined keyword or key name: #ifdef". Since RC normally understands #ifdef I
don't understand why it complains about one in a TEXTINCLUDE. Can someone
explain it?

On the same subject, this is an MDI application with one menu for each of many
different document types. I have had to put _all_ menus in both
app-name-specific resource files, and it's only because of the "About..." item
on the Help menu (since, by convention, this item includes the application name:
"About AppA..." or "About AppB..."). This means that any menu changes must be
done in both resource files. Is there an easier way to do this?

I am using VS .NET 2003.

David
 
D

David Wilkinson

David said:
Hello,

In a C++ MFC application I need to conditionally #include one of two additional
resource files in my main resource file because different forms of the
application have different names. I would also like to edit the main resource
file with the IDE's resource editor and save it without losing the conditional
#includes of the name-specific resource files. So, the main resource file
includes the following:

3 TEXTINCLUDE
BEGIN
"#include ""apptype.h""
"#ifdef APP_A" // <---- error
"#include ""app_a.rc""
"#else"
"#include ""app_b.rc""
"#endif"
// the other #includes
"\0"
END

However, RC is producing an RC2104 error on the marked line with the message
"Undefined keyword or key name: #ifdef". Since RC normally understands #ifdef I
don't understand why it complains about one in a TEXTINCLUDE. Can someone
explain it?

On the same subject, this is an MDI application with one menu for each of many
different document types. I have had to put _all_ menus in both
app-name-specific resource files, and it's only because of the "About..." item
on the Help menu (since, by convention, this item includes the application name:
"About AppA..." or "About AppB..."). This means that any menu changes must be
done in both resource files. Is there an easier way to do this?

David:

Not quite sure I understand what you want here, but if something cannot
be accomplished by the conditional compilation feature of the resource
editor, then I move the material into the .rc2 file, where you can put
#ifdef's as you desire without upsetting the resource editor. In my case
this is just a few strings.

As to the About box, it is easy enough to write code in OnInitDialog to
modify the text. Use AFX_IDS_APP_TITLE to get the app title (this is one
of the strings that I put in the .rc2 file).

David Wilkinson
 
G

Guest

Hi,

Can you try adding \r\n at the end of each line like this:

3 TEXTINCLUDE
BEGIN
"#include ""apptype.h"\r\n"
"#ifdef APP_A\r\n"
....

It's like that in my auto-generated .rc files. Maybe in your example he's
concatenating the 2 strings on the same line, which would explain the error.

Wim
 
D

David W

David Wilkinson said:
Not quite sure I understand what you want here,

I was hoping to understand one of:
1. Why TEXTINCLUDE doesn't appear to do what the name suggests, i.e., simply
insert verbatim whatever text is inside the quotes.
2. If it does do the above, then what syntax is needed to insert the #ifdef?
but if something cannot be accomplished by the conditional compilation feature
of the resource editor, then I move the material into the .rc2 file, where you
can put #ifdef's as you desire without upsetting the resource editor.

Thanks. I might try something like that if I can't get TEXTINCLUDE to do what I
want.
In my case this is just a few strings.

In my case a lot more.
As to the About box, it is easy enough to write code in OnInitDialog to modify
the text. Use AFX_IDS_APP_TITLE to get the app title (this is one of the
strings that I put in the .rc2 file).

The About box isn't the problem (that's duplicated as well, but there's only one
About box). The problem is the all the menus (9 of them, one for each MDI doc
type) that lead to the About box. By convention the menu item says "About
<appname>...", but since the appname varies according to the #ifdef, those menus
have to duplicated.

David
 
D

David W

Wim said:
Hi,

Can you try adding \r\n at the end of each line like this:

3 TEXTINCLUDE
BEGIN
"#include ""apptype.h"\r\n"
"#ifdef APP_A\r\n"
...

It's like that in my auto-generated .rc files. Maybe in your example he's
concatenating the 2 strings on the same line, which would explain the error.

Good suggestion. Unfortunately, it didn't work. Now I get, "Undefined keyword or
key name: \r\n".

I notice that the wizard-generated #includes that were already in that
TEXTINCLUDE did not have \r\n before the closing quote, though one line had
"\r\n" on its own.

David
 
D

David W

David W said:
Good suggestion. Unfortunately, it didn't work. Now I get, "Undefined keyword
or key name: \r\n".

I notice that the wizard-generated #includes that were already in that
TEXTINCLUDE did not have \r\n before the closing quote, though one line had
"\r\n" on its own.

I take that back. There is a comment on each line that's part of the included
text, and the \n\r is there. I'm pretty sure now that it was just a syntax
problem all along. You can count 5 quote marks in the apptype.h line, whereas
there should be an even number, and the \n\r was needed as well.

David
 
D

David Wilkinson

David said:
The About box isn't the problem (that's duplicated as well, but there's only one
About box). The problem is the all the menus (9 of them, one for each MDI doc
type) that lead to the About box. By convention the menu item says "About
<appname>...", but since the appname varies according to the #ifdef, those menus
have to duplicated.

David:

Well, you could write an ON_UPDATE_COMMAND_UI handler to modify the
displayed menu text.

David Wilkinson
 
B

Ben Voigt

David W said:
I take that back. There is a comment on each line that's part of the
included text, and the \n\r is there. I'm pretty sure now that it was just
a syntax problem all along. You can count 5 quote marks in the apptype.h
line, whereas there should be an even number, and the \n\r was needed as
well.

Coupled with a fundamental misunderstanding of what a pair of double quotes
does in C++. In VB, that gives you a quote character inside your string, in
C++ it ends one string and starts another, which the preprocessor helpfully
stitches together (it's useful when one of the strings is a macro
expansion).

What you want is to use the backslash to correct escape quotes, generating
quotes inside the string:
"#include \"apptype.h\"\r\n"
 
D

David W

Ben Voigt said:
Coupled with a fundamental misunderstanding of what a pair of double quotes
does in C++.

No, I know what a pair of double quotes does in C++.
In VB, that gives you a quote character inside your string,

Do you assume I'm a VBer who's new to C++? I've been writing C++ for >10 years.
I've hardly touched VB and know almost nothing about it.
in C++ it ends one string and starts another, which the preprocessor helpfully
stitches together (it's useful when one of the strings is a macro expansion).

Not necessarily anything to do with this problem, which is in _resource_ code,
not C++ code. For all I knew, resource code might or might not use C++ rules for
these things. I had no idea what it's rules were, which is why I asked.
What you want is to use the backslash to correct escape quotes, generating
quotes inside the string:
"#include \"apptype.h\"\r\n"

This is the resource code that the IDE's own wizard produces when you ask it to
create a skeleton MFC application. Note the double quotes.
3 TEXTINCLUDE
BEGIN
"#define _AFX_NO_SPLITTER_RESOURCES\r\n"
"#define _AFX_NO_OLE_RESOURCES\r\n"
"#define _AFX_NO_TRACKER_RESOURCES\r\n"
"#define _AFX_NO_PROPERTY_RESOURCES\r\n"
"\r\n"
"#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
"LANGUAGE 9, 1\r\n"
"#pragma code_page(1252)\r\n"
"#include ""res\\Temp.rc2"" // non-Microsoft Visual C++ edited
resources\r\n"
"#include ""afxres.rc"" // Standard components\r\n"
"#include ""afxprint.rc"" // printing/print preview resources\r\n"
"#endif\r\n"
"\0"
END

And this is the text that results from the above:
#define _AFX_NO_SPLITTER_RESOURCES
#define _AFX_NO_OLE_RESOURCES
#define _AFX_NO_TRACKER_RESOURCES
#define _AFX_NO_PROPERTY_RESOURCES

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1
#pragma code_page(1252)
#include "res\\Temp.rc2" // non-Microsoft Visual C++ edited resources
#include "afxres.rc" // Standard components
#include "afxprint.rc" // printing/print preview resources
#endif
#endif // not APSTUDIO_INVOKED

David
 
B

Ben Voigt

No, I know what a pair of double quotes does in C++.
Do you assume I'm a VBer who's new to C++? I've been writing C++ for >10
years. I've hardly touched VB and know almost nothing about it.

Sorry about the misinformed statements then. I simply would say that when
the strings already contain C++ escape sequences (\r\n), then other C++
escape sequences would most likely work. I suspect that \" will produce the
desired behavior, and would certainly be far more consistent with C++ code.

Resource files are run through the C preprocessor, so many of the same rules
apply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top