Detailed analysis of handle

xiaoxiao2021-03-06  102

Handle concept is a very important concept in Windows programming, playing an important role in many places. However, the handle concept produced is also small, such as: <

> (Microsoft Press, by Richard Wilton) The concept of the handle is: In the Windows environment, the handle is used to identify the project, including:

*. Module (Module)

*. Task (Task)

Instance (Instance)

*. File (File)

*. BLOCK OF MEMORY

*. Menu (MENU)

ContRol

*. Font (font)

*. Resource, including icon (icon), cursor, string, etc.

* .GDI object (GDI Object), including bitmap, brush, metafile, palette (PEN), region (Region), and device description table ( Device context.

In the Windows program, it is not used to identify a memory block, file, task, or dynamic load module. On the contrary, the Windows API assigns a determined handle to these items, and returns the handle to the application, and then passes through the handle. operating.

In <

> (Nanjing University Press) This book is said: The handle is Wondows to identify the unique integer of the object established or used by the application, and Windows uses a wide variety of handle identifies such as application instances, windows, Control, bitmap, GDI object, etc. The Windows handle is a bit icon in a C language.

From the above 2 definitions we can see, the handle is an identifier, which is to identify objects or projects. It is like our name, everyone will have one, different people's names, However, there may be a name as you like you. From the data type, it is just a 16-bit unsigned integer. Applications almost always get a handle by calling a Windows function, then other Windows functions can use the handle to reference the corresponding object. A large number of handles can be used in Windows programming, such as Hinstance (EQ), HBitmap, HDC (Device Description Table Handle), Hicon (Icon Handle), etc., this is also a universal handle It is Handle, such as the following statement:

Hinstance hinstance;

Can be modified:

Handle hinstance;

The second sentence of the above sentence is right.

A Windows application can get a handle of a particular item with a different method. Many API functions, Return values ​​such as CREATEWINDOW, GLOBALLOC, OpenFile are one handle value. In addition, Windows can also transmit a handle as a parameter to an application through an extraction function of the application. Once the application is obtained, the handle can be operated anywhere anywhere in the Windows environment. In fact, a large number of handles has affected the programming of every Windows.

The handle only begins with a project when it is the only project. The handle corresponds to an item in the project table, and only Windows itself can access this table directly. The application can only handle different handles through the API function, and give an example! For example: We can apply for a memory block for our application, and return a handle value by calling the API function GlobalAlloc: HMEM = GlobalAlloc (...);

In fact, the value of the HMEM is only an index value, not a physical address, the application does not directly access this memory. There is also a foreign matter here, that is, in general, when we are programming, the memory assigned to the application can be moved or can be discarded, so that the limited memory resources can be fully utilized, so in a certain At one time we allocate the address of the memory is uncertain, because he is movable, so you have to lock the memory block, which needs to call the API function GLOBALLOCK function to lock the handle. as follows:

LPMEM = GlobalLock (HMEM);

This application can access this memory.

Author: Gu Xubin

转载请注明原文地址:https://www.9cbs.com/read-124449.html

New Post(0)