0

I'm trying to make a macro that expands a symbolic constant to a case statement like this:

// #define WM_PAINT 0x000F
LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        BIND_MSG(WM_PAINT)
        // -> case WM_PAINT: Wnd_Main_WM_PAINT (hWnd, wParam, lParam); break;
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

I tried many things. The best working macro I got was:

#define BIND_MSG(msg) case (msg): Wnd_Main_##msg (hWnd, wParam, lParam); break;

But this macro doesn't expand to Wnd_Main_WM_PAINT, but to Wnd_Main_0x000F. When I try Wnd_Main_ ## #msg instead of Wnd_Main_##msg, I don't get Wnd_Main_WM_PAINT, but Wnd_Main_. Is there any possibility to concatenate Wnd_Main_ with the name of the symbolic constant WM_PAINT?

Visual Studio expands the macro to <code>Wnd_Main_0x000F</code>

(Sorry, the Screenshot is German. Visual Studio says, that the identifier Wnd_Main_0x000F isn't defined.)

Cubi73
  • 1,891
  • 3
  • 31
  • 52
  • [I get `Wnd_Main_WM_PAINT`](http://coliru.stacked-crooked.com/a/ab8f3818f4716db2). – chris Feb 09 '14 at 01:12
  • Is your `WM_PAINT` defined like `#define WM_PAINT 0x000F`? – Cubi73 Feb 09 '14 at 01:20
  • Yes, it's right in the link. I used as much of your posted code as I could. – chris Feb 09 '14 at 01:21
  • 1
    More important question "Why are you developing code that is difficult to maintain?" – Ed Heal Feb 09 '14 at 01:21
  • Yes, the link works, but Visual Studio continues expanding it to `Wnd_Proc_0x000F`. – Cubi73 Feb 09 '14 at 01:27
  • AFAIK, it's standard behaviour to expand it properly and non-standard behaviour to do otherwise. Does the exact code in the link expand to that? – chris Feb 09 '14 at 01:28
  • it sounds like `WM_PAINT` is being forced through intermediate expansion, something used as-designed for text macros [such as this](http://stackoverflow.com/questions/13275015/c-preprocessor-literal-construction/13275048#13275048). A better question is, if you're doing this, why not just use the message crackers in [`windowsx.h`](http://www.dreamincode.net/forums/topic/286954-using-windowsxh-for-better-code-organization-and-message-cracking/) – WhozCraig Feb 09 '14 at 01:37
  • 1
    @chris: Coliru expands it to `Wnd_Main_WM_PAINT`, Visual Studio to `Wnd_Main_0x000F` – Cubi73 Feb 09 '14 at 01:46

0 Answers0