WinMain() and the Windows Procedure

By RoD
Every Windows programming needs a main entry point. That being said, you may be wondering what this entry point is. The main entry point for any Windows program is called WinMain. The function prototype for WinMain is a little confusing at first, but as we continue to work with it you'll notice it becomes much easier to understand. Well. we've told you what it is; now were going to show you! Here's the prototype for WinMain:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);


As you may have already noticed, the return type for WinMain is, and always will be, int. All 32-bit Windows operating system applications use the calling convention WINAPI. This calling convention MUST be used to distinguish the function as the entry point. Now for the parameters. As you can see, WinMain uses four parameters when the program starts. Let's have a look at them, shall we?
hInstance - is a handle to your applications instance, where an instance 
can be considered to be a single run of your application. The instance 
is used by windows as a reference to your application for event handling,
message processing, and various other duties.
hPrevInstance - is always NULL. 
lpCmdLine - is a pointer string that is used to hold any command-line
arguments that may have been specified when the application began.  
For example, if the user opened the Run application and typed myapp.exe
myparameter 1, then lpCmdLine would be myparameter 1.
nShowCMD - is the parameter that determines how your application's window
will be displayed once it begins executing.
Pretty simple, right? Don't worry if it's confusing, it will make sense soon enough! The Windows program you create is going to need some way to handle the messages that Windows will send to it. A few examples of these messages are: WM_CREATE, WM_SIZE, and WM_MOVE. There are TONS of these messages, and we'll show you how to handle a large number of them. To allow Windows to communicate with your application, we'll create a dandy little function called a Windows procedure. The most common name for this function is WndProc. This function MUST be created and used to determine how your application will respond to various events. The Windows procedure may also be called the event handler because, well, it responds to Windows events! So let's have a quick look at the prototype:
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
This function is declared with a return type of LRESULT CALLBACK. The LRESULT type is used by Windows to declare a long integer, and CALLBACK is a calling convention used with functions that are called by Windows. The Windows Procedure is a function pointer, which allows you to call it whatever you want because the function's address will be assigned as a function pointer upon creation of the window class.
hwnd - Only important if you have several windows of the same class open
at one time. This is used to determine which window hwnd pointed to before 
deciding on an action.
message - The actual message identifier that WndProc will be handling.
wParam and lParam - Extensions of the message parameter. Used to give 
more information and point to specifics that message cannot convey on its own.
Well now you should have a better understanding of these two topics. You may be wondering when you get to see some openGL usage, and the answer is soon. We first need to cover the basics, let's remember, we're here to learn!
Previous: Introduction to Windows Programming
Next: A first Windows application
Back to OpenGL tutorial index