Simple Program using WinAPI

Hello World... Windows Style

    Introduction

    Setting up a Visual Studio Project, as shown in our earlier tutorial; we have learnt how to build a basic program which outputs into a Console display. As you know there are different types of programs you can develop in Windows and the one we are concentrating on in this article is writing a GUI (Graphical User Interface) Windows program using WinAPI (also commonly known as Win32, or Windows API). With this tutorial, there is a strong assumption that you know how to handle and manage Solutions and Projects with Visual Studio Express 2010, or any other version of Visual Studio; if you are unsure go to the Start Visual Studio C++ Project Tutorial. This program will be written in the C Programming Language

    WinAPI is a library which executes important functions which are running within Windows such as GUI, Threading, File Systems, Processes, Event and Input handling. We won't go straight into those subjects; as we will only go into the simple functions for now and that deals with GUI and event handling. Before all that we have to know how to set up a WinAPI based Windows program within Visual Studio Express.

    Setting up Windows Application

    First you have to open Visual Studio Express 2010, select File > New > Project. Not too dissimilar to setting up the project in our earlier tutorial. Once you get the New Project dialogue, select Win32 Project (See Image Below).

    Once you have named your project, which in this case will be SimpleWindowsProgram, you go to the next dialogue which you select Applications Settings on the left. Once you have done that on the Application Type in the main panel Select the Windows Application radio select button and make sure Empty Project is selected in the check box below (See Image Below).

    Writing the Code

    Once that is set up, we can proceed with the code. Creating our new source file, which will be a C extension source file. The core libraries within WinAPI are mostly in C, there are wrappers for other languages; but C is the best to use as it is as close to the hardware (without the complexities of Assembly and Machine Code). Writing code in C overall helps performance.

    When you are creating a new Win32 project you will be dealing with libraries and headers that interact with the main Windows kernel. The first thing you will need inside your code is the Windows Header File, which is windows.h. This header enables you to do the interacting with the kernel and access the GUI that we will using with our example code. It is a header file you cannot go without when writing Windows applications. The preprocessors inside Visual Studio are pointing to the main directories for headers and libraries so you will not need to do anything in regard to setting this up. Now we will go straight to the main program to go through the basics.

    Example Code In Detail

    First of all, as previously mentioned we include windows.h, it is best practise to do this first as all functions and subsequent headers that you will using in later tutorials will depend on it.

    #include <windows.h>
    		

    Now we will only do the main entry function, and you may have noticed unlike the section of example code before, this program does not use the more common int main function, we are using int WinMain as we are writing an Windows Application.

    //THE EASIEST WINAPI PROGRAM YOU WILL EVER DO!
    int WINAPI WinMain(HINSTANCE hInstance, 
    				   HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)

    As you can see, there are four function argument parameters inside the main entry. Because we are not passing commands through arguments; we do not need to concentrate on them at this point. Unless you are doing more sophisticated programs, it would be unnecessary to cover it for now. It will be covered more in-depth in later tutorials.

    {
    	//BRINGS UP THE MESSAGE DIALOGUE WINDOW WITH 'HELLO WORLD'
    	MessageBox(NULL, L"Hello World!", L"Reliant Code Says Hello World", MB_OK | MB_ICONASTERISK);
    	return 0;
    }

    Now we are inside the WinMain function (see above) and as you can see we will only call one simple function, which is the MessageBox function. This is a simple function which calls up a basic Dialogue Window; it is easy to begin with, but in later tutorials you will see where it can be best utilised. Inside MessageBox there are four argument parameters. The first parameter is left blank as there is no window handle to control this Message Dialogue (A Window Handle is a crucial component in Windows Programming as we will go into more depth into later tutorials). The second parameter is where you place the main body of text inside your dialogue window. You may have noticed that the string begins with a capital L, which denotes that the function is using wide chars (wchar_t/LPCTSTR data types). Converting it from a normal char string to a wide-char string, will enable you to use a more diverse character set (for example other different alphabet types, coded characters,etc) than those that are allowed in the normal ASCII character set (normal char/LPCSTR data types). For the last few years, Visual Studio now by default handles wide character strings, you can change that by going into the menu and select Project > (ProjectName) Properties > at the bottom of the menu. Once you open the Project Properties dialogue, select Configuration Properties > General. Go to Character Set, and then the drop-down list and select Not Set. The image below shows how to do this; this is only an option you may want to take. For these tutorials that won't be necessary.

    The third parameter is the where you enter a caption title for the Dialogue Window, which is located in the top bar. Same as before, it uses a wide character string. And the last parameter are the flags which you set for the dialogue window; there are a plethora of options for this as it describes which buttons you want to place in your dialogue and which icon you may want to use. The flags are simple integers, which you enter as many macros that you will need. They are predefined inside windows.h has all the macros that will be used. The flags in this example will be MB_OK, which allows the dialogue only to deploy an OK button; you can place other button options with other flags, but we only need one button for this example. And the second flag is MB_ICONASTERISK, which displays an lower case i icon inside the dialogue window, you can also display an warning or error icon; but those would be unnecessary for this program.

    As the image shows above, it is a simple program which allows you place a simple message. Once you have clicked on the OK button, the program will continue and close safely.

    Summary

    Now you know how to do a simple Windows API version of 'Hello World'. With this you understand how to gain access to the functions that are within Windows and display a fully functional application. This is only the beginning, but least you know how to set up a Windows program for yourself. Experiment with the code accompanied with this tutorial, so you can mess around with the flags and strings within the MessageBox function call. This will allow you to gain more knowledge on how these programs function.

    /*
            SIMPLE WINDOWS PROGRAM     main.c      Reliant Code
            Writing a Simple WinAPI Hello World for Windows
    */
    
    #include <windows.h>
    
    //THE EASIEST WINAPI PROGRAM YOU WILL EVER DO!
    int WINAPI WinMain(HINSTANCE hInstance, 
    				   HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
    	//BRINGS UP THE MESSAGE DIALOGUE WINDOW WITH 'HELLO WORLD'
    	MessageBox(NULL, L"Hello World!", L"Reliant Code Says Hello World", MB_OK | MB_ICONASTERISK);
    	return 0;
    }
    blog comments powered by Disqus
    Download Files Here

    PDF Version Here

    Download this Article in PDF

    Feedback on Article

    Report Bugs, Errors or Send Feedback

    SimpleWindowsProgram_MSVC2010

    Windows File - File Size 3.25 KB

    MD5 Hash Check : What is MD5?