Win32 API – Introduction
The Windows API (also known as Win32) is the core set of Microsoft Windows native APIs used by desktop applications. It provides functions, structures, macros, and constants for tasks such as window management, file I/O, memory management, threading, synchronization, and more.
Key Concepts
- Handles – opaque references to OS objects (e.g.,
HWND
,HANDLE
). - Message Loop – the central event-processing mechanism for GUI applications.
- Modules – functions are exposed through DLLs like
kernel32.dll
,user32.dll
, andgdi32.dll
. - Unicode vs ANSI – most APIs have a
W
(wide) andA
(ANSI) version.
Hello World Example
A minimal Win32 program that creates a window and displays "Hello, World!".
#include <windows.h> LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); TextOutW(hdc, 10, 10, L"Hello, World!", 13); EndPaint(hwnd, &ps); } return 0; } return DefWindowProcW(hwnd, uMsg, wParam, lParam); } int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow) { const wchar_t CLASS_NAME[] = L"SampleWindowClass"; WNDCLASSW wc = { }; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClassW(&wc); HWND hwnd = CreateWindowExW( 0, CLASS_NAME, L"Win32 Hello World", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL); if (hwnd == NULL) return 0; ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); MSG msg = { }; while (GetMessageW(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessageW(&msg); } return (int)msg.wParam; }
Further Reading
Explore the sections below to dive deeper into the Win32 API.