Jump to content

Check if window exists with WinGetHandle ?


VAN0
 Share

Recommended Posts

Hello.

My understanding that if window doesn't exist, WinGetHandle() returns 0x00000000, which pretty much equal to "false", this raises two questions for me:

1) why do we need WinExists()?

2) why do we need @error flag in WinGetHandle()?

Edited by VAN0
Link to comment
Share on other sites

@VAN0

To see if a Window exists, why don't you use directly WinExists()?

WinGetHandle() is used when you need to "manage" your window (move, use Control* functions...).

So, you can use WinExist() to check if a window exists or not.

@error (and @extended in some cases) is an AutoIt macro which tells you what has going on when you called a function; in almost every case, it will be 0 if no error occurred, otherwise it will be non-zero (you can find the reference of all error possible values in the Help file, of the specific function you are calling).

It's a good macro because you can debug your code, and, for example, in case of error, decide to exit your script.

So, that's it :)

 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

WinGetHandle returns DOES NOT return a null pointer. only if successful does it return the handle (pointer to) the window

Return Value

Success: a handle to the window.
Failure: sets the @error flag to non-zero if the window is not found.

Remarks

This function allows you to use handles to specify windows rather than "title" and "text".
Once you have obtained the handle you can access the required window even if its title changes.

 

return is NOT a pointer unless it is VALID!!! so, your understanding wrong. if failure, it sets @error flag to non-zero, so that is a lot more checking than a WinExists

 

 

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

6 hours ago, Earthshine said:

WinGetHandle returns DOES NOT return a null pointer. only if successful does it return the handle (pointer to) the window

So? If it doesn't return handle, it means window doesn't exist, which is exactly my point...

Here is a little example:

 

#include <GUIConstantsEx.au3>
$hGUI = GUICreate("Existing window")
GUISetState(@SW_SHOW, $hGUI)
$we1 = WinExists("Existing window")
$we2 = WinExists("Non-existing window")
$wgh1 = WinGetHandle("Existing window")
$wgh2 = WinGetHandle("Non-existing window")
ConsoleWrite("WinExists(existing) = " & ($we1 ? true : false) & ": (" &  $we1 & ")" & @CR)
ConsoleWrite("WinExists(non-existing) = " & ($we2 ? true : false) & ": (" &  $we2 & ")" & @CR)
ConsoleWrite("WinGetHandle(existing) = " & ($wgh1 ? true : false) & ": (" &  $wgh1 & ")" & @CR)
ConsoleWrite("WinGetHandle(non-existing) = " & ($wgh2 ? true : false) & ": (" &  $wgh2 & ")" & @CR)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Exit

 

Both WinExists() and WinGetHandle() can be used in If .. .Then without need of @error

 

So my point is, WinGetHandle practically replaces WinExists and @error is redundant as it doesn't contain any more useful information then a 0x00000000 handle returned when window doesn't exist

Unless of course WinGetHandle is super inefficient compare to WinExists...

Edited by VAN0
Link to comment
Share on other sites

Whatever floats your boat. Winexists is still easier. If it makes you happy though. Your understanding was still incorrect. And you could do testing on your own to see what’s more efficient

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...