answersLogoWhite

0

A handle is a reference for the operating system. It does not have the semantics of a programming reference but what it does do is allow the system resources to know what you are referring to when it is passed in an API call. Usually, the HANDLE is wrapped in an instance of a class. CWnd is a good example, it contains an HWND which is a handle to a window. You can do this. CWnd *pWnd = CWnd::FromHandle(hWnd) Note: that CWnd::FromHandle(hWnd) is static and does not require an instance. It will pass you back the wrapper that the hWnd is wrapped by. Well not quite! If the handle is not actually wrapped by an object it will create one AND IT WILL ONLY BE TEMPORARY.So use it the once then throw it away. It can create the instance because the hWnd has enough information in its struct for windows to instantiate a CWnd object. It does not add it to the handle v object table, so it is only temporary. The HWND is in fact a kernel object and theres more ? HWND (CWnd and CWnd-derived classes) HDC (CDC and CDC-derived classes) HMENU (CMenu) HPEN (CGdiObject) HBRUSH (CGdiObject) HFONT (CGdiObject) HBITMAP (CGdiObject) HPALETTE (CGdiObject) HRGN (CGdiObject) HIMAGELIST (CImageList) SOCKET (CSocket) (Should have been HSOCKET?) + others. I am not sure if all of these would pass back a temporary object if required. ::GetDC(hWnd) will get you a hDC from an hWnd but it will be temporary, probably better to use the CDC claa. It may be a fundamental requirement for windows programming? For a proper explanation look up Google with this "Inside MFC: Handle Maps and Temporary Objects"

User Avatar

Wiki User

19y ago

Still curious? Ask our experts.

Chat with our AI personalities

TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
JudyJudy
Simplicity is my specialty.
Chat with Judy
More answers

Generally speaking, a handle is an abstract reference to a resource that is managed by another system, such as the operating system or a database. Normally we use pointers to reference objects in memory, but the objects we point at are under our direct control; they do not arbitrarily relocate themselves without us knowing about it. But resources outwith our direct control have a nasty habit of moving around in memory behind our backs, which would be disastrous if we simply pointed at them (our pointers would constantly be invalidated).

By using a handle, the resources can freely move around as required by the external system that controls them without invalidating the handle. Implementations vary but a handle is often implemented as a key or index into a table of resources which map the handle to an actual memory address. Thus when we pass a handle into an API that understands the handle's purpose, the handle can be dereferenced accordingly.

For instance, when we call various functions in the Windows Win32 API, we might pass an HWND value which is a handle to a specific window, or an HKEY value which is a handle to a specific registry key, or an HFILE which is a handle to a disk file. We use these same APIs to initialise the handles and, if required, release them when they are no longer required. We cannot predict where these objects will reside in memory at any given moment, but with a handle we don't have to because the system that controls those resources can use the handle to locate them for us, via the API provided by that system.

User Avatar

Wiki User

11y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What is a handle in Windows Programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp