On occasion calling IsWindowVisible returns FALSE even when the window is still open

Posted on 2004-09-29
Last Modified: 2013-11-20
I have an activeX control that was created using C++.  This control will allow the opening of an existing file, MS Word document for example.  If a user saves changes to the document then once they close that document (or close MS Word) the control will prompt the user on what action to carry out next.

The Problem is, that on occasion a user opens a file using the control but as soon as they save (but not closing the document) the IsWindowVisible returns FALSE implying that the window is no longer available.  So despite the fact that the window is still open the control prompts the user to carry out the next step which shouldn't occur until such times as the user actually closes the document.  

To confirm that this is the problem I put a wait of a few seconds just after the statement while(IsWindowVisible(hWnd)) and then recall IsWindowVisible(hWnd) which it then returns TRUE implying correctly that the window is still open.

Example of steps to reproduce (however not consistently) are once the document is opened by the control the user :
1.  Clicks "Normal View" icon within MS Word
2.  Clicks View (menu) -> Header and Footer (option)
3.  Presses the 'space' button followed (quickly) by pressing CTRL-S to save the document.

Why is IsWindowVisible() returning the wrong result?  Is this a bug with the API call or am I missing something?

The CODE used is:-

void LaunchFile(...) {
while (IsWindowVisible(hWnd))
//Continue since window is now closed...

void PumpMessages()
      MSG msg;

      while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

Thanks in advance,
Martin Golden.
Question by:csf6gom
  • 2
  • 2
LVL 12

Expert Comment

ID: 12187230
Hi! May be IsWindow API may help you?

Expert Comment

by:Amritpal Singh
ID: 12187231
u can have a look at the following link;en-us;820252&Product=winxp

for more info plz look for microsoft article

Author Comment

ID: 12198590
Firstly, by increasing the sleep time from 1ms seemed to reduce the issue however I had to increase it to 500ms before the issue seem to stop occurring.  

As for the IsWindow API it seems to do the trick (at least for MS Word), however, I've been warned that since the IsWindow checks if the handle is still valid then if the window has been closed the handle may still be available due to waiting for garbage collection.  The result here would be that the window appears to be actually closed however it hasn't actually been destroyed yet resulting in a delay until garbage collection has taking place.  I guess this garbage collection issue is probably specific to a particular type of application being used.  I'm not sure what application may work in this manner but since the user may be using any type of application then I can't be sure that using the IsWindow is a safe option.  Can anybody confirm this?  Thanks so far!

Finally, amrit_82, since I'm using Win2000 I don't think this KB issue is of any help.  Thank anyhow!

Thanks in advance!
LVL 12

Accepted Solution

migel earned 250 total points
ID: 12200354
Okay You can install WH_CBT hook and catch window close event :-)

Author Comment

ID: 12215394
Hi Migel,

As my experience is fairly low with C++ API programming I'd appreciate if you could provide an example on using the WH_CBT in conjunction with IsWindow API!

Thanks in advance,

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 117
Whole sheet autoscrub still needed 19 51
matchUp  challenge 6 69
Message not shown 5 67
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question