Windows Handles

A handle is an unsigned integer that Windows uses to identify an object (data structure) created or used by a program. Windows uses a wide variety of handles, identifying objects such as allocated memory, controls, files, icons, menus, output devices, and windows.

Most handles are index values for internal tables. Windows uses handle indexes to access the information stored in the table. Typically, your program has access only to the handle, not the data. When you need to change the data, you supply the handle and Windows does the rest. This is one way that Windows protects data in its multitasking environment.

For efficiency and memory management reasons, Windows uses movable sections of memory. Handles are often used to keep track of data in these movable sections. Although the data's memory location may change, the handle to the data remains constant.

Some of the more common handle types in Windows are shown in Table B.3.

Table 0.3 Windows Handles

Handle Type Description

HANDLE generic handle (WORD)
HBRUSH handle to a brush
HCURSOR handle to a cursor
HDC handle to a device context
HFONT handle to a font
HICON handle to an icon
HMENU handle to a menu
HPEN handle to a pen
HWND handle to a window

One of the best ways to learn about handles in Windows is to examine the sample programs included with the chapters in the “Creating Windows Programs” section.