Win32 Programming – How to create a simple GUI based application?

By adding Graphical User Interface (GUI) to the applications we can change whole look and feel of the applications and users will feel more comfortable to access the GUI based applications than the text-based or console-based applications.

Win32 API provides vast number of API functions to create and deal with Graphical User Interface (GUI) applications. In this article I am going to create a simple GUI application using Win32 API in “Visual C++”.

Usually GUI based applications contains, windows, icons, controls on windows to accept user input and display the output. All these are part of the user-interface; hence we call them as user-interface elements. User-interface elements can able to display text, graphics or image(s).

Lets create our simple GUI application.

Step 1. The first step in creating our GUI application is, create a window. We use CreateWindow Win32 API to create our application window. The syntax of CreateWindow API looks like below:

HWND WINAPI CreateWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);

Where lpClassName is the name of the class, lpWindowName is the name of the window. dwStyle is the window style and x, y are the co-ordinates to position the window. nWidth, nHeight are the window width and height respectively. hwndParent is the parent window handle, hMenu is the menu handle of the window. lpParam is a pointer to one of the variations of CREATESTRUCT structure variable. Most of the parameters in this function are optional.

Upon success, CreateWindow function returns the window handle. Otherwise, it returns NULL as the return value.

The function call looks like below:

HWND hWnd = CreateWindow("", "My first GUI window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 200, 200, NULL, NULL, NULL, NULL);

From above code, we are trying to create a 200×200 window with the window title “My first GUI window”. We are trying to create a WS_OVERLAPPEDWINDOW and make it visible (WS_VISIBLE) when we run the application.

The code looks like below:

// sample.cpp
#include <windows.h>
#include <stdio.h>

int main()
    HWND hWnd = CreateWindow("", "My first GUI window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 200, 200, NULL, NULL, NULL, NULL);
    if (hWnd == NULL)
       printf("0x%x\n", GetLastError());

   return 0;

Observe that in the code we used main() function instead of WinMain(); just for simplicity. If we use main() function, console window is automatically attached to the application; so, we can use printf like functions to display the results on the console window.

Step 2. Now we need to compile and run our program. Lets compile it, to generate an executable file. Below is the command to compile our “sample.cpp” file.

c:\Visual Studio Projects 2012>cl sample.cpp /link User32.lib

Because we have used “CreateWindow” function in our code and it is referenced in “User32.lib” file, we need to link “User32.lib” library file when compiling our “sample.cpp” file. Otherwise, we will get below error message when compiling our “sample.cpp” file:

sample.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExA@48
referenced in function _WinMain@16

After successful compilation and linking “User32.lib” library file, the above command generates “sample.exe” file. Now its time to run our application “sample.exe”.

Lets run it; and observe that, our application will not display any window. It simply displays the error code “0x57f” on the console window.

Is there anything wrong we did in our code? Why our application didn’t display the GUI window?

Step 3. To figure out what went wrong, first we need to analyze the error code “0x57f” which is displayed on the screen when we run our application.

To analyze this, we use “Visual Studio 2012” tool “Error Lookup”. “Error Lookup” tool is used to lookup the error message which belongs to the error code; for example: in our case the error code is “0x57f”.

Once we type this error number “0x57f” in “Error Lookup” window, and click on “Lookup” button; “Error Lookup” tool will display the message “Cannot find window class.”. That means our application returns “Cannot find window class.” error. But why our application returns this error message? To understand this, first we need to understand what is window class and how it works.

Window class is nothing but a structure containing attributes and a window procedure to process window messages to control window behavior and appearance. We can use the same window class for multiple windows. We can use pre-defined window classes (eg: BUTTON, STATIC etc.,.) or we can create our own window classes. Before using a window class to creating GUI windows, first the window class has to be registered with Windows Operating System.

Remember that the first argument to our “CreateWindow” function is, a name of the window class. In our program we have passed an empty string. Actually we have to pass a valid window class name; either pre-registered or our own window class name. Because we passed an empty string; we got “Cannot find window class.” error when we run the application; that means we need to pass a valid window class name when calling “CreateWindow” Win32 API function.

I want to create our window with our own class instead of pre-defined window class names.

So, how to create our own window class? Lets discuss about creating our own window class and registering the class, in our next article.


Win32 Programming – How to create a simple GUI based application?

3 thoughts on “Win32 Programming – How to create a simple GUI based application?

Leave a Reply

Your email address will not be published.

Scroll to top