Win32 Tutorials


WinMain Function

Tutorials > Win32 > WinMain Function

View Full Source


As in a normal C++ console program, we need to start the program by having a main function. When creating a Win32 program, this function is created differently. It is called the WinMain function.

Contents of main.cpp :

First of all, we have to include the windows.h header file. This header file contains all declarations necessary for a large number of tasks available in the Win32 API. Every Win32 application you make, will include this header file.

#include <windows.h>

The WinMain function below may look quite daunting so we'll discuss it word for word.

As with the normal main function, an integer must be returned as an error code.

The WINAPI macro is used as a calling convention for system calls. If you leave this out, you will receive a compiler warning. As the WinMain is a function call, you need to include the WINAPI macro.

The HINSTANCE data type is a handle to the instance of an object. In Win32 programming, most objects have a HANDLE which is used to identify an object.

The first parameter is a handle to the current instance of your application.

The second parameter is a handle to the previous instance of your application. This is not used and is always given the value of NULL.

An LPSTR is a pointer to a character string. If you hold your cursor over the LPSTR in Visual Studio, you will see that it is actually just a typedef.

The third parameter is a pointer to a null terminated string holding the command line passed to the program excluding the program name. This is similar to your argv in the main function.

The last parameter indicates how the window should be shown eg. maximized, minimized, hidden, etc. You may have seen in windows how you can view the properties of an exe and change how it must be started.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		LPSTR lpCmdLine, int nShowCmd)

A value of 0 is returned as per normal to indicate that no error has occurred.

	return 0;

Congratulations. You have begun the journey on towards Win32 development. You will notice that when you run the program, nothing happens. Win32 programs do not run in a console window so none will be displayed. The next tutorial will show you how to display something on the screen.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++

< Tutorial 02 - Setting Up Your Environment Tutorial 04 - Message Boxes >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer