Search code examples
cwinapiwin32guilpcwstr

Problem with the L prefix ( LPCWSTR typecast) in winAPI


I'm new to winAPI and encountered a problem I can't seem to solve... Couldn't find a solution by google yet, either.

My program has several buttons of similar size so I made a macro to hide all the mess. The original macro was:

#define _BUTTON(s, x, y, v)    CreateWindowW(L"Button", (L)s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);

However, "L(s)" doesn't work with or without the parenthesis, on s or L. I also tried replacing L with LPCWSTR, WCHAR*, _T(), etc... The compiler error is always the same: "L (or LPCWSTR, etc) is not declared in this scope" although I thought it should be...

For now I resolved the issue by going with the non-Unicode:

#define _BUTTON(s, x, y, v)    CreateWindow("Button", s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);

But I'd like all the windows to support the same chars... Where is the problem?


Solution

  • One way is what RbMm metioned, like:

    #define Create_Button(s, x, y, v)     CreateWindowW(L"Button", L##s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);
    

    Another way is to use a common approach:

    #define Create_ButtonA(s, x, y, v)    CreateWindowA("Button", s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);
    #define Create_ButtonW(s, x, y, v)    CreateWindowW(L"Button", s, WS_VISIBLE | WS_CHILD, x, y, 75, 25, hWnd, (HMENU)v, 0, 0);
    #ifdef _UNICODE
    #define Create_Button(s, x, y, v)     Create_ButtonW(s, x, y, v)
    #else
    #define Create_Button(s, x, y, v)     Create_ButtonA(s, x, y, v)
    #endif
    

    Usage:

    Create_Button(TEXT("name"),10,10,2);
    Create_ButtonA("name",10,10,2);
    Create_ButtonW(L"name",10,10,2);