Windows Program and Memory Interface: Win32 Handles, VirtualAlloc and Shared Memory

Follow one Windows machine through process creation, page reservation and commitment, and a shared mapping. The worked traces separate IDs, handles, addresses and cleanup.

KnowledgeGate Team

Exam prep & CS education

Updated 22 Nov 20266 min read
Windows Program and Memory Interface: Win32 Handles, VirtualAlloc and Shared Memory

A Windows program sees functions, handles and virtual addresses, while the OS manages processes, kernel objects and mapped pages. Confusing those layers turns a PID into a handle, a reserved page into a committed page, or two virtual addresses into two shared-memory objects. Assume Worker.exe runs on one Windows machine: its parent's handles are counted; six 4 KiB pages are reserved and three committed; then a 12 KiB mapping is viewed by two processes at different bases. Windows OS Architecture Explained: User Mode, Kernel Mode and a Worked I/O Trace follows an I/O request across user and kernel mode. Application-facing questions instead track the resource being acquired, its process-local reference, its byte or page range, and its release or synchronization rule.

Windows program and memory interface

The programming interface contains documented operations for requesting OS work. A Win32 function is the application contract. A kernel object is OS-managed; a handle is one process's reference to it; a virtual address belongs to one process's address space.

Program wants to

Documented operation

Value returned or used

Resource involved

Start a process

CreateProcessW

PROCESS_INFORMATION with process and thread handles plus IDs

Process and primary thread

Reserve or commit pages

VirtualAlloc

Base virtual address

Calling process's address space

Create shared backing

CreateFileMappingW

Mapping-object handle

File mapping object

Expose backing as addresses

MapViewOfFile

View base address

Mapped view

Release a handle or view

CloseHandle or UnmapViewOfFile

Success or failure

Caller's reference or view

Windows Operating System: Architecture, Processes, Memory and Worked Examples supplies the broader architecture, scheduling, address-translation and security map. API-level problems begin after an application chooses CreateProcessW, VirtualAlloc or file mapping. One Win32 function need not correspond to one system call, so classify the resource, reference type, state, size and cleanup separately. PID, handle and pointer are not interchangeable.

Worked process launch: IDs, handles and lifetime

Assume these trace values. Parent Launcher.exe, PID 4100, starts with log-file handle 0x40. It calls CreateProcessW for C:\Apps\Worker.exe with mutable command line "C:\Apps\Worker.exe" --blocks 3, bInheritHandles=FALSE, flags 0, and valid STARTUPINFOW and PROCESS_INFORMATION. Success supplies child PID 4176, primary-thread ID 4180, process handle 0xA4 and thread handle 0xA8 to the parent.

  1. Before: 1 open handle, 0x40.

  2. After success: 1 + 2 = 3, handles 0x40, 0xA4, 0xA8.

  3. Close 0xA8: 3 - 1 = 2.

  4. Wait through 0xA4; the child exits with code 0; 0xA4 remains valid.

  5. Close 0xA4: 2 - 1 = 1.

The child does not inherit 0x40. The returned process and thread handles must be closed. Success does not mean the child has initialized or exited. PID 4176 identifies it system-wide during its lifetime; 0xA4 is the parent's access-controlled reference and could differ elsewhere.

Worked VirtualAlloc trace: free, reserved and committed pages

VirtualAlloc page map showing six reserved pages, three committed, P3 decommitted, then all released.

Assume GetSystemInfo returns 4 KiB pages and 64 KiB allocation granularity. Reservation claims address space without physical storage. Commitment charges memory and guarantees zero-initialized contents on first access; it does not prove RAM residency.

VirtualAlloc(NULL, 24 KiB, MEM_RESERVE, PAGE_READWRITE) returns assumed base 0x10000000, aligned to 64 KiB, reserving six 4 KiB pages, P0 to P5.

VirtualAlloc(base + 8 KiB, 10 KiB, MEM_COMMIT, PAGE_READWRITE) starts at 8192; its last byte is 8192 + 10240 - 1 = 18431. It touches P2 (8192-12287), P3 (12288-16383) and P4 (16384-20479). Thus 10 KiB commits 3 x 4 KiB = 12 KiB. Initial readable bytes are zero.

VirtualFree(base + 12 KiB, 4 KiB, MEM_DECOMMIT) decommits P3. P2 and P4 remain committed, 2 x 4 KiB = 8 KiB; four pages are reserved-only, and the original six-page reservation still spans the whole range. VirtualFree(base, 0, MEM_RELEASE) returns all six pages to free. Review Memory Management in OS: Paging and Segmentation, using Microsoft Learn for Windows semantics.

Worked shared-memory mapping: one byte, two addresses

One 12 KiB mapping connects two views at different bases, with offset 5000 resolving to two addresses.

Process A calls CreateFileMappingW(INVALID_HANDLE_VALUE, ..., PAGE_READWRITE, 0, 12288, L"Local\\ScoreBoard"), creating a paging-file-backed 12 KiB mapping with handle 0x74. B opens it and gets handle 0x58. Neither action inserts a view into an address space.

Both map the whole object with MapViewOfFile. A's base is 0x50000000; B's base is 0x70000000. For decimal offset 5000:

  • Page index: floor(5000 / 4096) = 1.

  • In-page offset: 5000 - 4096 = 904 decimal, or 0x388.

  • Overall mapping offset: 5000 decimal, or 0x1388.

  • Address in A: 0x50000000 + 0x1388 = 0x50001388.

  • Address in B: 0x70000000 + 0x1388 = 0x70001388.

A writes 0x2A. After explicit synchronization, B reads 0x2A because both views refer to that mapping offset. Microsoft Learn's file and memory sharing documentation describes the mechanism.

Equal mapping offset identifies shared backing; equal addresses are unnecessary. A mutex, semaphore, event or other coordination is still required. Sharing does not prevent races.

Protection and cleanup: pair each resource with its release

Resource acquired

Correct cleanup

Scope

Key distinction

VirtualAlloc reservation or commitment

VirtualFree

Pages or reservation

Decommit pages; release the full reservation

Process and thread handles

CloseHandle

Each handle

Process exit does not close it

Mapping-object handle

CloseHandle

Object reference

Closing it does not unmap a view

Mapped view

UnmapViewOfFile

Address-space view

Unmapping does not close a handle

Two handles and two views exist initially. A unmaps and closes 0x74; B's view and 0x58 remain usable. B then unmaps and closes 0x58. After the final view and handle are gone, the object can be destroyed.

Access rights are separate from allocation state. This mapping uses PAGE_READWRITE; do not write through a read-only view or request executable protection without a genuine need.

Traps that change the answer

  • Identity: PID is not handle; handle is not pointer; one object can have different handle values. A process handle can outlive the child.

  • Memory: Reservation is not commitment or residency. Count from the byte interval: offsets [8 KiB, 18 KiB) touch P2, P3, P4. In this trace, MEM_RELEASE needs the reservation base and size 0.

  • Sharing:CreateFileMappingW creates or opens backing, not an address. Different bases can share backing; identical names do not bypass access checks; visibility supplies no synchronization.

Before solving, write four columns: object, per-process reference, byte or page range, cleanup or coordination.

How exams and interviews test the interface

Questions distinguish IDs, handles and addresses; count handles; compute pages touched; trace memory states; separate objects from views; translate offsets; or find missing cleanup and synchronization. Each form tests whether the resource, reference, range and lifetime stay separate.

Predict this drill. An 18 KiB buffer starts 3 KiB into a 4 KiB page. Its span from the page start is 3 + 18 = 21 KiB, so it touches ceil(21 / 4) = ceil(5.25) = 6 pages. At bases 0x20000000 and 0x60000000, local addresses differ by 0x40000000, yet one mapping offset can name the same backing byte.

For an interview follow-up, explain the resource, reference, range and cleanup. Then change one buffer length or mapping offset and solve the new trace unaided.

Short version and the next study step

Programs request OS work through documented APIs. Handles refer to OS-managed objects, not IDs or pointers. Virtual pages move through free, reserved and committed states. Views can expose the same backing bytes at different addresses. Remember 1 -> 3 -> 2 -> 1 handles, three 4 KiB pages for the 10 KiB range, and offset 5000 becoming 0x50001388 in A versus 0x70001388 in B. Build a semester-level base with Zero to Hero, Complete CS Course, or choose a topic-specific route from CS Fundamentals for Exams & Placements. Rebuild the page-state and shared-offset traces on paper, then rerun every count unaided.