Newsgroups: comp.windows.ms.programmer
Path: utzoo!utgpu!watserv1!watmath!hyper.hyper.com!bonneau
From: bonneau@hyper.hyper.com (Paul Bonneau)
Subject: Re: What do I do with stale window handles? (GetDesktopWindow() )
Message-ID: <1991Mar8.161702.10783@hyper.hyper.com>
Keywords: Handles, Stale, Release, Destroy
Reply-To: bonneau@hyper.UUCP (Paul Bonneau,,)
Organization: HyperCube Inc.
References: <1991Mar6.215823.3216@ssd.kodak.com>
Distribution: na
Date: Fri, 8 Mar 1991 16:17:02 GMT

In article <1991Mar6.215823.3216@ssd.kodak.com> thomas@ssd.kodak.com (Thomas B. Kinsman (37681)) writes:
>
>    Do I need to get rid of the window handle once I am done with it?
>    If so, how?  Release?  Destroy?
>-------  snippet  ---  snippet  ---  snippet  snippet  ---  snippet  ------
>    if (<something horribly wrong >)
>    {
>HWND                    hwndDisplay;		/* handle to display */
>
>	hwndDisplay = GetDesktopWindow();
>	MessageBox(hwndDisplay,
>		   (LPSTR) "Something went horribly wrong",
>		   (LPSTR) "on a call to : Process(foo, bar)", MB_OK);
>	return FALSE;
>    }

The window handle passed to MessageBox() determines who gets
actived when it returns.  Activiting the desk top window is a
problem since it is only a placeholder (root of the window
tree--also used to display wallpaper, etc.).

Better to call it like:

MessageBox(GetActiveWindow(),...).

Also, consider using the MB_SYSTEMMODAL | MB_ICONHAND since
this guarantees the cal will not fail due to memory shortage
(ie. you want to do this if you are alerting the user to a
low memory situation).

Finally, window handles only become "stale" when the window
is destroyed.  They are not like pointers, in that they do
not become invalid due to changes in memory configuration.
They are in fact memory handles from the USER local heap (or
they were in win 2.0 anyway).  You can check for validaty
with IsWindow().  Furthermore, if the window returned by
GetDesktopWindow() is invalid, you can bet your booties that
Windows has been corrupted beyond all hope (read: this
should never happen!).  Unlike resources, DC's, etc.,
you do not have to "release" a window handle, it is only a
reference.

cheers - Paul Bonneau.
-- 
          +--+
	 /|\/|\
        / +/\+ \
