Illustration--What exactly is a Windows handle

Source: Internet
Author: User

Illustration--What exactly is a Windows handle

Reference: http://blog.csdn.net/newjerryj/article/details/4383701

Http://www.cnblogs.com/yellowyu/archive/2009/06/07/1497910.html

Write in front:

For the "handle", under the smattering has been stuck in the cognitive level, recently in the next study of Windows programming, decided to take this opportunity to fully understand the handle. Look up some of the information on the network, found that the explanation on the network can be divided into two categories: a metaphor, the analogy of the way to explain, although the image is understandable, but not from the principle, in essence, to reveal, people still want to ask "why?" "," How to achieve? ”。 The other is to give the source code, understandable, this is of course the most essential explanation, but in this way, it is not intuitive enough for beginners to understand a certain degree of difficulty. In view of this, under the inconsiderable of the energy, combined with their own humble opinion, in the compromise between the two, with a graphical approach to the principle presented, to do at a glance.

Here you need to explain:

1. All the things that the handle can identify (such as Windows, files, brushes, etc.) are collectively referred to as "objects".

2. A small horizontal box in the figure represents a certain size of memory area and does not represent a byte, such as a 0x00000ac6-labeled horizontal box representing 4 bytes.

3. The purpose of the diagram is to be intuitive, so it does not necessarily correspond to the source code, there will be some simplification.

Let's look at the picture first and then explain.

In this case, Figure 1 is a memory snapshot of the program running to a certain point, and Figure 2 is a memory snapshot of the program running backwards to another moment. The red section marks two changes.

Simple explanation:

Windows is a virtual memory-based operating system, many times, the process of code and data are not all loaded into memory, a certain part of the process is loaded into memory, may also be swapped out to external memory, when needed again, then loaded into memory. Two-time addresses are not the same in most cases. In other words, the address of the same object in memory changes. (for readers who are not very knowledgeable about virtual memory, you can refer to the books on the operating system) so, how can the program accurately access the object? To solve this problem, Windows introduces a handle.

The system allocates a certain area in memory for each process, which is used to store each handle, a 32-bit unsigned integer value (32-bit operating system). Each 32-bit unsigned integer value is equivalent to a pointer to another area in memory (which we might call zone a). In zone A, it is the address of the object in memory. When the object's position in memory changes, the value of zone A is updated to become the address of the current object in memory, and in this process, the location of zone A and the value of the corresponding handle are not changed. This mechanism, in the form of an image, can be expressed as: there is a fixed address (handle), point to a fixed position (area a), and the value in zone A can change dynamically, it always records the current moment object in memory address. Thus, regardless of how the object's position changes in memory, as long as we have mastered the value of the handle, we can find the area A and find the object. The value of the handle is absolutely constant during this run of the program, and we (i.e. the system) can certainly master it. This is status quo, retrace, the clues.

So, we can understand that Windows Handle :

Numerically, is a 32-bit unsigned integer value (32-bit system); Logically, a pointer to a pointer; Image understanding, is a unique, fixed ID of each object in Windows; In effect, Windows uses a handle to identify objects such as windows, bitmaps, brushes, and so on. And find these objects through a handle.

Here are some key details about the handle:

1. The so-called "unique", "invariant" refers to the operation of a program. If this is done, close the program and start the program again, then the value of the handle of the same object and the last runtime comparison are generally different in this run.

In fact, it is very natural to understand that the so-called "a piece of a piece, this is the one, that is, the two irrelevant" ("put" is the image of the argument, like playing cards, here refers to a program run).

2. The handle is specified by the system when the object is generated, the property is read-only, and the programmer cannot modify the handle.

3. In different systems, the handle size (number of bytes) is different, and sizeof () can be used to calculate the handle size.

4. Through the handle, the programmer can only invoke the service provided by the system (i.e. API calls), and cannot do anything else like using pointers.

Write in the back:

1. So far, here's a quick introduction to the Windows handle. What needs to be explained is that this article is for beginners, is designed to let the reader have a complete and clear understanding of the handle, both to avoid knowing its why the confusion, but also to avoid deep source of difficult obscure. Therefore, this article does not achieve the absolute direct nature, but also may be in the individual details and the actual situation slightly discrepancy, but under the thought that this does not harming the beginner to the handle understanding. Because the cognition of a certain knowledge, from almost ignorant or smattering to "proficient", often need more new knowledge of the supplement, a short time is difficult to achieve, without affecting the use of knowledge under the premise, first grasp the whole, in the gradual depth of detail, is a wise choice. Readers who want to get a deeper understanding of Windows handles can look at the next article, "Anatomy of the Source code – deep into the nature of windows handles."

2. Under the limited knowledge, the understanding is not deep, if has the mistake wrong place, here repeatedly implored everybody must for the next point. Everyone's criticism refers to the source of progress.

Illustration--What exactly is a Windows handle

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.