What is a handle in Windows Programming?

What is a handle in Windows Programming?

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


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”