The difference and contact between the handle and the pointer

Source: Internet
Author: User

The so-called handle is actually a data. is a long (full-length) data.

A handle is a unique integer used by wondows to identify objects that are created or used by an application, and Windows uses a variety of handles to identify forms such as application instances. Control, bitmap. GDI objects, and so on.

The Windows handle is somewhat like a file handle in the C language. From the definition above we can see that the handle is an identifier, it is to identify the object or project, it is like our name, each person will have one. Different people don't have the same names, but. There may also be a person who has the same name as you.

From the data type, it is just a 16-bit unsigned integer. Applications almost always get a handle by calling a Windows function, and other Windows functions can then use the handle to refer to the corresponding object.

Assuming that you want to know the handle more thoroughly, I can tell you that the handle is a pointer to the pointer. We know that the so-called pointer is a memory address. After the application is started, the objects that make up the program are left in memory.

Assumptions are simple to understand. It seems that we just need to know the first address of this memory, then we can use this address to access the object at any time. But. Let's say you really feel that way. Then you're a big mistake. We know that Windows is a virtual memory-based operating system. In such a system environment. The Windows Memory manager often moves objects back and forth in memory to meet the memory needs of various applications.

The object being moved means that its address has changed.

Assuming the address always changes, where should we go to find the object?

To solve the problem, the Windows operating system frees up some internal storage addresses for each application. Used specifically to register the address changes in memory for each application object, and this address (the location of the storage unit) itself is unchanged.

When the Windows Memory Manager moves the object's location in memory, it stores the address of the new object to the handle. This way, we just need to remember that the address of the handle can indirectly know where the object is in memory in detail.

This address is assigned by the system when the object is loaded (load). When the system is unloaded (Unload) is released to the system. Handle address (Stable) → The address of the object in memory ────→ the address of the object in memory (unstable) → actual object nature: A memory block, file, task, or dynamic loading module is not identified by a physical address in a Windows program. Instead, the WINDOWS API assigns the identified handles to these items and returns the handle to the application. The handle is then manipulated. However, it is important to note that the program starts every time. The system cannot guarantee that the handle assigned to the program is the same as the original one, and the vast majority of cases are indeed different. If we take the movie into the cinema as an application start-up execution. Then the system assigns the handle to the application is always different, this and every time the movie theater sells our ticket is always the same seat is the same reason.

1, the handle refers to can be a very complex structure. And very much can be related to the system, for example, the thread above the handle, it points to a class or structure, he and the system has a very close relationship. When a thread is due to unforeseen reasons. When terminated, the system is able to return the data it occupies, such as the CPU. Memory, and so on. In turn, to be able to know that some of the items in this handle are interacting with the system.

Because of the Windows system. is a multi-tasking system that can allocate memory at any time. Reclaim memory and reorganize memory. 2, the pointer it can also point to a complex structure. However, it is generally user-defined. So the necessary work must be completed by the user. Especially at the time of deletion. But in the vc++6.0 also have some pointers, they are used to deal with some small problems, such as the most common character pointers, it is also for the user to deal with the assumption that you dynamically allocated memory, but CString do not deal with the user, it is in fact a VC in a class. So the operation is completed by the member function, resulting (allocation) by the constructor, delete (recycle) by the destructor complete. 3. Handle is label, pointer is memory address

Handles and pointers are not the same thing at all. There's basically no common ground. My definition of the handle is:. A handle is a flag or index of a data structure that is protected within the system. My explanation: When your application or system builds kernel objects. The user object. GUI objects when actually these objects are some kind of memory data structure (I don't have to explain it) obviously these objects need to be protected. Users can not be arbitrarily changed access, or the system is very easy to crash so provides a mechanism to visit these objects in a protective way, that is, the API handle is the tag of these objects, or the index in memory often there is a handle table. Generally at least there are, for example, the following structure index (that is, the handle) pointer (pointing to the Memory object data structure) Other Items 1 ox??????

??

2 Ox??????

??

3 ox??

??

??

?? 4 ... the system takes the API, queries the handle table, obtains the corresponding pointer of the handle, this pointer is the real pointer. Use it to access changes to protected memory. . If the handle is a pointer. 1 You output the value of some handles, the handle is generally a very small integer value. For example 1,2,3,4 ... If it is a pointer, does it not point to a protected area? Wouldn't it be a null pointer??? The 2 handle is assumed to be a pointer. Wouldn't the user be able to access the memory object directly? What is the stability and security of the system? 3 You get the handle, for example, the HWND to do hwnd++,hwnd--and other operations, you see wrong good other reasons I don't want to say. Suppose someone says see the definition of a handle as a pointer (I haven't seen it) then you go as a pointer, anyway the pointer is also a 32-bit value. and unsigned integers no matter what the difference.

Note: Three ways to get a form handle

1.HWND FindWindow (LPCTSTR lpclassname, LPCTSTR lpwindowname)

HWND FindWindowEx (HWND hwndparent, hwnd hwndchildafter,lpctstr lpclassname, LPCTSTR lpwindowname)

2.HWND Windowfrompoint (point& point)//The form HWND that gets the current mouse cursor position

3.BOOL CALLBACK Enumchildproc (HWND hwnd,lparam LPARAM)

BOOL CALLBACK enumchildwindows (HWND hwndparent, Wndenumproc lpenumfunc,lparam LPARAM)

BOOL CALLBACK EnumWindows (Wndenumproc lpenumfunc, LPARAM LPARAM)

BOOL CALLBACK Enumwindowsproc (HWND hwnd, LPARAM LPARAM)

Conversion between pointer handles

A. Obtaining a handle by the pointer CWnd * PWND; CWnd HWnd; HWnd = Pwnd->getsafehwnd ();

B. Get pointers by 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 the window handle: static cwnd* PASCAL FromHandle (HWND hwnd); HWND GetSafeHwnd () const; For bitmaps: Static cbitmap* PASCAL fromhandle (hbitmap hbitmap); Static cgdiobject* PASCAL FromHandle (hgdiobj hobject); Hgdiobj getsafehandle () const;

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

The difference and contact between the handle and the pointer

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.