When using C ++ for Windows Programming, programmers needKernel Object, GDI object, and user objectManagement, these objects are identified by handles, and different resource objects are used by operating these handles. Like heap memory, programmers also need to manage these object resources to avoid system resource leakage.
Handle (handle)Is the unique integer that wondows uses to identify the objects created or used by the application. Windows uses various handles to identify such objects as application instances, windows, controls, bitmaps, and GDI objects. The handle is actually a pointer to a certain resource, but it is different from the pointer: the pointer corresponds to a data address in the memory, and the pointer can be freely modified. Windows does not want a general program to modify its internal data structure, because it is too insecure.
[Differences between kernel objects, GDI objects, and user objects]
The kernel object does not belong to a process and belongs to the Windows kernel. A process has only one kernel object handle table to store all kernel object handles. Therefore, multiple processes can use a single kernel object at the same time-as long as there is a handle.
For GDI objects and user objects, they are internal items of a process and are not shared by multiple processes. The GDI object is related to the drawing, while the user is related to the interaction.
------
The direct owner of kernel objects is the kernel of the operating system. All processes share these kernel objects. Therefore, a mechanism is required to ensure the correct construction and destruction of kernel objects. Windows uses the reference counting technology; the kernel object maintains a reference counting member. A process creates a kernel object. The reference count of the object is 1. If the object is shared by another process, the reference count is increased by 1 for each process, when a process calls the closehandle function, the reference count is reduced by 1. If the reference count is changed to 0, the operating system destroys the kernel object. The reference count is similar to the reference count of COM count. After using the kernel object, you must call closehandle. This function is used to count the reference of the kernel object-1. When the count is 0, the kernel object will become signal and be destroyed by the operating system.
You do not need to call closehandle to destroy the GDI and user objects. The destroy of each GDI object and user object has its corresponding destroy or delete method.
------
# Kernel Object ##
Core object |
Generation Method |
Event object |
Handle createevent (); |
Object |
Handle createfile (); |
File ing object |
Handle createfilemapping (); |
I/O completion object |
Handle createiocompletionport (); |
Job object |
Handle createjobobject (); |
Mailbox object |
Handle createmailslot (); |
Mutex object |
Handle createmutex (); |
MPs queue object |
Bool createpipe (); |
Process object |
Bool CreateProcess (); |
Semaphores |
Handle createsemaphore (); |
Thread object |
Handle createthread (); |
Wait for timer object |
Handle createwaitabletimer (); |
# GDI object ##
GDI object |
Generation Method |
Destruction Method |
Bitmap) |
Createbitmap, createbitmapindirect, Createcompatiblebitmap, createdibitmap, Createdibsection, creatediscardablebitmap |
Deleteobject |
Image painter (hbrush) |
Createbrushindirect, createdibpatternbrush, Createdibpatternbrushpt, createhatchbrush, Createpatternbrush, createsolidbrush |
Deleteobject |
Device context (HDC) |
Createdc |
Deletedc, releasedc |
Font (hfont) |
Createfont, createfontindirect |
Deleteobject |
Memory DC (HDC) |
Createcompatibledc |
Deletedc |
Palette (hpalette) |
Createpalette |
Deleteobject |
Paint Brush (Hpen) |
Createpen, createpenindirect |
Deleteobject |
Region (hrgn) |
Combinergn, createellipticrgn, Createellipticrgnindirect, createpolygonrgn, Createpolypolygonrgn, createrectrgn, Createrectrgnindirect, createroundrectrgn, |
Deleteobject |
# User object ##
User object |
Generation Method |
Destruction Method |
Shortcut Key (haccel) |
Createacceleratortable |
Destroyacceleratortable |
Caret |
Createcaret |
Destorycaret |
Cursor (hcursor) |
Createcursor, loadcursor, LoadImage |
Destorycursor |
Hook) |
Setwindowshookex |
Unhookwindowshookex |
Icon (hicon) |
Createiconindirect, loadicon, LoadImage |
Destroyicon |
Menu (hmenu) |
Createmenu, createpopupmenu Loadmenu, loadmenuindirect |
Deletemenu |
Form (hwnd) |
Createwindow, createdomainwex, Createdialogparam, createmdiwindow |
Destroywindow |
Hdwp |
Begindeferwindowpos |
Enddeferwindowpos |
-- The usage of resources by programs can be viewed in the task manager.