The difference and connection between a handle and a pointer

Source: Internet
Author: User

The

So-called handle is actually a data, which is a long (full-length) data. A handle is a unique integer used by wondows to identify objects that are created or used by the application, and Windows uses a variety of handles to identify such as application instances, Windows, controls, bitmaps, 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, is to identify the object or project, it is like our name, everyone will have a, different people's name is not the same, but there may be a name and you the same person. It's just a 16-bit unsigned integer from the data type. Applications almost always get a handle by calling a Windows function, and then other Windows functions can use the handle to refer to the corresponding object.

If you want to know the handle more thoroughly, I can tell you that the handle is a pointer to a 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. If we simply understand, it seems that we just know the first address of this memory, then we can access the object at any time with this address. But if you really think so, then you're wrong. We know that Windows is a virtual memory-based operating system. In this 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. If the address always changes so, where should we go to find the 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 memory for each application object, and the 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. So we just have to remember this handle address to know indirectly where the object is in memory. This address is assigned by the system when the object is loaded (load) and released to the system when the system is unloaded (Unload). Handle address (Stable) → The address of the object in memory ────→ the address of the object in memory (unstable) → actual object nature: The Windows program does not use a physical address to identify a block of memory, a file, a task, or a dynamic loading module, instead, Windows The API assigns the identified handle to these items and returns the handle to the application, which is then manipulated by a handle. However, it is important to note that each time the program is restarted, the system cannot guarantee that the handle assigned to the program is the same as the original one, and that the vast majority of cases are indeed different. If we see a movie going into a movie theater as an application start-up run, the system assigns a different handle to the application, which is the same as a seat that sells to us every time.

The

 1, the handle refers to can be a very complex structure, and can be related to the system, such as the above-mentioned thread handle, it points to a class or structure, he and the system has a very close relationship, when a thread for unforeseen reasons, And at the end of the system can be back to the data it occupies, such as CPU, memory, and so on, in turn want to know that the handle of some of the items, is to interact with the system. Because the Windows system is a multitasking system, it is possible to allocate memory, reclaim memory, and reorganize memory at any time. 2, pointer it can also point to a complex structure, but is usually user-defined, so the necessary work must be done 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 handle if you dynamically allocated memory, but CString do not handle the user, it is actually a VC + + class, so the operation is done by the member function, Generated (allocated) by the constructor, delete (recycle) is done by the destructor. 3. Handle is label, pointer is memory address

Handles and pointers are not the same thing at all, there is 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, user objects, GUI objects, these objects are actually some kind of memory data structure (I don't have to explain it). Obviously these objects need to be protected and not let the user modify the access arbitrarily. Otherwise the system is prone to crash so it provides a mechanism to access these objects in a protective way, that is, the API handle is the tag of these objects, or the index in memory often has a handle table, generally at least the following structure index (that is, the handle) pointer (to the Memory object data structure) Other Items 1 ox???????? 2 Ox???????? 3 Ox???????? 4 ... the system takes the API, queries the handle table, gets the pointer corresponding to the handle, this pointer is the real pointer, it can access to modify the protected memory. . If the handle is a pointer. 1 You output some of the handle values, the handle is generally a small number of integer values, such as 1,2,3,4 ... If it is a pointer, it does not point to the protected area. Not a null pointer ... 2 If the handle is a pointer, then the user can not directly access the memory object. What is the stability and security of the system? 3 You get the handle, such as the HWND to do hwnd++,hwnd--and other operations, you see wrong good other reasons I don't want to say if someone said see the handle is defined as a pointer (I have not seen) then you go to the pointer, anyway, the pointer is also a 32-bit value, and unsigned integer no difference.

Note: Three ways to get a window handle

1.HWND FindWindow (LPCTSTR lpclassname, LPCTSTR lpwindowname)

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

2.HWND Windowfrompoint (point& point)//window HWND for 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;

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.