What is a handle (Windows OS)

Source: Internet
Author: User
What is a handle (Windows OS) FavoritesA handle is a unique integer used by wondows to identify objects created or used by applications. Windows uses various handles to identify application instances, windows, controls, bitmaps, and GDI objects. A Windows handle is a bit like a file handle in C.

From the above definition, we can see that a handle is an identifier used to identify an object or project. It is like our name, and each person has a name, different people have different names, but there may be people with the same name as you. In terms of data type, it is only a 16-bit unsigned integer. Applications almost always obtain a handle by calling a Windows function. Then other Windows functions can use this handle to reference the corresponding objects.

To better understand the handle, I can tell you that the handle is a pointer to the pointer. We know that a pointer is a memory address. After the application starts, the objects that make up the program stay in the memory. If we simply understand it, it seems that we only need to know the first address of the memory, then we can use this address to access the object at any time. However, if you really think so, you are very wrong. We know that Windows is an operating system based on virtual memory. In this system environment, Windows Memory Manager often moves objects back and forth in memory to meet the memory needs of various applications. The object is moved, which means its address has changed. If the address is always changing, where should we find this object?

To solve this problem, the Windows operating system frees up some internal storage addresses for each application to specifically register the address changes in the memory of each application object, and this address (the location of the storage unit) it remains unchanged. After the Windows Memory Manager moves the object's location in the memory, it notifies the new address of the object to save the address. In this way, we only need to remember this handle address to indirectly know the location of the object in the memory. This address is assigned by the system during object loading. When the system is detached, it is released to the system.

Handle address (stable) → record the address of the object in the memory ── ─ → address of the object in the memory (unstable) → actual object

Nature: Windows programs do not use physical addresses to identify a memory block, file, task, or dynamic Mount module. In contrast, Windows
The API assigns a definite handle to these projects, returns the handle to the application, and then operates through the handle.

However, it must be noted that each time the program is restarted, the system cannot ensure that the handle assigned to the program is the original one, and the vast majority of cases are indeed different. If we regard going to a cinema to watch a movie as the startup and running of an application, the handles assigned by the system to the application are always different, this is the same as the fact that tickets sold to us at a cinema are always different.
Influenced by M $'s help documentation and many windows programming books, we generally know that a handle is an integer used to identify a Windows Object. A handle is not a pointer ......

In fact, these are just the secrets of Data encapsulation by M $. Let's take a look at what handle is.

First go to windef. h to find the definition of the vast majority of handles:
Declare_handle (hwnd );
Declare_handle (hhook );
......
Declare_handle (hgdiobj );
Declare_handle (hbitmap );
Declare_handle (hbrush );
......
Typedef handle hglobal;
Typedef handle hlocal;
......

Okay. Now let's turn to winnt. h and see what declare_handle and handle are:
# Ifdef strict
Typedef void * handle;
# Define declare_handle (name) struct name ##__ {int unused;}; typedef struct
Name ##__ * Name
# Else
Typedef pvoid handle;
# Define declare_handle (name) typedef handle name
# Endif
Typedef handle * phandle;

Haha, now you know, handle is pvoid, that is, no type pointer,
And declare_handle (hwnd); is:
Struct hwnd __{
Int unused ;};
Typedef struct hwnd _ * hwnd;
It is actually clear that these handles are just pointers to struct. As for the usefulness of this struct, even M $ says unused, ^ o ^

Now I want to explain the meaning of M $. This is the so-called Data encapsulation. You can transmit the internal structure pointer of M $ in your program, but you don't know what it actually points to, and you can write a handle to prevent everyone from questioning it :). The M $ program can be written as follows:
# Include <windows. h> // This is the same
# Include "windows_in.h" // This is for use by M $. Do not try to see ^ o ^

Hsomethingelse dosomething (hsomething ){
Struct realsomething * P = (struct realsomething *) hsomething; // force type conversion to internal structure pointer first
...... Do something ......
Return (hsomethingelse) prealsomethingelse; // force type inverse Conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.