win32 hello world type app

merc141

Limp Gawd
Joined
Jul 24, 2007
Messages
194
I thought maybe somebody could help me out here. This is my first try with win32 and I am not sure where to start... I think its some sort of unicode issue but I don't know... I am using C++ 2008 Express

The error is in RegisterWindowClass function and a call to CreateWindowEx

Errors:
Code:
1>c:\users\matt\documents\visual studio 2008\projects\basicwindowsapp\basicwindowsapp\basicwindowsapp.cpp(68) : error C2440: '=' : cannot convert from 'const char [7]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\matt\documents\visual studio 2008\projects\basicwindowsapp\basicwindowsapp\basicwindowsapp.cpp(91) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [7]' to 'LPCWSTR'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

The code in entirety:
Code:
//////////////////////////////////////////////////////
// BasicWindowsApp.cpp
//////////////////////////////////////////////////////

#include <windows.h>

// Function prototypes.
LRESULT WINAPI WndProc(HWND hWnd, UINT msg,
    WPARAM wParam, LPARAM lParam);
void RegisterWindowClass(HINSTANCE hInstance);
void CreateAppWindow(HINSTANCE hInstance);
WPARAM StartMessageLoop();

// Global variables.
HWND g_hWnd;


//////////////////////////////////////////////////////
// WinMain()
//////////////////////////////////////////////////////
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
    RegisterWindowClass(hInstance);
    CreateAppWindow(hInstance);
    ShowWindow(g_hWnd, SW_SHOWDEFAULT);
    UpdateWindow(g_hWnd);
    StartMessageLoop(); //book has: INT result=StartMessageLoop(); return result;
    return 0;
}

//////////////////////////////////////////////////////
// WndProc()
//////////////////////////////////////////////////////
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
        return 0;

    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;

    case WM_PAINT:
        ValidateRect(g_hWnd, NULL);
        return 0;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

//////////////////////////////////////////////////////
// RegisterWindowClass()
//////////////////////////////////////////////////////
void RegisterWindowClass(HINSTANCE hInstance)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "WinApp"; ////////////////////////// ERROR 1
    wc.hIconSm = NULL;

    RegisterClassEx(&wc);
}

//////////////////////////////////////////////////////
// CreateAppWindow()
//////////////////////////////////////////////////////
void CreateAppWindow(HINSTANCE hInstance)
{
    g_hWnd = CreateWindowEx(
        NULL,
        "WinApp",
        "Basic Windows Application", 
        WS_OVERLAPPEDWINDOW,
        100,
        100,
        648,
        514,
        GetDesktopWindow(),
        NULL,
        hInstance,
        NULL); ////////////////////////////////////////////// ERROR 2
}

//////////////////////////////////////////////////////
// StartMessageLoop()
//////////////////////////////////////////////////////
WPARAM StartMessageLoop()
{
    MSG msg;
    while(1)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            // Use idle time here.
        }
    }
    return msg.wParam;
}

Thanks in advance
 
I figure that you've set your application to compile targeting Unicode. This means that lpszClassName is a pointer to a Unicode string, but you're supplying an ANSI string. Same thing for the CreateWindowW parameter. (If you were compiling for ANSI, you'd be compiling for CreateWindowA.)

Later versions of Visual Studio default to Unicode support for new projects, and you probably just accepted that default. The setting can be changed on the "General" page of your project's properties window.
 
Ok I can compile everything fine using multibyte, but suppose I wanted to use unicode. I can get rid of the first error easy enough (wc.lpszClassName = L"WinApp";) ... I will have to look in to a unicode version of the function. Thanks for the quick reply.
 
"Something" is a multibyte string, and L"Something" is a Unicode string. You can get the right kind of string at compile time using the _T() macro. _T( "Something" ) will be "Something" if you're building for ANSI, or L"Something" if you're building for Unicode.

Similarly, the Win32 API functions are wrapped with macros. CreateWindowEx() is automatically CreateWindowExA() for ANSI if you're building ANSI, and CreateWindowExW() if you're building for Unicode.

The C Runtime functions have different names for for switchable version. strcat() becomes _tcscat(), for example.
 
Mike, can you recommend any books on Windows programming?
 
Well, there's the book I wrote; it's mostly about MFC and assumes you know C++. Professional MFC with Visual C++ 6 is the current edition, which is getting out of date but still has value, paricularly since it's out of print and available in the used market very inexpensively. You can find Professional Mfc With Visual C++5 for even less.

Petzold's Programming Windows book is the de facto reference for C/C++ programmers.

You don't mention which language you want to use, so I've assumed C or C++.

Regardless of language, you should learn how Windows works when programming it. The best book for this is Microsoft Windows Internals. The 4th edition is current, the 5th edition was announced for November and then pushed back to February.
 
As an Amazon Associate, HardForum may earn from qualifying purchases.
Thanks for those references! (and yes, C++, sorry for not specifying) I've been waiting on the 5th edition of Internals for ages, it seems. How much has MFC changed since those related books were published?
 
MFC is very much the same as far as its strucutre goes; what parts of the app go where, and so on. There's a few fundamental changes, like CString having a templated version, now; but I don't think the book has become invalid. It's certainly a bargain on the used market, and considering the price, I don't see how you can go wrong.
 
The Programming Windows book looks a bit dated. The MFC one is more recent... is the content more or less the same since then? I don't know enough about MFC to know if I really need it. I am trying to get through a Direct3D book and was just wondering which book would be best to get. Thanks for the help!
 
Programming Windows isn't dated. The Windows API has grown in scope by adding interfaces to feature that Windows didn't have before. The fundamentals--what you need to learn before you can get to any of the newer APIs, anyway--haven't changed substantively since the release of Win32.
 
Back
Top