c++windowscreatewindowcreatewindowex

Struggling with the CreatWindow function


I am a beginner and I am trying to code my first game. I was following a tutorial that was made some time ago. This is my code:

#include <windows.h>


//Callback function

LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}



int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    //Create Window Class 

        WNDCLASS window_class = {};
        window_class.style = CS_HREDRAW | CS_VREDRAW;
        window_class.lpszClassName = L"Game Window Class";
        window_class.lpfnWndProc = window_callback;

    //Register Class

        RegisterClass(&window_class);
    

    //Create Window
    CreateWindow(window_class.lpszClassName, "My First Game!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);




};

When I try to compile the code it gives me back these 2 errors:

E0167   argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
    
C2664   'HWND CreateWindowExW(DWORD,LPCWSTR,LPCWSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)': cannot convert argument 3 from 'const char [15]' to 'LPCWSTR'  

What am I doing wrong?


Solution

  • You should use TEXT macro to express string literals when you use Windows API without explicitly specifying ANSI version (with A suffix) or Unicode version (with W suffix).

    Wrong lines:

    window_class.lpszClassName = L"Game Window Class";
    CreateWindow(window_class.lpszClassName, "My First Game!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);
    

    Corrected lines:

    window_class.lpszClassName = TEXT("Game Window Class");
    CreateWindow(window_class.lpszClassName, TEXT("My First Game!"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);