The differences among objects, handles, and IDs in MFC.

Source: Internet
Author: User

WindowsProgramThere are a variety of resources (window, icon, cursor), the system will allocate memory for these resources when creating, and return the ID number to identify these resources, that is, the handle (ID ). Hicon, hcursor, and hbrush ).

Why do I need a window handle for a window object? What other thread handles, even the Control ID and process ID? Objects in MFC, such as application objects and view objects, occupy the memory space. We can use pointers to this object for access, but windows also provides handle access, it seems a little redundant at first glance, but not actually. If one of our processes wants to access another process, access based on the object pointer will not work. Windows is a fully protected system. Applications work in the CPU protection mode and the virtual memory technology is introduced. Each process has an independent 4 GB address space, so the address in the application is the address in your own eyes, not universal. The two processes cannot simply pass an address. The form handle is a data structure of the Windows Kernel. Different Forms (can be different forms of different processes) there are different form handles, and Windows can identify different form objects through the handle. Is the process handle globally unique? None! The process handle is also valid in the current process. It is obtained when a process is created or opened. The process ID is globally unique. Is the Control ID globally unique? No! The Control ID represents a resource. In many cases, it represents the path and Resource Name of a resource.

 

What is the handle? What is the pointer?

In fact, there is nothing magical about the handle,Regardless of the handle, it is actually an integer.It identifies a resource, such as a window or a bitmap. Just like finding a person, you must know its address. If you want to operate a resource, you must first obtain the handle. "Retrieve window handle ()" is not only a window handle. You can use this command to retrieve all window controls, such as edit boxes and labels. For example: edit box 1. take the window handle () or label 1. take the window handle (). The handle of a control is the same. Any control has its own special properties, and the handle also refers to its special properties (including commonalities ).

Handle is intended to be a handle. It means something that deals with the operating system. For example, if you are admitted to a university, the school (operating system) will give you a student ID card number. Note: This number is specified by the school. YouUnable to select. With this number (student ID card, if one card is used for multiple purposes), 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. The service provided by the system in the computer is called by API. With handle, you can confidently propose an API service to the system. 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.

You can refer to a person on csdn to describe the relationship between the handle and pointer object instances:

Shepherd's remote finger Apricot Flower village. The hand of the shepherd boy is the pointer, the brand of the apricot Village is the handle, and the apricot Village Hotel is an instance of the object.

 

In-depth discussion of handle

more thoroughly, A handle is a pointer to a pointer . As we all know, pointers are memory addresses. 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) the body remains unchanged. inform the new address of the object to save the address . In this way, we only need to remember this handle address indirectly knows the location of the object in the memory. This address is assigned by the system during object loading. When the system is uninstalled, It is unloaded) and then released to the system. handle address (stable) → records the address of the object in the memory → the 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 watch a movie at a cinema as an application, the handles assigned to the application by the system are always different, this is the same as the fact that tickets sold to us at a cinema are always different.

MFC _ window ID, handle, and pointer conversion functions

Mutual conversion between ID--HANDLE--HWND
ID-> handle hwnd =: getdlgitem (hparentwnd, ID );
ID-> pointer cwnd: getdlgitem ();
Handle-> ID id = getwindowlong (hwnd, gwl_id );
Handle-> pointer cwnd * pwnd = cwnd: fromhandle (hwnd );
Pointer-> ID id = getwindowlong (pwnd-> getsafehwnd, gwl_id );
Getdlgctrlid ();
Pointer-> handle hwnd = cwnd. getsafehandle () or mywnd-> m_hwnd;

 

Usage of pointersIt is vital in the programming process. The correct and correct usage of pointers not only improves the running efficiency of the program itself, but also helps to save the resources required for program execution. The pointer corresponds to the address of a data in the memory space. Once the pointer is obtained, the data can be freely modified.HandleA pointer can also be compared to the index value of a data item in a table (the table corresponds to the memory space of a process ). A handle is an indirect reference object.

Differences between pointers and handles:

    • A handle can be a complicated structure and may 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 for unexpected reasons, the system can recycle the information it occupies, such as CPU and memory. In turn, some of these handles interact with the system.
    • The pointer can also point to a complex structure, but it is usually defined by the user, so some necessary work must be done by the user, especially when deleting.
    • Note thatA handle often has its own limitations. For example, if a process is passed to another process, the handle will become meaningless (Index value of the data item in the tableIf the index is removed from a specific table, the meaning is lost ).

Specific Conversion:

(Convert handle to pointer)

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;

Of course, more detailed information needs to be queried in specific use.

Suggestions: The use of pointers and handles is a complicated and risky application. In practice, if possible, do not use pointers and handles, it is best to select an existing, well-encapsulated method to implement it, not to mention the conversion of the pointer with the handle, which is more dangerous.

For example, when operating on a string, use the cstring class as much as possible, and use the defined constructor and destructor to complete the allocation and collection. It is best not to use a pointer for dynamic operations.

 

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.