0

MSDN mentions the following remark for the GetWindow() function.

The EnumChildWindows function is more reliable than calling GetWindow in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.

Using EnumChildWindows works just fine when only interested in listing all windows. However, what if you need the additional z-order functionality which GetWindow provides? I can't find all windows ordered by z-order using EnumChildWindows.

Does this mean there is no 'safe' way to do so?

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161

1 Answers1

1

If you need Z-order, I'd use GetWindow(), but guard against the concerns noted.

If you see a window more than once, stop. When subsequently referencing a returned window via its handle, be prepared for failure.

Ed Staub
  • 15,480
  • 3
  • 61
  • 91
  • And how to 'be prepared for failure'? Check whether the window isn't destroyed first? – Steven Jeuris Oct 20 '12 at 20:15
  • Sure, that would work - e.g., try GetParent() just to be sure it's real. It depends on the situation. If the next API call on that window is in the same piece of code, then you may just want to deal with it in error-handling on whatever that call is. If the windows you're dealing with aren't really very dynamic (subject to being destroyed or Z-reordered, you can ignore the warning. – Ed Staub Oct 20 '12 at 20:45