Posted September 30, 2009 by Spyros in C/C++ Programming

Windows API Programming Introduction


API stands for Application Programming Interface. This is in fact a set of routines and programming structures provided by libraries in order to help a developer code a program for an operating system. It could be created to be language specific or language independent. For instance, the windows api is created to be independent of what programming language we decide to use in order to program.

I’ll be describing how to go on with programming with the windows api and show you how to find what you want in the process.
The simplest win32 program you can ever find is probably something like:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
MessageBox(NULL, "Hello codercaste.com !", "Hi !", MB_OK);

If you are using Visual Studio like i do for programming such things for windows, specify a win32 c++ project. At first we include windows.h which is in fact the win32 api routines and structures. WinMain() is the main() equivalent of a c program, what we call the entry point. MSDN, the microsoft developer network holds everything we need to know about the windows api. It’s a great documentation and very easy to use. If you own visual studio you already have the msdn reference and i’d highly suggest that you install it locally on your system. However, you can view it online. For instance, WinMain’s reference can be found here at http://msdn.microsoft.com/en-us/library/ms633559(VS.85).aspx . It shows the below :


int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow

It also says many things about what the parameters of the function are. While for now it’s not essential to know more details about what all those are, i think it’s important to remembet than an instance is in fact a pointer to the module(executable,dll) in memory. GetModuleHandle is the function you may need sometime to get it. hPrevInstance is NULL for win32 programs, lpCmdLine is a string showing the command line arguments NOT showing the binary name. CmdShow is a value that can be passed to ShowWindow(), mostly used in order to hide or show an application window.

When inside our window main, we just call a simple messagebox with:

MessageBox(NULL, "Hello codercaste.com !", "Hi !", MB_OK);

MSDN shows :

int MessageBox(
HWND hWnd,
LPCTSTR lpCaption,
UINT uType

hWnd is the handle to window, meaning the window handle of our current window. We pass NULL, meaning the window that creates the process. lpText is the text that appears on the messagebox and lpCaption is the text that appears on the blue upper bar, called title bar. uType is the messagebox type, essentially the buttons and interface. If it has cancel and ok, just ok, or maybe an explamation icon. MB_OK is the simplest one that just has an OK button that we press to terminate.

ExitProcess does exactly what is implies. It exits the current process and its threads. The only parameter it uses, is the ExitCode which is a code that the process returns when it ends and most people tend to pass 0 or -1, but it has no real difference.