Window Creation error

HI,

im currently beginning to learn Windows API Programming with C++.
Im having trouble getting my first window program to work: I always get an error. :(

Can anyone please tell me what im doing wrong?
Thanks in advance!

Heres my source:
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 <windows.h>

const char g_szClassName[] = "myWindowClass";

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;

    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 = LPCWSTR(g_szClassName); 
/*Note when I use g_szClassName without LPCWSTR I get a compilation error(I use Visual Studio 2010)
[output]error C2440: '=' : cannot convert from 'const char [14]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast[/output] */
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

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

    hwnd = CreateWindowExA(
        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);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
Last edited on
What exactly is failing?
Replace LPWCSTR with LPCSTR and you'll be fine.
Edit: oh wait. Looks like it's the other way around for you.

In that case:
filipe wrote:
What exactly is failing?


Edit2: I suppose I can guess... replace char in the third line with wchar_t.
Last edited on
I think by default projects in VS 2010 are UNICODE, need to change project setting "Character Set".
@ filipe I always get the error message after I compile
I see. As binarybob350 said, it's because the UNICODE symbol is defined. Either change the types of every char* you pass to the API functions to wchar_t*, as Athar said, or change the set to ASCII.
Ive changed the Caracter Set to Multi-Byte(ASCII) and now the program works!

THANK YOU VERY MUCH!!!! :)
If you ever want a program that you can compile in both ASCII and UNICODE you can use TCHAR in place of char or wchar_t. check out the include file tchar.h provided by MS. Also you need to drop the capitcal A from the end of the following functions: MessageBoxA and CreateWindowExA. If you call MessageBox it will map to MessageBoxA when in ASCII and MessageBoxW when in UNICODE. Also you would need to wrap all double quoted strings ith _T()

For example:

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

would become

MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);

Topic archived. No new replies allowed.