LPCWSTR conversetion problem

hey
im having this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <atlstr.h>
#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))
    {
        MessageBoxA(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)
    {
        MessageBoxA(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;
}

and i got these errors when i try to build and compile it:
"cannot convert from 'const char [14]' to 'LPCWSTR' line 42 (this line: "wc.lpszClassName = g_szClassName;")
and
"Error 2 error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [14]' to 'LPCWSTR' line 58
(this line: "hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);")

did i missed something ?
thanks !

1
2
3
4
5
6
7
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
(LPCWSTR)g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);


You should use a string where possible though.
closed account (z05DSL3A)
I would change line 42 to:
const TCHAR g_szClassName[] = TEXT("myWindowClass");

and line 58 to
1
2
3
4
5
6
7
hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        TEXT("The title of my window"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);


Have read around the TEXT() macro, TCHAR and Unicode, but basicaly if you use TCHAR and the TEXT() macro the correct charecter type will be used (wide or narrow).

HTH

edit:
I noticed you use MessageBoxA()
1
2
3
4
    MessageBoxA(NULL, 
               "Window Registration Failed!", 
               "Error!",
               MB_ICONEXCLAMATION | MB_OK);

you would be better off using MessageBox() with the TEXT() macro:
1
2
3
4
    MessageBox(NULL, 
               TEXT("Window Registration Failed!"), 
               TEXT("Error!"),
               MB_ICONEXCLAMATION | MB_OK);


Basically there are a lot on macros 'wrapping' function calls for example CreateWindowEx is a macro that will either be resolved to CreateWindowExW(...) or CreateWindowExA(...) depending on the definition of UNICODE. If you get used to using TCHAR and the TEXT macro you will not have to worry about the W and A variants of function calls.


Edit: Typo in final code block has been corrected: (it did read MessageBoxA and should have been MessageBox)
Last edited on
you can use either 1), 2) or 3)
1)
1
2
3
4
 MessageBoxA(NULL, 
               "Window Registration Failed!", 
               "Error!",
               MB_ICONEXCLAMATION | MB_OK);

2)
1
2
3
4
MessageBoxW(NULL, 
               L"Window Registration Failed!", 
               L"Error!",
               MB_ICONEXCLAMATION | MB_OK);

3)
1
2
3
4
MessageBox(NULL, 
               TEXT("Window Registration Failed!"), 
               TEXT("Error!"),
               MB_ICONEXCLAMATION | MB_OK);


if UNICODE defined, 3) == 2)
else 3) == 1)
closed account (z05DSL3A)
Kavin9,

I have read your post several times but can not determine what your point is, can you clarify?
well, I mean if macro UNICODE defined
MessageBoxW() euqls to MessageBox() and L"String here" euqls to TEXT("String here")

1
2
3
4
5
#ifdef UNICODE
#define MessageBox  MessageBoxW
#else
#define MessageBox  MessageBoxA
#endif // !UNICODE 
Try making a LPTSTR conversion...
1
2
char buffer[50]="Window registration failed!Error!";
MessageBoxA(NULL, LPTSTR(buffer), MB_ICONEXCLAMATION | MB_OK);

In Project options go to general and change unicode to multibyte
Topic archived. No new replies allowed.