Tutorial: How to write 'Hello World' using Win32 api

WinBoot

Registered Member
Joined
Aug 25, 2010
Messages
62
Reaction score
14
Everyone probably knows the 'Hello world' code when you started coding.
It is the first turorial you should read whenever you start a new
coding language.

As i haven't read any 'Hello world' example written for use in Windows,
i thought i might just write my own tutorial, so here it is:

First of all we include the windows handler
Code:
#include <windows.h>

Next thing we do is hooking the main function, which is called
WinMain for use in Windows. And save the instance outside the function
Code:
HINSTANCE hInst;
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	hInst = hInstance;
	return 0;
}

This is the main function we are going to use, now in this function, we have
to call a function which sets up our main window. We also have to leave the window open
as long as we didn't exit it and keep it updated. So now the main function looks like this:
Code:
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
	MSG msg;
	hInst = hInstance;

	initApplication( hInst );

	while( GetMessage( &msg, 0, 0, 0 ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}

	return msg.wParam;
}

In the function initApplication which we call in WinMain, we will have to initialize the main window.
Code:
void initApplication( HINSTANCE hInstance )
{
	WNDCLASS wC; // Defines the class

	wC.cbClsExtra = 0;
	wC.cbWndExtra = 0;
	wC.hbrBackground = ( HBRUSH ) GetStockObject( LTGRAY_BRUSH ); // Choose background color
	wC.hInstance = hInst; // Mention the instance
	wC.hCursor = LoadCursor( NULL, IDC_ARROW ); // Load the cursor
	wC.hIcon= LoadIcon( NULL, IDI_APPLICATION ); // Load the icon
	wC.lpszClassName = "Main"; // Name of the window
	wC.lpfnWndProc = ( WNDPROC ) MainProc; // The window process function
	wC.lpszMenuName = NULL;
	wC.style = CS_HREDRAW | CS_VREDRAW;

	RegisterClass( &wC ); // Register the class

	// Setting up the position and size
	int x = 300;
	int y = 300;
	int w = 300;
	int h = 300;

	// Creating the actual window
	Main = CreateWindow( "Main", "Main", WS_OVERLAPPEDWINDOW, x, y, w, h, 0, 0, hInstance, 0 );

	ShowWindow( Main, SW_SHOW ); // Displays the main window
	UpdateWindow( Main ); // Update it
}

Now we have to declare the process function and write it.
Code:
LRESULT APIENTRY MainProc( HWND, UINT, WPARAM, LPARAM ); // Definition
Code:
LRESULT APIENTRY MainProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) // Actual process function
{
	#define HELLOWORLD 1

	switch ( msg )
	{
		case WM_CREATE: // When WM_CREATE is used, you can create buttons, etc
		{
			CreateWindow( "Static", "Hello World", WS_CHILD | WS_VISIBLE, 50, 50, 100, 20, hwnd, ( HMENU ) HELLOWORLD, hInst, 0 ); // Create the static
		}
		break;

		case WM_DESTROY: // If WM_DESTROY is used, you should leave the program
		{
			ExitProcess( 0 );
		}
		break;

		default:
			return DefWindowProc( hwnd, msg, wParam, lParam );
	}

	return 0;
}

Project is attached,
Enjoy!
 

wacked

Newbie
Joined
Jul 17, 2010
Messages
19
Reaction score
2
....
Next thing we do is hooking the main function, which is called
WinMain for use in Windows.
-----
No just no. hooking is very different.
--------
And save the instance outside the function
Code:
HINSTANCE hInst;
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
    hInst = hInstance;
    return 0;
}
This is the main function we are going to use, now in this function, we have
to call a function which sets up our main window.
-------
No. We could do this in WinMain.
------
We also have to leave the window open
as long as we didn't exit it and keep it updated. So now the main function looks like this:
Code:
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
    MSG msg;
    hInst = hInstance;

    initApplication( hInst );

    while( GetMessage( &msg, 0, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    return msg.wParam;
}
In the function initApplication which we call in WinMain, we will have to initialize the main window.
Code:
void initApplication( HINSTANCE hInstance )
{
    WNDCLASS wC; // Defines the class

    wC.cbClsExtra = 0;
    wC.cbWndExtra = 0;
    wC.hbrBackground = ( HBRUSH ) GetStockObject( LTGRAY_BRUSH ); // Choose background color
    wC.hInstance = hInst; // Mention the instance
    wC.hCursor = LoadCursor( NULL, IDC_ARROW ); // Load the cursor
    wC.hIcon= LoadIcon( NULL, IDI_APPLICATION ); // Load the icon
    wC.lpszClassName = "Main"; // Name of the window
    wC.lpfnWndProc = ( WNDPROC ) MainProc; // The window process function
    wC.lpszMenuName = NULL;
    wC.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass( &wC ); // Register the class

    // Setting up the position and size
    int x = 300;
    int y = 300;
    int w = 300;
    int h = 300;

    // Creating the actual window
    Main = CreateWindow( "Main", "Main", WS_OVERLAPPEDWINDOW, x, y, w, h, 0, 0, hInstance, 0 );

    ShowWindow( Main, SW_SHOW ); // Displays the main window
    UpdateWindow( Main ); // Update it
}
Now we have to declare the process function and write it.
Code:
LRESULT APIENTRY MainProc( HWND, UINT, WPARAM, LPARAM ); // Definition
Code:
LRESULT APIENTRY MainProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) // Actual process function
{
    #define HELLOWORLD 1

    switch ( msg )
    {
        case WM_CREATE: // When WM_CREATE is used, you can create buttons, etc
        {
            CreateWindow( "Static", "Hello World", WS_CHILD | WS_VISIBLE, 50, 50, 100, 20, hwnd, ( HMENU ) HELLOWORLD, hInst, 0 ); // Create the static
        }
        break;

        case WM_DESTROY: // If WM_DESTROY is used, you should leave the program
        {
            ExitProcess( 0 );
        }
        break;

        default:
            return DefWindowProc( hwnd, msg, wParam, lParam );
    }

    return 0;
}
The {} on case are unnecessary.
...
Also you should use UNICODE. #define UNICODE, #DEFINE _UNICODE, TCHAR szCoolString[]=TEXT("Cool String. w00t w00t");
 

ExobiT

Junior Member
Joined
Apr 21, 2008
Messages
167
Reaction score
31
What a shi*tload of crap for just writing "hello world" in python it can be done in 1 line in C it can be done in 5-6 lines..
 

WinBoot

Registered Member
Joined
Aug 25, 2010
Messages
62
Reaction score
14
Offcourse it can, but that is not what this tutorial is about is it
 

MaDeuce

Newbie
Joined
Oct 24, 2008
Messages
45
Reaction score
16
WinBoot, you are trying to contribute something to the community, which is admirable. Good job. Thank you. I'm not trying to hijack your thread, but I couldn't resist; forgive me.

Your tut was a real eye-opener for me and had more or less the same reaction as ExobiT. I'm not criticizing your work at all, but ExobiT's point should be well-taken by anyone who actually needs a 'hello, world' for Win32. In other words, for someone that is just getting started.

Just to give readers a feel for ExobiT's point, here are two 'hello, world' one-liners:

First, without any windows (UI windows of any sort, not MS Windows):
Code:
python -c "print 'hello, world'"

Now with all the extra overhead that a UI with windows creates:

Code:
python -c 'from Tkinter import *;r=Tk();r.geometry("200x50");a=Frame(r);a.grid();l=Label(a,text="hello, world");l.grid();r.mainloop()'

That really is the equivalent of your example.

Finally, how about a web server implemented in one line:

Code:
python -m SimpleHTTPServer 8080

which will create a server for the current directory at hxxp://localhost:8080

Most scripted languages such as perl, TCL/TK, etc., provide similar leverage.
 
Top