Results 1 to 2 of 2

Thread: Introduction to Win32 programming using C/C++

  1. #1
    Junior Member
    Join Date
    Dec 2002

    Introduction to Win32 programming using C/C++

    With all the many tutorials out there for Win32 programming, the reason I choose to start writeing my own was because it re-enforces what I previously learned, and for fun. Im not an expert however, if you notice any mistakes please let me know. Sugestions and comments are welcomed.

    Win32 API programming series
    Part 1


    The information provided within is intended to be used as a learning aid, and NOT to be used for Illegal activities. Also note that I am by no means a professional programmer and as a result I am not liable for any damages caused by using my code examples.

    Things needed:
    1) A 32 bit C and/or C++ compiler/linker
    2) An understanding of either C++ or C. (must know at least the basics)
    3) The windows.h header file, should be located inside your /include directory for your compiler.

    1) Integrated Development Environment, i.e. Dev-C++ (www.bloodshed.net)
    2) A copy of the Win32 Programmer's Reference available from http://www.friends-of-fpc.org/tools/api/win32/
    You can optionally use the MSDN website rather then downloading
    The Win32 Programmers Reference. http://msdn.microsoft.com/

    NOTE: We will not be using MFC or any of the available kits or graphics libraries.


    This first part in my tutorial on Win32 API programming series will focus on the fundamental principles, concepts and terminology involved in Windows programming. We will then dive more deeply and slowly uncover what we need to know about the inner workings of the Win32 API so that we can create Windows applications. With that said, lets begin.

    Let us first begin with the ever so popular hello world example, we will first take some sample code, compile it, then we will examine it. Since that is the way I like to do things. Here is an example that will open a message box and display Hello, World! GUI style.

    #include <windows.h>

    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hpInst, LPSTR lpszArgument, int nShowCommand)
    MessageBox(NULL, "Hello, World!", "Title Bar", MB_OK);
    return 0;

    This should compile with no problems, if you get any errors or warnings dont panic. First make sure you indeed do have a 32 bit compiler, and also ensure that you have a copy of the windows.h header file. If necessary look up the error messages in your compiler documentation.

    Assuming that you have compiled successfully, once you execute your program, a message box should be displayed with "Hello, World!" Feel free to experiment and change things. Now, you may have noticed a few things. First we do not have a main() but instead we have a WinMain. In a Windows program, the entry point is our WinMain, remember that DOS or console applications required main(). Since we are now programming using the Win32 API things are a bit different.

    As always you should look up new functions to get used to them. If you downloaded the Win32 Programmer's Reference you can use the index to look up the MessageBox function. The first thing you notice is MessageBox wants us to pass it a handle to the owner window. Since we do not have any owner window, which will be explained later, we pass it NULL for now. The next parameter is a pointer to a string spesifying the message to be displayed, as the next parameter is a pointer to a string spesifying the text to be displayed in the title bar. The last parameter, MB_OK is defined in windows.h, this is an unsigned integer that speifyes that our message box contains an ok button.

    For now you don't need to wory about the parameters of WinMain too much. Just know that hInst is a handle to the current application instance, this can be thought of as a pointer to the code that is being executed in memory. This parameter is often used as an argument to function calls that require it. hpInst is nolonger used and may be ignored. The WinMain parameters will become clearer as we begin to use them.

    We are finished with part 1. And although we did not get much programming done, and are still not familiar with all the new terms, we have learned a great deal.


    You should now be familiar with how to compile and link a Windows application. You should know that Windows programs begin execution at the WinMain function instead of main(). And you should have experimented with the MessageBox function and should be somewhat familiar with its parameters. Thetas it for the first part of this series.

    Please let me know if there are any errors. Also feel free to make suggestions and/or comments. Thank you.
    You never run out of things that can go wrong
    Quoted from:[glowpurple]Aston Shell[/glowpurple]

  2. #2
    Join Date
    Dec 2002
    Great! I'm looking forward to part2!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts