[comp.windows.ms.programmer] What do I do with stale window handles?

thomas@ssd.kodak.com (Thomas B. Kinsman (37681)) (03/07/91)

    In the heat of computation I suddenly discover that something has
    gone wrong.  I don't have a global variable for the window handle
    because I'm encapsulating my routines.

    I want to use MessageBox() to signal the user (me) *what* went
    wrong.  MessageBox() needs a handle to a window.  The following
    snippet of code correctly displays the message box, but later hangs.

    Do I need to get rid of the window handle once I am done with it?
    If so, how?  Release?  Destroy?

    Thanks in advance.

		- Thomas Kinsman		thomas@ssd.kodak.com

-------  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;
    }
-- 
----
thomas@ssd.kodak.com	Voice: 716/253-7681(w)		Fax: 716/253-7443
Thomas B. Kinsman, Eastman Kodak Co., Flr 1, Bldg 10, C, Roch., NY 14653-9011
"Knowledge is what's left when you throw the books away."	- A. Einstein

johnm@spudge.UUCP (John Munsch) (03/08/91)

In article <1991Mar6.215823.3216@ssd.kodak.com> thomas@ssd.kodak.com (Thomas B. Kinsman (37681)) writes:
>	hwndDisplay = GetDesktopWindow();
>	MessageBox(hwndDisplay,
>		   (LPSTR) "Something went horribly wrong",
>		   (LPSTR) "on a call to : Process(foo, bar)", MB_OK);

We had mysterious crashes when we did what you are doing now.  I have no idea
why (Windows works in mysterious ways), but I do have a suggestion for a fix.
Try MessageBox(NULL, ...) instead and I don't believe you'll have any more
problems.
 
John Munsch
"Go buy some more Mac Classics, Apple has lawyers to pay."

bonneau@hyper.hyper.com (Paul Bonneau) (03/09/91)

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.
-- 
          +--+
	 /|\/|\
        / +/\+ \