Windows in the handle (handle)

1. What handle is?
   In the windows, and the object handle is a one to one 32-bit unsigned integer value. Objects can be mapped to a unique handle, the handle can be mapped to a unique object.
2. Why do we need to handle?
   More precisely, the windows need to handle. windows need to provide the necessary programming interfaces to the programmer, in these interfaces, allowing the programmer to access, create and destroy objects. However, for the package to be considered, windows did not want to return a pointer to the programmer. Pointer contains too much information. First, we are given the exact position of the pointer object storage; secondly, to operate a pointer, the programmer must know the internal structure referents pointer, i.e., windows corresponding data structure must be exposed to the programmer, and these data structures may operating system programmers want to hide.
   If the COM techniques to hide the data to the user, only the exposed interface and allows only methods defined by the interface operation
for data, then, this way you handle your own way allows direct manipulation of data, but not to the windows
exposed to direct you data. Direct programmers need operation data, exposure data is not needed for windows,
handles encapsulation achieve what they want.
3. How to handle maps and objects?
   Behind package must have a local decoding may be implemented to achieve the object handle and conversion. In the
windows, there are two mapping methods:
   A congruent mapping. That is, the handle itself is a pointer. Map here just cast it.
This situation has, the process instance handle or a module handle and resource handle, and so on.
   b. table-based mapping. This is the most common mechanism for mapping between object pointer and handle. Create an operating system
built form, and save all of the objects to be considered. When you need to create a new object, first find an empty entry in the table
And then the insertion of the data object represented therein. When an object is deleted, its members and their data in the table
entry is released.
4. handle the definition and implementation of
   our GDI objects as an example for discussion. Create a GDI object, you'll get a handle on the object. Handle
objects may be a HBRUSH, HPEN, HFONT or the HDC, which depends on the GDI object class you created
type. But the most common type of GDI objects are HGDIOBJ. HGDIOBJ is defined as a null pointer.
   HPEN actual compiler compile-time type definitions with different macros STRICT differ. If STRCIT has
been defined, HPEN is this:
   struct HPEN__ {int unused};
   typedef struct HPEN __ * HPEN;
   if STRICT is not defined, HPEN is defined as:
   typedef void * HANDLE;
   typedef HANDLE HPEN;
   The above code is a detail-oriented programmers closest place to handle, so we focus on analyzing
it. There is a little bit of skill. If the macro is defined STRICT, HPEN point there is a single unused field
pointer structure, otherwise a null pointer HPEN. C / C ++ compiler allows any type of pointer as an empty transfer means even
delivery, not conversely. Two different types of non-null pointer is incompatible. In STRICT version, edited
translation of an incorrect mix GDI object handles will be given a warning, for non-GDI handle, such as HWND, HMENU misregistration
correct mix will be given a warning, so that the procedures are more compiler checks STRICT .
   The following analysis may be less interest you, but it is more profoundly reveals the handle. Come on GDI handles
that despite the windows header files define it as a pointer, but if you double-check the values of these handles, it simply
would not like pointer, which is why I say it is only a 32-bit unsigned integer values of reason. To handle refers to the
case of the needle, these words still apply. Let us freely generate some handles, for example, you use GetStockOb
Ject () to get some handle, you will find that their total value in the range 0x01900011 to 0xba040389.
The former point to the user area unallocated ineffective area, which point to the kernel address space. You may find additional
values between the two handles may be a thin value 1, which also shows GDI handle is not a pointer.
   And not the same as most people think, the handle is not a simple index value. GDI object handle to it,
GDI by the handle 8, a heap object flag (indicating whether the object is created in the heap), and the object type information 7
4 16-bit high index of 0, as shown:
33 2 2,222,222,221,111,111,111 0 0 0 0 0 0 0 0 0 0
1,098,765,432,109,876,543 2 0. 9. 6. 8. 7. 1. 5. 4. 3. 1 0 2
|. 8-bit reference to count | stack | Object type 7 | 16-bit index |
                standard
                in mind

Here you can see that with GDI, it uses only 16 as an index. This means that at most only one process
can create a handle to less than 64K, in fact, subject to a number of other restrictions, the whole windwos system can probably accommodate about
16384 (0x4000) a GDI object.

Reproduced in: https: //www.cnblogs.com/henryhappier/archive/2010/05/20/1739728.html

Guess you like

Origin blog.csdn.net/weixin_34185320/article/details/93537611
Recommended