Jacob Poeschel
Jacob Poeschel

Reputation: 11

Errors when trying to create a blank window in C++ / difference between LPCWSTR and LPCSTR, WinAPI

I have followed several tutorials, and am attempting to make a basic C++ game, starting with getting the window running.

When I try to run my code, I get a series of errors regarding the type of string, and no matter which tutorial I follow, I end up with similar errors.

I have tried including UNICODE, messing with basically every type of string declaration I could find, and it still doesn't work.

Overall, I am just very confused on how the Windows API handles strings, and why the type of string even matters.

Attached is my Window's constructor, which results in this error:

window.cpp:25:30: error: cannot convert 'const wchar_t*' to 'LPCSTR'
> {aka 'const char*'} in assignment    25 |     wndClass.lpszClassName =
> CLASS_NAME;
Window::Window()
    : m_hInstance(GetModuleHandle(nullptr))
{
    const wchar_t* CLASS_NAME = L"Jacob's Window Class";

    WNDCLASS wndClass = {};
    wndClass.lpszClassName = CLASS_NAME;
    wndClass.hInstance = m_hInstance;
    wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndClass.lpfnWndProc = nullptr; // Will update this later once I write it

    RegisterClass(&wndClass);

    DWORD style = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;

    int width = 640;
    int height = 480;

    RECT rect;
    rect.left = 250;
    rect.top = 250;
    rect.right = rect.left + width;
    rect.bottom = rect.top + height;

    AdjustWindowRect(&rect, style, false);

    m_hWnd = CreateWindowEx(
        0,
        CLASS_NAME,
        L"Title",
        style,
        rect.left,
        rect.top,
        rect.right - rect.left,
        rect.bottom - rect.top,
        NULL,
        NULL,
        m_hInstance,
        NULL
    ); 

    ShowWindow(m_hWnd, SW_SHOW);
}

Upvotes: 1

Views: 94

Answers (1)

Jeaninez - MSFT
Jeaninez - MSFT

Reputation: 4040

I suggest you could refer to the Doc:Conventions for Function Prototypes

For all functions with text arguments, applications should normally use the generic function prototypes. If an application defines "UNICODE" either before the #include statements for the header files or during compilation, the statements will be compiled into Unicode functions.

You could try to provide the generic function name implemented as a macro in the header file.

Or you could try to use Unicode function name (The letter "W" is added at the end of the generic function name) instead of generic function name.

Upvotes: 0

Related Questions