[Reprint] analyze the handle and pointer from getsafehwnd () and getsafehandle ()

Source: Internet
Author: User

Main differences between getsafehwnd () and getsafehandle:

1. Different users:

(1) form usage:

Getsafehwnd () is used to obtain the secure handle (that is, hwnd) of the Form. With hwnd, We can conveniently perform the required operations on the form pointed to by hwnd;

(2) Use a GDI object:

Getsafehandle () is used to obtain the handle of the GDI object.

 

Note: we strongly recommend that you do this when using pointers:

// Psomewnd is a pointer to a form

If (null! = Psomewnd & null! = Psomewnd-> getsafehwnd ())

{

// Do something.

}

//////////////////////////////////////// //////////////////////////////

Additional knowledge:

Memory handle and pointer differences:

(1 ). the handle is actually a pointer, but the biggest difference between it and the pointer is: Give You A pointer, you can do anything through this pointer, maybe it is a good thing, maybe it is through this pointer to destroy the memory, do something messy. I think everyone will have encountered this, because the use of pointers may cause program crash.
The handle does not have this disadvantage. Through the handle, you can only do things that Windows allows you to do (for example, call some API functions), without the random pointer.

(2). the handle is the index of some tables, that is, the pointer to the pointer. The handle and pointer are both addresses. The handle is a key concept in Windows programming. Writing Windows applications always involves various handles.
A handle is a unique number used to identify many different object types, such as Windows, menus, memory, brushes, and paint brushes. In Win32, the handle is a pointer to a "untyped object" (void), that is, a 4-byte long data.
No matter what its nature is, the handle is not a real pointer.
In terms of structure, a handle is a pointer, although it does not point to the memory location used to store an object. In fact, the handle points to a location that contains a reference to this object.
The handle declaration is as follows:
Typedef void handle

Because Windows is a multitasking operating system, it can run multiple programs or multiple copies of one program at a time. These running programs are called an instance. To manage multiple copies of the same program, Windows introduces an instance handle. Windows creates a table for each application, and the instance handle is like an index for this table.

 
The difference is:
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 for unexpected reasons, the system can recycle the resources 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 memory at any time.
2. the pointer can also point to a complex structure, but it is usually user-defined and all necessary work needs to be done by the user, especially when deleting. But there are also some pointers in VC ++ 6.0, which are used only when dealing with small issues. For example, the most common character pointer also needs to be processed by the user, for example, if you dynamically allocate memory, but cstring does not need to be processed by the user. It is actually a class in VC ++, and all the operations are completed by the member function, generate (allocate) the constructor deletes (recycles) the constructor.


Note: three methods for obtaining window handle

1. hwnd findwindow (lpctstrLpclassname, LpctstrLpwindowname);

 

  HWND FindWindowEx( HWND hwndParent, HWND hwndChildAfter,                         LPCTSTR lpszClass, LPCTSTR lpszWindow );

2. hwnd windowfrompoint (
PointPoint); // Obtain the Windows hwnd at the current cursor position

3. bool callback enumchildproc (hwndHwnd, LparamLparam);

 

BOOL EnumChildWindows( HWND hWndParent,
                       WNDENUMPROC lpEnumFunc,
                       LPARAM lParam );
BOOL EnumWindows( WNDENUMPROC lpEnumFunc, LPARAM lParam ); 
BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam );
 

Additional knowledge: conversions between pointers and handles

A. Get the handle from the pointer
Cwnd * pwnd;
Hwnd;
Hwnd = pwnd-> getsafehwnd ();

B. Get the pointer from the handle:
Cwnd * pwnd = fromehandle (hmyhandle );
Pwnd-> setwindowtext ("Hello world! ");

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


Some people say that a handle is a identifier and an ID number, which is incorrect. An ID number can contain multiple resources. For example, the idr_mainframe in a single document generally refers to resources on the hard disk. However, when resources on the hard disk are transferred to the memory, a handle points to it, but the handle can only point to one resource. And the handle knows the memory size. The Pointer Points to the address and does not know the size of the allocated memory.
However, if you define a handle, right-click the VC, and select "go to definition of handle", you will find that it is essentially a pointer, but its function is different from pointer.

The handle is a pointer pointing to a piece of memory, but the caller does not need to know how the memory is associated with the object identified by the handle. The caller only needs to know, this handle is associated with a Win32 object.
A handle is a physical address that can be transferred across processes. For example, a window in handle ha process, you can use a value equal to ha in process B (that is, they are forcibly converted to int32 values equal) to initialize a handle.

 

Address: http://blog.csdn.net/ghevinn/article/details/21445767

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.