简体   繁体   中英

Beginning DirectX 11 Game Programming Error

Here is the code that it gives me on page 33:

#include<Windows.h>

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
       LPWSTR cmdLine, int cmdShow)
{
    UNREFERENCED_PARAMETER( prevInstance );
    UNREFERENCED_PARAMETER( cmdLine );
    WNDCLASSEX wndClass = { 0 };
    wndClass.cbSize = sizeof( WNDCLASSEX ) ;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    wndClass.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "DX11BookWindowClass";
    if( !RegisterClassEx( &wndClass ) )
    return -1;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    HWND hwnd = CreateWindowA( "DX11BookWindowClass", "Blank Win32 Window",
    WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.
    left,
    rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
    if( !hwnd )
    return -1;
    ShowWindow( hwnd, cmdShow );

    return 0;
}

This code gives me 2 errors-

1>------ Build started: Project: BlankWindow, Configuration: Debug Win32 ------ 1> main.cpp 1>c:\\coding\\c++\\visual c++\\directx\\blankwindow\\blankwindow\\main.cpp(10): error C2065: 'WndProc' : undeclared identifier 1>c:\\coding\\c++\\visual c++\\directx\\blankwindow\\blankwindow\\main.cpp(15): error C2440: '=' : cannot convert from 'const char [20]' to 'LPCWSTR' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Basically

'WndProc' : undeclared identifier And '=' : cannot convert from 'const char [20]' to 'LPCWSTR'

What is wrong with this code?

Here is a basic working program of your code:

#include<Windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
            {
                    case WM_CLOSE:
                            DestroyWindow(hwnd);
                            break;
                    case WM_DESTROY:
                            PostQuitMessage(0);
                            break;
                    default:
                            return DefWindowProc(hwnd,
                                                 message,
                                                 wParam,
                                                 lParam);
            }
    return 0;
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
       LPSTR cmdLine, int cmdShow)
{
    UNREFERENCED_PARAMETER( prevInstance );
    UNREFERENCED_PARAMETER( cmdLine );
    WNDCLASSEX wndClass = { 0 };
    wndClass.cbSize = sizeof( WNDCLASSEX ) ;
    wndClass.style = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.hCursor = LoadCursor( NULL, IDC_ARROW );
    wndClass.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = "DX11BookWindowClass";
    if( !RegisterClassEx( &wndClass ) )
    return -1;
    RECT rc = { 0, 0, 640, 480 };
    AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );
    HWND hwnd = CreateWindowA( L"DX11BookWindowClass", L"Blank Win32 Window",
    WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.
    left,
    rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
    if( !hwnd )
    return -1;
    ShowWindow( hwnd, cmdShow );
    MSG msg;
    while (GetMessage (&msg, 0, 0, 0))
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
    return msg.wParam;
}

Points:

  1. I added a WndProc which handles all messages sent to your program.

  2. I added a message loop to your program. Otherwise, the window would close right away.

  3. I added the L prefix as suggested by the_mandrill in the other answer.

This is pretty much your "Hello World" of WinAPI programming. However, I strongly suggest you learn the WinAPI first before jumping into DirectX programming.

The problem is that the project is being built as Unicode , but your code is non-Unicode. In other words the Windows API calls are expecting wide (ie 16-bit) strings but your code uses 8-bit strings ('char'). LPCWSTR means long pointer to a constant wide string . So the function call is expecting a constant wide string, but you're passing an 8-bit string. You have two options:

  • Change the project to use the non-Unicode libraries (Properties -> General -> Character Set = 'Use Multi-byte')
  • Fix up your code to make it Unicode. In this case by changing the strings it is complaining about to make them into wide strings. You can do this by prefixing them with L , eg L"DX11BookWindowClass"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM