Suppose I am writing a function that, by descriptor value, should return a boolean value if there is a window with such a descriptor. I present a huge number of implementations - from iterating through all windows through GetProcesses or EnumWindow and to calling some function that accepts hwnd and returns the result. Or maybe I'm complicating everything, and such a function is included in user32. In general, how will be better and faster?
1 answer
There is a WinApi function IsWindow , but as written in this answer, the handle can be reassigned => if you pass a certain window and want to check its existence, the window may already be closed, but the handle with the same number was created for another window.
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindow(IntPtr hWnd); It turns out that it makes sense to use this function here, if the window has a specific name GetWindowText , it will return the Caption window and you can make sure that the desired window is alive.
Through WinApi, I think it's faster.
What's better?
It all depends on how often you are going to do it. If not often, then I would not bother and iterate through the processes.
- Thanks, lost sight of IsWindow! - Uranus
|