I am writing code for a window in DirectX, but I experienced an error while compiling.
The error would pop up in a box that says “Exception thrown” alongside the following details:
Exception thrown at 0x00007FFAF7F7C8EA (ntdll.dll) in BlankWindow.exe: 0xC0000005: Access violation reading location
Why does this happen? I have no clue.
Here is my code:
#include <Windows.h>
#define MAX_NAME_STRING 256
#define hInstance() GetModuleHandle(NULL)
WCHAR WindowClass[MAX_NAME_STRING];
WCHAR WindowTitle[MAX_NAME_STRING];
int WindowWidth;
int WindowHeight;
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE,
LPSTR lpCmdLine,
INT nCmdShow
)
{
// Init global vars
wcscpy_s(WindowClass, TEXT("TutorialOneClass"));
wcscpy_s(WindowTitle, TEXT("myWindow"));
WindowWidth = 1280;
WindowHeight = 720;
// Create window class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
wcex.hIcon = LoadIcon(0, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(0, IDI_APPLICATION);
wcex.lpszMenuName = WindowClass;
wcex.lpszMenuName = nullptr;
wcex.hInstance = hInstance();
wcex.lpfnWndProc = DefWindowProc;
RegisterClassEx(&wcex);
// Create and display our window
HWND hWnd = CreateWindow(WindowClass, WindowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, WindowWidth, WindowHeight,
nullptr, nullptr, hInstance(), nullptr);
if (!hWnd)
{
MessageBox(0, L"Failed to create window! Sucks for you!", 0, 0);
return 0;
}
ShowWindow(hWnd, SW_SHOW);
// Listen for message events
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}