4

I have a resource file where needed create string define with concatenation macros and string, something like this

#define _STRINGIZE(n) #n
#define STRINGIZE(n) _STRINGIZE(n)
#define Word_ Word
100 DIALOGEX 0, 0, 172, 118
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Hello"STRINGIZE(Word_)=>"Hello"Word" 

but needed simple "HelloWord" without average quotes.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Topilski Alexandr
  • 669
  • 1
  • 12
  • 34
  • 1
    I know the difference between C and C++, please answer the question, if you don't know how solve this problem please go away and not spaming here.I market with C tags because concatenation may useful in c language too. – Topilski Alexandr Sep 28 '12 at 09:43
  • Luchian, if you can't tell when a question addresses a common feature of C and C++ rather than specifically addressing one or the other, you should start with a constructive response, rather than develop snark... – jonvuri Sep 28 '12 at 10:40

1 Answers1

4

For anyone who cares: a .rc file is a resource file from an MFC project that defines UI elements, such as dialog layouts. It uses the same preprocessor as C++, but it does not share C++'s syntax -- and in a window CAPTION field, two string literals won't concatenate by just juxtaposing them. Within a string literal, two double quotes is actually an escape sequence that generates one double quote character. So the literal:

"Hello""World"

ends up looking like

Hello"World

In your dialog Window's caption.

The problem with the example given:

CAPTION "Hello"STRINGIZE(Word_)

Is that the double-quote at the end of "Hello" must be removed, but the preprocessor cannot do this. However, if "Hello" is allowed to be included in a macro, concatenation is possible. First, I defined these macros:

#define CONCAT(a,b) a##b
#define STRINGIZE_(x) #x
#define STRINGIZE(x) STRINGIZE_(x)

then, inside the dialog record:

  ...
EXSTYLE WS_EX_APPWINDOW
CAPTION STRINGIZE(CONCAT(Hello,World))
FONT 10, "Segoe UI Semibold", 600, 0, 0x0
  ...

With this, the dialog's caption ends up looking like HelloWorld -- no stray quotes or anything. I hope you can use this technique.

Christopher Oicles
  • 3,017
  • 16
  • 11
  • Thanx, i knew this solution but my program is multilanguages and it is imposible to define all string lines,i can't open double quotes elsewhere, because this line contains some special characters as (, \n ''). i create some macros as STRINGIZE_COMMA2(x,y) STRINGIZE(CONCAT(X,Y)) and so on, but special symbols do this work very hard and some line i don't know how to join. – Topilski Alexandr Sep 29 '12 at 05:04
  • I found concatenation and stringizing to work properly only with the boost preprocessor macros (as mentioned in this SO answer). Special characters can be used/inserted via their hex representation (e.g. \x2c for ',' or \x20 for a space) – klaus triendl Oct 22 '15 at 11:37