KeyC0de
KeyC0de

Reputation: 5287

Cannot start DirectX 11 executable with Visual Studio, Build works properly

Following is Directx 11 code, that displays a window and keeps it open waiting for messages:

#include "stdafx.h"
#include <iostream>

LRESULT CALLBACK WindowProc(_In_ HWND   hwnd, _In_ UINT   uMsg,
    _In_ WPARAM wParam, _In_ LPARAM lParam)
{
    if (uMsg == WM_DESTROY) {
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

// Directx main
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmd, int nCmdShow)
{
    WNDCLASSEX window;
    ZeroMemory(&window, sizeof(WNDCLASSEX));
    window.cbSize = sizeof(WNDCLASSEX);
    window.hbrBackground = (HBRUSH) COLOR_WINDOW;
    window.hInstance = hInstance;
    window.lpfnWndProc = WindowProc;
    window.lpszClassName = (LPCWSTR)"MainWindow";   // class name
    window.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClassEx(&window);

    HWND windowHandle = CreateWindow((LPCWSTR)"Main Window", (LPCWSTR)"DirectX Tut!", WS_OVERLAPPEDWINDOW,
        100, 100, 600, 800, NULL, NULL, hInstance, 0);

    if (!windowHandle) 
        return -1;

    ShowWindow(windowHandle, nCmdShow);

    MSG message;
    while (GetMessage(&message, NULL, 0, 0))    // continuously loop for messages
    {
        DispatchMessage(&message);
    }

    return 0;
}

stdafx.h is a precompiled header file in which i include all the DirectX includes. Namely in C:\Program Files (x86)\Windows Kits\8.1\Include\shared;C:\Program Files (x86)\Windows Kits\8.1\Include\um;

I also include the C:\Program Files (x86)\Windows Kits\8.1\Include\shared;C:\Program Files (x86)\Windows Kits\8.1\Include\um; libraries located in the C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64

I'm using Visual Studio 2015, Windows 8.1 64bit. I followed this tutorial to create Directx application. Simply made a Win32 project, done these modifications in include and libs, pasted the code and built it properly. Running however does not output anything. It just says build succeeded. VS works with all my other projects. I have tried all configurations in x64 mode. If i had to guess i'd say that it is not finding a dll.. I cannot find the culprit.

Upvotes: 0

Views: 413

Answers (1)

Asesh
Asesh

Reputation: 3371

You have specified "MainWindow" as it's class name when registering window class but when creating window you have specified ""Main Window", so Windows was unable to find that class. Passing "MainWindow" as it's class name to CreateWindow will fix that issue:

window.lpszClassName = L"MainWindow";   // class name
window.style = CS_HREDRAW | CS_VREDRAW;

RegisterClassEx(&window);
HWND windowHandle = CreateWindow(L"MainWindow", L"DirectX Tut!", WS_OVERLAPPEDWINDOW,
    100, 100, 600, 800, NULL, NULL, hInstance, 0);

As shown above, you should use L as a prefix for wide character string literals

Upvotes: 1

Related Questions