Blog Archive

Wednesday, May 5, 2010

Win32 API Hello World In C

Those of you want to write programs for windows, this is a entry point tutorial of windows api.
You should familiar with console application. You would also know what is a main() function in c. main() is entry function of any c program. main() is the super most function in a c application. It is called by OS(which is DOS in case of dos console application).
following is the hello world program for console.

int main()
printf("Hello World.");
But in windows application there is many differences.
Now lets see windows version of hello world in c.

int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrenIns, LPSTR szCmdLine, int iCmdShow)
MessageBox(NULL,TEXT("Hello World"),TEXT("Windows Message."),0);
return 1;

You must wondering what all the keywords does means. Basically you need good command over c with detailed understanding of Visual C (It is basically c language, but microsoft redefined its various parts).

Following is a brief description of above program.

Windows uses WinMain() instead of main() for entry point of windows application.

WINAPI is a macro defined in windows.h it just tell the compiler that this is a api function and will going to be called by OS.
definition of WINAPI is
#define WINAPI __stdcall

There are four parameters of WinMain.

HINSTANCE is data type defined with typedef. HINSTANCE is a unsigned int.
size of HINSTANCE is 4 bytes.

First parameter is a number, which identifies current Application(HINSTANCE is call window handle).

Second parameter is handle to previous application. This parameter is here because of historical reasons. In Old days every application in windows environment had a window handle to previous application. But now in new versions of windows this is always 0.

Third parameter is LPSTR which is a long pointer to string. This is a string containing command line arguments passed to application.

Fourth parameter which is an int, is related to applications initial status.

MessageBox just display a dialog box with a button. TEXT macro is used for conversion between a unicode and single byte string, because windows is always uses unicode internally for strings.

How To Compile:
To compile above code you need a c compiler capable of compiling win32 application.
You could use dev c++ our microsoft vc++.

In My Example I Have Used visual c++.
If you have visual c++ installed on your computer go to.

start menu> all programs> visual c++ redistributable(if you have express edition.)OR microsoft visual studio>Visual Studio tools> Visual Studio Command prompt.

Now a console window will appear, now type following command.

edit window.c

Now a editor will open. Type the code given above and close the editor.

To compile window.c type following command
and press enter.

cl window.c user32.lib

Following out put will be generated.

Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.


To Run Program just type window and press enter.
A dialog box containing hello world message will appear.

1 comment: