WndProc is Undefined

I'm reading through a DirectX 11 book and with a block of code within the book keeps throwing me an error...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;


I've tried fixing the problem myself, but I'm still fairly new to C++ so not sure if it's a good idea getting into graphics at an early stage or not, but wanted to give it ago at least.
closed account (E0p9LyTq)
You need to declare and define a global function, your window procedure. The function that handles all the messages Windows sends to your program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include	<windows.h>

// FUNCTION PROTOTYPES =========================================================
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

// entry point for a Windows application =======================================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nWinMode)
{
   // define some variables
   char     szWinName[]  = "Win32App";
   char     szAppTitle[] = "Win32 API Skeletal Application";
   HWND     hwnd;
   MSG      msg;
   WNDCLASS wc;

   // define a window class
   wc.hInstance     = hInstance;                         // handle to this instance
   wc.lpszClassName = szWinName;                         // window class name
   wc.lpfnWndProc   = WndProc;                           // pointer to window proc
   wc.style         = 0;                                 // default style
   wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);   // predefined icon
   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);       // predefined cursor
   wc.lpszMenuName  = NULL;                              // no class menu
   wc.cbClsExtra    = 0;                                 // no extra info needed
   wc.cbWndExtra    = 0;                                 // no extra info needed
   wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);       // predefined color brush

   // register the defined window class
   if (RegisterClass(&wc) == 0)
   {
      // an error occurred, abort the program
      MessageBox(NULL, "Couldn't Register the Window Class!", "ERROR", MB_OK | MB_ICONERROR);
      return 0;
   }
	
   // now that a window class has been registered, create the main window
   hwnd	= CreateWindow(szWinName,           // name of window class to create
                        szAppTitle,          // window title bar caption
                        WS_OVERLAPPEDWINDOW, // window style - normal
                        CW_USEDEFAULT,       // X coordinate - let Windows decide
                        CW_USEDEFAULT,       // Y coordinate - let Windows decide
                        CW_USEDEFAULT,       // width - let Windows decide
                        CW_USEDEFAULT,       // height - let Windows decide
                        NULL,                // no parent window
                        NULL,                // no menu
                        hInstance,           // handle to this instance
                        NULL);               // no additional arguments

   // check to see if window was successfully created
   if (hwnd == NULL)
   {
      // an error occurred, abort
      MessageBox(NULL, "Couldn't Create the Main Window!", "ERROR", MB_OK | MB_ICONERROR);
      return 0;
   }

   // display (and update) the newly created window
   ShowWindow(hwnd, nWinMode);
   UpdateWindow(hwnd);

   // create the message loop
   while (GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);  // translate keyboard messages
      DispatchMessage(&msg);   // return control to Windows
   }

   return msg.wParam;
}


// processes the messages that Windows sends to the application ================
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   // choose which Windows messages you want to use
   switch(message)
   {
      // the window is being destroyed, so terminate the application
      case WM_DESTROY:
         PostQuitMessage(0);
         return 0;
   }

   // let Windows process any messages not specified in the switch statement
   return DefWindowProc(hwnd, message, wParam, lParam);
}
closed account (E0p9LyTq)
I suggest you get a book (or more) on creating Windows applications first, before you jump into DirectX. The Windows API is quite massive by itself, and can do graphics natively.

Though a DirectX app doesn't use a lot of the Win32 API. Never hurts to have a solid foundation first.

This is a classic "bible" for learning the Win32 API, if a bit outdated:

Programming Windows®, Fifth Edition (Developer Reference)
https://www.amazon.com/Programming-Windows%C2%AE-Fifth-Developer-Reference/dp/157231995X/ref=sr_1_4?s=books&ie=UTF8&qid=1517852127&sr=1-4&keywords=charles+petzold
Topic archived. No new replies allowed.