Win32 API Reference

Introduction to Win32

The Win32 API (Application Programming Interface) is a collection of functions and data structures that form the core of the Microsoft Windows operating system. It provides a consistent way for applications to interact with the operating system's services, including:

  • Window Management: Creating, displaying, and managing windows, dialogs, and controls.
  • Graphics Device Interface (GDI): Drawing text, shapes, and images.
  • Memory Management: Allocating and freeing memory.
  • Process and Thread Management: Creating and managing processes and threads.
  • File System Operations: Reading from and writing to files and directories.
  • Registry Access: Reading and writing configuration data.
  • Networking: Communicating over networks.

This reference provides documentation for commonly used Win32 API functions, categorized for easier navigation. Understanding the Win32 API is crucial for developing native Windows applications.

Tip: Most Win32 API functions are C-based and typically called from C, C++, or other languages that can interface with C libraries.

Key Concepts

Handles

A handle is an application-defined value that uniquely identifies an object within a process. Objects can include windows, files, memory blocks, etc. Handles are opaque and should not be interpreted directly; they are used by the system to refer to the actual object.

HANDLE hFile = CreateFile(...);
// ... use hFile ...
CloseHandle(hFile);

Data Types

The Win32 API defines a rich set of data types, often prefixed with 'LP', 'P', 'H', 'W', 'L', 'BOOL', etc. Common examples include:

  • HANDLE: A unique identifier for an object.
  • HWND: A handle to a window.
  • DWORD: A 32-bit unsigned integer.
  • LPCSTR: A pointer to a constant null-terminated string (ANSI).
  • LPCWSTR: A pointer to a constant null-terminated string (Unicode).
  • BOOL: A boolean value (TRUE or FALSE).

Error Handling

Many Win32 functions return a specific value (often NULL, FALSE, or 0) to indicate failure. The specific error code can then be retrieved using the GetLastError() function.

BOOL success = SetWindowText(hwnd, L"My Window Title");
if (!success) {
    DWORD error = GetLastError();
    // Handle the error, e.g., outputting error message
    wprintf(L"Failed to set window text. Error code: %lu\n", error);
}

Commonly Used Functions (Examples)

CreateWindowEx

Creates an extended window.

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

Parameters:

dwExStyle: Extended window styles.

lpClassName: The registered class name.

lpWindowName: The window's text.

dwStyle: Window styles.

x, y, nWidth, nHeight: Geometry of the window.

hWndParent: Handle to the parent window.

hMenu: Handle to a menu.

hInstance: Handle to the application instance.

lpParam: Pointer to window-creation data.

Return Value:

If successful, the return value is the handle to the new window. Otherwise, it is NULL.

MessageBox

Displays a message box.

int MessageBox(
  HWND      hWnd,
  LPCTSTR   lpText,
  LPCTSTR   lpCaption,
  UINT      uType
);

Parameters:

hWnd: Handle to the owner window.

lpText: The message to be displayed.

lpCaption: The title of the message box.

uType: The contents and behavior of the message box.

Return Value:

If the function succeeds, the return value is one of the common message box return values (e.g., IDOK, IDCANCEL).

GetMessage

Retrieves messages from the calling thread's message queue.

BOOL GetMessage(
  LPMSG lpMsg,
  HWND  hWnd,
  UINT  wMsgFilterMin,
  UINT  wMsgFilterMax
);

Parameters:

lpMsg: Pointer to a MSG structure that receives message information.

hWnd: Handle to the window whose messages are to be retrieved.

wMsgFilterMin: Minimum message value.

wMsgFilterMax: Maximum message value.

Return Value:

If the function retrieves a message other than WM_QUIT, the return value is nonzero. If the function retrieves the WM_QUIT message, the return value is zero. If there is an error, the return value is -1.

Resources

The Win32 API is vast. For comprehensive documentation, please refer to the official Microsoft documentation:

Note: This page provides a simplified overview and examples. The full Win32 API contains thousands of functions and complex structures.