1. What is a handle?
In Windows, the handle is a 32-bit unsigned integer corresponding to the object. An object can be mapped to a unique handle or a unique object.
2. Why do we need a handle?
To be more accurate, a handle is required for Windows. Windows needs to provide programmers with the necessary programming interfaces in which programmers can access, create, and destroy objects. However, for encapsulation purposes, Windows does not want to return pointers to programmers. The pointer contains too much information. First, the pointer gives the exact location of the Object Storage. Second, to operate a pointer, the programmer must know the internal structural features of the object referred to by the pointer, that is, windows must expose the corresponding data structure to the programmer, which may be hidden by the operating system.
If COM technology hides data from users, only interfaces are exposed and only operations defined by interfaces are allowed.
For data, the handle method allows you to operate data directly in your own way, but Windows does not
You can expose data directly. Directly operating data is required by programmers. It is required for Windows to not expose data,
The handle encapsulation method is required.
3. How does a handle map to an object?
There must be a place behind encapsulation that can be decoded to implement mutual conversion between handles and objects. In
In Windows, there are two ing methods:
A. Full ing. That is, the handle itself is a pointer. Here, the ing is only type conversion.
In this case, process instance handle, module handle, and resource handle.
B. Table-based ing. This is the most common ing mechanism between object pointers and handles. Operating System Creation
Create a table and save all the objects to consider. To create a new object, you must first find an empty entry in the table.
And then add the data that represents the object. When an object is deleted, its data members and its
The entry is released.
4. definition and implementation of handles
Let's take the GDI object as an example. When a GDI object is created, the handle of the object is obtained. Handle
The object may be one of hbrush, Hpen, hfont, or HDC, depending on the GDI object class you created
Type. However, the most common type of GDI object is hgdiobj. Hgdiobj is defined as a null pointer.
The actual compilation Type Definition of Hpen varies with the Compilation Time macro strict. If strcit already exists
The Hpen is defined as follows:
Struct Hpen _ {int unused };
Typedef struct Hpen _ * Hpen;
If strict is not defined, Hpen is defined as follows:
Typedef void * handle;
Typedef handle Hpen;
The above code is a detail-oriented programmer closest to the handle, so we focus on the analysis
. Here are some tips. If the strict macro is defined, Hpen points to a single unused field.
Structure pointer; otherwise, Hpen is a null pointer. The C/C ++ compiler allows any type of pointer to be passed as a null pointer.
Delivery, but not vice versa. Two non-null pointers of different types are incompatible. In the strict version, compile
A warning is given for incorrect mix of GDI object handles. For non-GDI handles, such as incorrect hwnd and hmenu
A mix of them will also give a warning so that the program can get a more strict check in the compiler.
The subsequent analysis may not interest you so much, but it reveals the handle more deeply. For
Although the Windows header file defines it as a pointer, if you carefully check the value of these handles
It is not like a pointer, Which is why I say it is only a 32-bit unsigned integer. Handle
In the case of needles, this sentence also applies. Let's generate some handles at will, for example, you use getstockob
To get some handles, you will find that their values are from 0x01900011 to 0xba040389.
The former points to the unallocated invalid region in the user zone, and the latter points to the kernel address space. In addition, you may find that
, The value between two handles may only be 1, which also indicates that the GDI handle is not a pointer.
Unlike most people think, the handle is not a simple index value. For the GDI object handle,
The GDI handle is marked by an 8-bit, 1-bit heap object (indicating whether the object is created in the heap), 7-bit object type information, and
A 16-digit index with four digits in height and zero digits,
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 6 5 4 3 2 1 0 9 8 6 5 4 3 2 1 0
| 8-bit reference count | heap | object type 7 | 16-bit index |
Mark
Note
Here you can see that for GDI, it only uses 16 bits as the index. This means that a process can only
You can create less than 64 K handles. In fact, due to some other restrictions, the entire windwos system can accommodate approximately
16384 (0X4000) GDI objects.