Some knowledge about handle

Source: Internet
Author: User
Control (Control), Font, resource, including icon, cursor, string, and
Object), including bitmap, brush, Metafile, palette, pen, region ), and device context ).


<Windows programming is short and fast> (Nanjing University Press ):
A handle is a unique integer used by wondows to identify objects created or used by applications. Windows uses various handles to identify application instances, windows, controls, bitmaps, and GDI objects. A Windows handle is a bit like a file handle in C.


Ii. Source Code of MFC:

# Ifdef strict
Typedef void * handle;
# Define declare_handle (name) struct name ##__ {int unused;}; typedef struct name #__* name

# Else
Typedef pvoid handle;
# Define declare_handle (name) typedef handle name
# Endif

Declare_handle (hmodule );
Declare_handle (hinstance );
Declare_handle (hlocal );
Declare_handle (hglobal );
Declare_handle (HDC );
Declare_handle (hrgn );
Declare_handle (hwnd );
Declare_handle (hmenu );
Declare_handle (haccel );
Declare_handle (htask );


Iii. Understanding:
Handle is pvoid, that is, there is no type pointer,
The handle handles of the above resources is just a pointer to struct. As for the usage of this struct, even M $ has said unused. Now I want to explain the meaning of M $, this is the so-called Data encapsulation. You can transmit the internal structure pointer of M $ in your program, but you don't know what it actually points.


The handle and pointer are indeed completely different concepts. The handle is only a 32-bit integer. The Win32 object used to mark a system or process can be understood as an object index (because M $ is not fully exposed, to a certain extent, this index can only be understood in this way. This index is more like a ing relationship (from the handle to the object pointer ing) than a purely "array subscript ".



A handle can be understood as a "resource" used to point to or identify the memory. These resources include file, block of memory, and menu. The operating system uses handles to locate core objects and system resources.

A pointer refers to a unit of "data or command" pointing to the memory.

To be exact, the handle is actually a pointer to a certain resource, but it is different from the pointer: the pointer corresponds to a data address in the memory, after getting the pointer, You can freely modify the data. Windows does not want the general program to modify its internal data structure, because it is too difficult Security . Therefore, Windows assigns a handle to each memory area declared by using functions such as globalalloc (essentially a pointer, but do not operate on it directly ), generally, you only use this handle to specify the memory to operate when calling an API function.



Iv. Metaphorical words:
Mutong remote finger Xinghua 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.


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)

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.