Newbie question on c++ win32 programming.

**Hi there,

I’m working on my first non-console program and I’m stuck. This is the skeleton code I start with :**


#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

When I try to compile it (using VS2k5), I get the following errors:
*
1>simplewindow.cpp(40) : error C2440: ‘=’ : cannot convert from ‘const char [14]’ to ‘LPCWSTR’
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>simplewindow.cpp(46) : error C2664: ‘MessageBoxW’ : cannot convert parameter 2 from ‘const char [28]’ to ‘LPCWSTR’
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>simplewindow.cpp(57) : error C2664: ‘CreateWindowExW’ : cannot convert parameter 2 from ‘const char [14]’ to ‘LPCWSTR’
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>simplewindow.cpp(62) : error C2664: ‘MessageBoxW’ : cannot convert parameter 2 from ‘const char [24]’ to ‘LPCWSTR’
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast*

Supposedly, I have to add a capital L before each string, like so: L"bla bla" however, when I do so, I only manage to get this additional error:

simplewindow.cpp(3) : error C2440: ‘initializing’ : cannot convert from ‘const wchar_t [14]’ to ‘const char []’
1> There is no context in which this conversion is possible

**Could anyone explain the problem and how to fix it to me?

Thank you for your time.

Gozu**

ps: Bold added for clarity’s sake. (to make my comments stand out from the code)

The quick answer is to ensure the proprocessor define UNICODE is not defined in your code. In project properties C/C+±>Preprocesser->preprocessor defines, often it is set as part of the “project defaults” so you need to uncheck the “inherit from parent or project defaults” box.

Having this defined will mean all the windows functions take 16-bit unicode wide-chars (the …W functions) instead of normal 8-bit chars (the …A functions).

That worked! Thank you sir!

It used to be important to always define both UNICODE and _UNICODE. I don’t know if it still is or if the Windows headers have straightened it out.