A Windows Hello World

In this section we will write our first Windows program.

The Hello World

I am sure all programmers have come across the traditional Hello World program. This is where you write a program that says Hello World, when you write your first program for a new programming language.

WinMain

As we are using Windows, our Hello World will be displayed with a message box. When coding C/C++ for DOS, our main function is called main(). However with Windows, its called WinMain, and is declared like this:

int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)

Okay, lets break this up step-by-step to gain an understanding of what it all means.

The first part, int, is straight forward; the routine returns an integer value to the operating system. If the WinMain is terminated before the program enters its message handling loop, it will return a 0. Otherwise, it will return whatever value is stored in the WM_QUIT messages parameter.

The next part, PASCAL, declares Pascal-style parameter-passing conventions when applied to a function header (which means the first parameter is pushed onto the stack first; the called fucntion cleans up the stack). I know, that is hard to understand, and I am working on a better way to explain this.

Then we have WinMain, which is the name of our main() function.

The next , hInstance and hPrevInstance, are of data type HINSTANCE. This is a handle to an instance, or occurence, of an application. The first, hInstance, is a handle to the current instance of the aplication, hPrevInstance is a handle to the previous instance of the application. For a Win32 program, this will always be NULL.

lpszCmdParam points to a string specifying any command line parameters passed to the application.

nCmdShow Specifies how the window is to be shown. This parameter can be one of many values, such as SW_SHOW, which activates a window and displays it in its current size and position.

Message Boxes

Now that we all understand that, we can look at the MessageBox function.

int MessageBox(HWND hWnd, LPCTSTR lpText,LPCTSTR lpCaption, UINT uType );

The first, int, returns an integer value that is 0 if there is insufficient memory to display the box, or an integer value that corresponds to the button pressed, eg IDOK means OK was pressed.

The next parameter, hWnd, of type HWND, is the handle of the Window which owns the message box. A better way to say this it is the handle, or reference, of the window over which the message box will appear.  If this parameter is NULL, the box has no owner, it will appear on the screen.  We will use the function GetActiveWindow() to find the handle of the active window. This function takes no parameters, and returns the HWND of the window over which to display the message box.  If there is no window active, it will return a NULL value.

The next 2 paremeters for MessageBox() are strings used for the message being displayed and title of the box respectively. However, if the second one is NULL, Error is used as the default title.

The final parameter, uType, specifies the buttons that appear on the box, and what values are returned.  This value can be a combination of many values. For example, MB_OK will dispay the message box with an OK button.

So, now we now how to setup a WinMain and display a message box. Congratulations, you have created your first Windows program.

Download source code and executable here

Back