0

I'm following handmade hero series and I run into some bugs when I tried compiling, I'm running Visual Studio 2017 here's a screenshot of bugs:

bugs

Here's the code were the problem occurs:

int CALLBACK
WinMain(HINSTANCE Instance,
HINSTANCE PrevInstance,
LPSTR CommandLine,
int ShowCode)
{
WNDCLASS WindowClass = {};

// TODO(casey): Check if HREDRAW/VREDRAW/OWNDC still matter
WindowClass.lpfnWndProc = Win32MainWindowCallback;
WindowClass.hInstance = Instance;
//    WindowClass.hIcon;
WindowClass.lpszClassName = "HandmadeHeroWindowClass";

if (RegisterClassA(&WindowClass))
{
    HWND WindowHandle =
        CreateWindowExA(
            0,
            WindowClass.lpszClassName,
            "Handmade Hero",
            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            0,
            0,
            Instance,
            0);

Compile errors:

  • "const char *" cannot be assigned to an entity of type "LPCWSTR"
  • "WNDCLASS *" is incompatible with parameter of type "const WNDCLASSA *"
  • "LPCWSTR" is incompatible with parameter of type "LPCSTR"
jww
  • 97,681
  • 90
  • 411
  • 885
  • You are compiling for wide characters but your code uses narrow strings and the A functions. – drescherjm Sep 16 '18 at 21:07
  • https://stackoverflow.com/questions/33714546/winapi-unicode-and-ansi-functions – drescherjm Sep 16 '18 at 21:12
  • https://stackoverflow.com/questions/1319461/how-do-i-turn-off-unicode-in-a-vc-project – drescherjm Sep 16 '18 at 21:14
  • Use `HWND WindowHandle = CreateWindowEx(...)` and `_T("Handmade Hero")` for starters. – jww Sep 17 '18 at 02:36
  • @jww: There is no convincing reason to use [generic-text mappings](https://learn.microsoft.com/en-us/cpp/c-runtime-library/generic-text-mappings). The code isn't going to get compiled for different systems, where one of them only supports one set of API versions (ANSI or Unicode). – IInspectable Sep 17 '18 at 07:15

1 Answers1

0

The issue is caused by a mixture of incompatible types. While the code makes sure to call RegisterClassA (the ANSI version of the API), it fails to provide a matching WNDCLASS structure. This is mixing a type used for generic-text mapping in a context that's using explicit ANSI or Unicode invocations.

The easiest fix is to replace WNDCLASS with WNDCLASSA. A better approach would be to learn about ANSI, Unicode, and why it is best to use the Unicode versions in the Windows API throughout your code (Unicode in Windows equates to UTF-16LE encoding).

Start by reading Conventions for Function Prototypes.

IInspectable
  • 46,945
  • 8
  • 85
  • 181