Data types in Microsoft Examples.

I just started programing in Windows and i keep getting stuck on the same problems over and over with microsofts example codes, where they keep switching from PCWSTR data types to LPCSTR data types, without mking any casts. That slows things down considerably since i keep getting compiler errors in VS2010.

Are the authors of those examples unintentionally using 16bit char values and/ forgot to cast to 8 bit ones.

Or is there a trick hidden in there somewhere?

Here's the example code im getting stuck on.

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
BOOL Create(
        PCWSTR lpWindowName,
        DWORD dwStyle,
        DWORD dwExStyle = 0,
        int x = CW_USEDEFAULT,
        int y = CW_USEDEFAULT,
        int nWidth = CW_USEDEFAULT,
        int nHeight = CW_USEDEFAULT,
        HWND hWndParent = 0,
        HMENU hMenu = 0
        )
    {
        WNDCLASS wc = {0};

        wc.lpfnWndProc   = DERIVED_TYPE::WindowProc;
        wc.hInstance     = GetModuleHandle(NULL);
        wc.lpszClassName = ClassName();

        RegisterClass(&wc);

        m_hwnd = CreateWindowEx(
            dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
            nWidth, nHeight, hWndParent, hMenu, GetModuleHandle(NULL), this
            );

        return (m_hwnd ? TRUE : FALSE);
    }

PCWSTR  ClassName() const { return L"Sample Window Class"; }



Basicly the ClassName function is called in one of the CreateWindowEx's parameters that only takes LPCSTR data types.

Link to the entier page.
http://msdn.microsoft.com/en-us/library/ff381400%28v=VS.85%29.aspx
I think you need to alter your project so it builds Unicode

CreateWindowEx (like many WIN32 API functions which take a LPCSTR/LPCWSTR) is a macro that evaluates to CreateWindowExA (takes a LPCSTR) when _UNICODE is not defined, and CreateWindowExW (takes a LPCWSTR) when _UNICODE is defined

Or you can use CreateWindowExW directly.

You can switch to Unicode on the project's properties' "General" page : should be "Character Set" = "Use Unicode Character Set"

Andy
Last edited on
Thank's That fixed it :)
Topic archived. No new replies allowed.