Further understanding of handle in VC

Source: Internet
Author: User

Preface:

Recently, it is difficult for some friends to understand the concept of a handle. I will write down my understanding of the handle below. You can refer to it. If anything is wrong, please confirm your letter.

Body:

   In Microsoft Press, by Richard Wilton, the handle concept in Michrosoft Windows 3 Developer's Workshop is as follows: in Windows, the handle is used to identify a project, these projects include: module, task, instance, file, block of memory, menu) control, font, resource, including icon, cursor, string, and GDI object ), including bitmap, brush, metafile, palette, pen, region, and device context ).

In Windows programming, In a book published by Nanjing University Press, the handle is the unique integer that WONDOWS uses to identify objects created or used by applications, WINDOWS uses various handle identifiers, such as application instances, WINDOWS, controls, bitmaps, and GDI objects. A windows handle is a bit like a file handle in C.

From the two definitions above, we can see that the handle is actually an identifier used to identify an object or project. The handle is a 32-bit positive integer, microsoft Windows uses it to recognize forms or other objects, such as fonts or bitmaps. Applications almost always obtain a handle by calling a WINDOWS function. Then other WINDOWS functions can use this handle to reference the corresponding object. In WINDOWS programming, a large number of handles are used, such as HINSTANCE, HBITMAP, HDC, and HICON, there is also a general HANDLE.

A windows application can obtain the handle of a specific item in different ways. Many API functions, such as CreateWindow, GlobalAlloc, and OpenFile, return a handle value. In addition, WINDOWS can also transmit a handle to the application as a parameter through the app's function extraction. Once the application obtains a specific handle, you can operate the handle anywhere in WINDOWS. To be exact, 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, after getting the pointer, You can freely modify the data. Windows does not want a general program to modify its internal data structure, because it is too insecure. Therefore, Windows assigns a handle to each memory area declared by using functions such as GlobalAlloc (essentially a pointer, but do not operate on it directly ), generally, you only use this handle to specify the memory to operate when calling an API function. When you need to perform direct operations on a memory, you can use GlobalLock to lock the memory and obtain the pointer for direct operations.

"Handle" (handle), handle is intended to be the handle, the meaning of the handle. It is something you deal with the operating system. For example, if you have lost your mind (I am talking about it, for example, haha) and unfortunately let me catch the handle, then you have to do what I want you to do, it's because you have something on me. This is also true for programming. For example, if we want to manipulate a window, we must "grasp its handle". Only in this way can we change its attributes and style, or even destroy it ). Let's cite another example. For example, if you are admitted to a university, the school (operating system) will give you a student ID number after you enter the university. Note that this number is specified by the school and you cannot select it. With this number (student ID card, for example, a multi-purpose student ID card), you can enjoy the services provided by the school. For example, you can borrow books from the library, go to the canteen for dinner, and go to the classroom for classes. But you cannot buy beer in the canteen because the school does not allow this service. In computer systems, the service provided by the system is API calling. With HANDLE, you can confidently propose the service to the system to call the API. The pointer has a lot of power. With the pointer, you can drink and fight everywhere, and the school (operating system) cannot handle it, the difference between a handle and a pointer is that a handle can only call the services provided by the system. While the handle is a number that can be differentiated from each other, it is different from the ordinary idnumber. The ordinary idnumber can be defined by the programmer, but the handle cannot, it is specified by the system when an object is generated to distinguish the objects in the system. This handle is not assigned by the programmer. In practical applications, file handles and window handles are commonly used. For example, the window handle value is a long integer, and each form is represented by a handle. Therefore, the handle will not be repeated. Many functions will use the form handle.

Now, I think you should have a better understanding of the concept of "handle". To put it bluntly, this is just a personal understanding. If you really want to have a deeper understanding, let's take a look at the MSDN explanation, which is the real authority!

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.