Differences and relationships between handles and pointers

Source: Internet
Author: User

The so-called handle is actually a data, which is a long data. Wondows identifies the unique integer of the object created or used by the application. Windows uses various handles to identify application instances, windows, controls, bitmaps, 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: in Windows, a physical address is not used to identify a memory block, file, task, or dynamic Mount module. In contrast, Windows API assigns a definite handle to these projects, return the handle to the application, and then use the handle to perform the operation. 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.

1. The handle refers to a very complex structure and can be related to the system. For example, the thread handle mentioned above points to a class or structure, it is closely related to the system. When a thread is terminated due to unpredictable reasons, the system can return the information it occupies, such as CPU and memory, in turn, I want to know that some items in the handle interact with the system. Because Windows is a multi-task system, it may need to allocate memory, recycle memory, and reorganize the memory at any time. 2. the pointer can also point to a complex structure, but it is usually user-defined, so the necessary work must be completed by the user, especially when deleting. However, there are also some pointers in VC ++ 6.0, which are used only when dealing with small issues, such as the most common character pointers, it also needs to be processed by the user. If you dynamically allocate memory, but cstring does not need to be processed by the user, it is actually a class in VC ++, so the operations are completed by the member function, the constructor generates (allocates) and deletes (recycles) the constructor. 3. The handle is the label and the pointer is the memory address.

The handle and pointer are basically not the same thing. My definition of the handle is :. A handle is a identifier or index of a protected data structure in the system. my explanation: when your application or system establishes kernel objects, user objects, and GUI objects, these objects are actually some memory data structures (I don't want to explain this) obviously, these objects need to be protected and cannot be modified or accessed by users. Otherwise, the system will crash easily, so a mechanism is provided to access these objects in a protective manner, that is, the API handle is the mark of these objects, or the index usually has a handle table in the memory. Generally, there are at least the following structure indexes (that is, the handle) pointer (pointing to Memory Object Data Structure) other item 1 ox ???????? 2 ox ???????? 3 ox ???????? 4... the system uses APIs to query the handle table and obtain the pointer corresponding to the handle. This pointer is the real pointer and can be used to access and modify the protected memory .. assume that the handle is a pointer. 1. You can output some handle values. Generally, the handle values are small integers, such as 1, 2, 3, and 4 ...... if it is a pointer, does it start to point to a protected area? Isn't it a null pointer ??? 2. If the handle is a pointer, can the user directly access the memory object? What stability and security does the system have? 3. You perform operations on the obtained handle, such as hwnd for hwnd ++ and hwnd for example, you are wrong. I don't want to say anything about other reasons. If someone says that the handle is defined as a pointer (I have never seen it), you should be a pointer, the pointer is also a 32-bit value, which is no different from an unsigned integer.

Note: three methods for obtaining window handle

1. hwnd findwindow (maid, lpwindowname)

Hwnd find1_wex (hwnd hwndparent, hwnd hwndchildafter, lpctstr lpclassname, lptstr lpwindowname)

2. hwnd windowfrompoint (point & Point) // obtain the Windows hwnd at the current cursor position

3. bool callback enumchildproc (hwnd, lparam)

Bool callback enumchildwindows (hwnd hwndparent, wndenumproc lpenumfunc, lparam)

Bool callback enumwindows (wndenumproc lpenumfunc, lparam)

Bool callback enumwindowsproc (hwnd, lparam)

Conversion between pointer handles

A. the pointer obtains the handle cwnd * pwnd; cwnd hwnd; hwnd = pwnd-> getsafehwnd ();

B. Get the pointer from the handle:

Cwnd * pwnd = fromehandle (hmyhandle); pwnd-> setwindowtext ("Hello world! "); Or cwnd * pwnd; pwnd-> attach (hmyhandle); some of the MFC classes also provide standard methods, such as window handle: static cwnd * Pascal fromhandle (hwnd ); hwnd getsafehwnd () const; For bitmap: static cbitmap * Pascal fromhandle (hbitmap); static cgdiobject * Pascal fromhandle (hgdiobj hobject); hgdiobj getsafehandle () const;
 

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.