WINAPI Programming/Getting Started

From Wikiversity
Jump to navigation Jump to search

A simple Win32 Program...[edit | edit source]

Make sure you specify a Win32 GUI and NOT a console program when compiling the project below.

#include <windows.h>

 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev,  LPSTR lpCmd, int nShow)  
    MessageBox (NULL, TEXT("Hello, Windows!"), TEXT("Hello"), MB_OK);  
    return 0; 

If you have no experience with the Windows API try to compile and run the above program. First check for any errors and correct them as necessary. If you are not sure how to correct the errors look them up in the accompanying documentation that came with your compiler. Remember to compile this as a C program by saving the program as somename.c or anyname.c as most compilers will compile the project as a C program if .c follows as the extension. If the program ran correctly you should have gotten a simple message box that displayed the Message Hello Windows! and Hello in the client area and title bar respectfully.

Explaining the code...[edit | edit source]

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev,  LPSTR lpCmd, int nShow)

WinMain is the same as the main() function in dos. Program execution starts in WinMain(). HINSTANCE hInst is the handle to the programs executable module in memory. HINSTANCE hPrev is always NULL for Win32 Applications, it use to be the handle to the previous instance for Win16 programs. LPSTR lpCmd are the command line arguements as a single string not counting the program name. hInstance is used for loading resources and any other task which is performed on a per-module basis. A module is either the EXE or a DLL loaded into your program. For purpose of this tutorial there will be only one module, the .exe. The calling convention is WINAPI which is defined as _stdcall.

Winapi Home

Questions[edit | edit source]

Questions and comments here...