Windows API Reference

Application Architecture

This section provides an overview of the fundamental architectural components and concepts that underpin Windows applications. Understanding these elements is crucial for developing robust, efficient, and scalable software on the Windows platform.

Core Concepts

Application Models

Windows supports various application models, each with its own development paradigms and capabilities:

Inter-Process Communication (IPC)

Mechanisms for communication and data sharing between different processes:

System Services

Key system services that applications rely on:

Example: Process and Thread Creation

The following C++ snippet demonstrates a basic example of creating a new process and a thread within that process using the Win32 API:


#include <windows.h>
#include <iostream>

DWORD WINAPI ThreadFunction(LPVOID lpParam) {
    std::cout << "Hello from a new thread!" << std::endl;
    return 0;
}

int main() {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    // Start the child process.
    if (!CreateProcess(
        NULL,   // No module name (use command line)
        "notepad.exe", // Command line
        NULL,   // Process handle not inheritable
        NULL,   // Thread handle not inheritable
        FALSE,  // Set handle inheritance to FALSE
        0,      // No creation flags
        NULL,   // Use parent's environment block
        NULL,   // Use parent's starting directory
        &si,    // Pointer to STARTUPINFO structure
        &pi )   // Pointer to PROCESS_INFORMATION structure
    ) {
        std::cerr << "CreateProcess failed (" << GetLastError() << ")." << std::endl;
        return 1;
    }

    std::cout << "Created Notepad process with PID: " << pi.dwProcessId << std::endl;

    // Create a thread in the parent process
    HANDLE hThread = CreateThread(
        NULL,              // Default security attributes
        0,                 // Default stack size
        ThreadFunction,    // Function to be executed
        NULL,              // Parameter for the thread function
        0,                 // Default creation flags
        NULL);             // Thread identifier

    if (hThread == NULL) {
        std::cerr << "CreateThread failed (" << GetLastError() << ")." << std::endl;
        return 1;
    }

    std::cout << "Created a new thread." << std::endl;

    // Wait until child process exits.
    WaitForSingleObject(pi.hProcess, INFINITE);

    // Wait for the created thread to finish
    WaitForSingleObject(hThread, INFINITE);

    // Close process and thread handles.
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    CloseHandle(hThread);

    std::cout << "Exiting." << std::endl;
    return 0;
}