Jump to content
Sign in to follow this  

string comparison of window functions broken (maybe newline?)

Recommended Posts

Hi All,

Wanted to do something very easy with latest Autoit3 and x64 mode enabled on Win 8.1 64bit.

Wanted to see if current windows is "Chrome" is active. So here's what I tried

#include <MsgBoxConstants.au3>
Local $wnd = WinActive("[ACTIVE]")
If @error Then
   MsgBox($MB_SYSTEMMODAL, "", "An error occurred when trying to retrieve the current window handle.")
Local $wndText = WinGetText($wnd)
If StringInStr($wndText,"Chrome Legacy Window") Then
    MsgBox(1,"Info","Chrome found")

This works. But, why do I have to use "StringInStr" to check $wndText Variable. I also tried normal string comparison like this:

If $wndText = "Chrome Legacy Window" Then

If just doesn't match. I tried with various windows functions like WinGetClassList and so on, all of them need this "StringInStr" workaround. Also this doesn't work

If  WinActive("[CLASS:Chrome_RenderWidgetHostHWND]") Then

Although I used WinGetClassList on $wnd to get it's class.

Do you have any idea what's going on here? Why do the strings not match and why does WinActive Not match the class which I got from WinGetClassList for $wnd? Any Ideas. I'm a bit lost although I'm no newbie with autoit3, but havent used it for quite some time now.

Please also forgive me if this question has been asked before, but I couldn't find any hint on FAQ Page or by searching.

Share this post

Link to post
Share on other sites

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post

Link to post
Share on other sites

WinGetClassList retuns a list containing the class for each control found in a window.
In your case, you want to work with the window's class : maybe _WinAPI_GetClassName could be useful.

An another way could be to check the process name of the active window (even if i prefer the window class method in the case of the user renames the exutable).

$iActiveWinPid = WinGetProcess("[Active]")
If _ProcessName($iActiveWinPid) = "chrome.exe" Then ConsoleWrite("Active Window is chrome")

Func _ProcessName($iPid)
    Local $aProcessList = ProcessList()
    For $i = 1 To $aProcessList[0][0]
        If $aProcessList[$i][1] = $iPid Then Return $aProcessList[$i][0]
    Return ""


Share this post

Link to post
Share on other sites

And why not the ultra simple solution suggested by boththose ?

Opt("WinTitleMatchMode", -2)

If WinActive("chrome") Then ConsoleWrite("Active Window is chrome")


​Well, As the Title of Chrome is always matching the currently active tab. You never know the title especially as the suffix ".... - chrome" is totally missing. So you either have to match via Class or by Text (which is strangely "Chrome Legacy Window"). The only new Idea was to match by PID which might be also a good idea.

So, WinTitleMatchMode won't help I guess as it's only for window title by not class or text right?

Plus, I still have no answer why a simple "If $wndText = "Chrome Legacy Window" Then ..." doesn't match (where $wndText is the text returned by WinGetText()), same is true for WinGetClasslist, If a print out the string from "WinGetClassList" and make an If statement I have exactly the same problem.

If I remember correctly this used to work in earlier windows / autoit versions. Makes it a bit hard if you always have to work with regexes and/or StringInString functions and stuff like that. BTW: is there a builtin function which only strips trailing cr, lf and cr &lf from a string or has it to be done by a regex as well.

Share this post

Link to post
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
Sign in to follow this  

  • Create New...