Monday 28 November 2011

What is a handle? in C programming

What is a handle?

A handle under Windows is much like a handle used to refer to a file in C. It is simply a numeric representation of an object. Under Windows, a handle can refer to a brush (the object that paints the screen), a cursor, an icon, a window, a device context (the object that is used to output to your screen or printer), and many other objects. The handle assigned to an object is used to reference it when calling other Windows functions.

Handle numbers are assigned by Windows, usually when a Windows API function call is made in your program. Typically, variables that represent handles are prefixed with the letter h and a mnemonic representation of the object they refer to. For instance, to create a window, you might make the following Windows API call:

hwndSample =
CreateWindow(szApplicationName, /* Window class name */
“FAQ Sample Program”, /* Caption for title bar */
WS_OVERLAPPEDWINDOW, /* Style of window */
CW_USEDEFAULT, /* Initial x position */
CW_USEDEFAULT, /* Initial y position */
CW_USEDEFAULT, /* Initial x size */
CW_USEDEFAULT, /* Initial y size */
NULL, /* Window handle of parent window */
NULL, /* Menu handle for this window */
hInstance, /* Instance handle */
NULL) ; /* Other window parameters */

The Windows API function CreateWindow() is used to create an instance of a window on the screen. As you  can see from the preceding example, it returns a handle to the window that is created. Whenever this window is referenced from this point on, the handle variable hwndSample is used. Windows keeps track of handle numbers internally, so it can dereference the handle number whenever you make a Windows API function call.

Cross Reference:

None.

No comments:

Post a Comment