Monday, May 28, 2007

MFC Temporary Handles

If we want a CWnd object from  a hwnd , the first functions coming to my mind is these.
CWnd -> Attach()

The difference is this , the CWnd::FromHandle first checks if the specified hwnd is already in the Permenent handle map. If so it
returns the CWnd * from the map. If it can't find it searches in the Temporary map and returns. if it couldn't see the hwnd in both
permenent and temporary map it will create a new CWnd * object and add it to temporary map.

So the point is this. The temporary objects are deleted during the idle time. So don't store the pointer returned from CWnd::FromHandle like functions ( CPen, CDC , CGDIObject etc ).

If we are calling Attach. it should be detached, otherwise during the destruction of the class (CWnd), the destructor calls the ::DestroyWindow API , to destroy the attached window( may be we don't want to do that). Also don't give non-MFC window pointer to Attach function. It is not valid . If you want , then use FromHandle function

No comments: