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

Posted on 2004-09-29
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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 750 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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