Jump to content
Sign in to follow this  
reikenberg

WinExits works, WinActivate fails!?

Recommended Posts

reikenberg

Hello,

i want to automate a window. If i run the window on XP and try to control it with WinExits and WinActivate it works without problems. If i run it on a Win2003 Server RC2 running on VMware WinExists works but WinActivate always fails. But controlling checkboxes for example works on both systems.

I don´t know know how to slove this problem, any advise?

Sincerly

Rigoletto

Share this post


Link to post
Share on other sites
Valuater

Tut - Ta - da - daaa!!

Run("notepad.exe")
WinWaitActive("")

$Title = "Untitled -"

While WinExists($Title)
    If WinExists($Title) Then
        if WinActive($Title) Then
            ; do your stuff
        Else
            WinSetState("", "", @SW_MINIMIZE)
        EndIf
    EndIf
    Sleep(2000)
WEnd

I have been on this for move than 1/2 hour and I tried "EVERYTHING"

.... Finally got this to work!!

@Salty, I noticed you have been here a while too - nothing else seems to work, how about for you?

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
reikenberg

Hello,

i trying around and find out that WinActivate only works when somebody is watching at the desktop where the window that should be controlled exists. Via RDP for example. But if the RDP window minimized WinActivate always fails.

Here is our complete code:

#NoTrayIcon
#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=P:\TC_Auto\InERP_BP_Watcher.exe
#AutoIt3Wrapper_Res_Fileversion=1.0.0.20
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_Language=1031
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****
#Include <Constants.au3>

Opt("CaretCoordMode", 0)
Opt("MouseCoordMode", 0)
Opt("SendKeyDelay", 1000)

Dim $crlf = chr(13) & chr(10)
Dim $rv
Dim $parameter
Dim $focus
Dim $clipboard
Dim $exititem
Dim $infoitem
Dim $app_to_watch = "Batch Printer"
Dim $app_text
Dim $temp[10]

Dim $szIPADDRESS = @IPAddress1
Dim $nPORT = 62222
Dim $MainSocket, $szIP_Accepted
Dim $ConnectedSocket = -1
Dim $msg, $recv

If $CmdLIne[0] = 1 Then
    $parameter = $CmdLine[1]
EndIf

init()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            If Not WinExists($app_to_watch) Then
            ;MsgBox(0, "Zu überwachendes Fenster fehlt", $app_to_watch)
            EndIf
            
            If $ConnectedSocket = -1 Then
                $ConnectedSocket = TCPAccept($MainSocket)
            EndIf
            If $ConnectedSocket >= 0 Then
                $szIP_Accepted = SocketToIP($ConnectedSocket)
                $recv = TCPRecv($ConnectedSocket, 2048)
                if $recv = "" and @error = "10054" then
                    $ConnectedSocket = -1
                EndIf
            EndIf

                if $recv <> "" Then
                    $app_text = $recv
                    
                    Select
                        Case $app_text = "start batchprinter"
                            start_batchprinter()
                        Case $app_text = "stop batchprinter"
                            stop_batchprinter()
                        Case $app_text = "config batchprinter"
                            config_batchprinter()
                        Case $app_text = "exit batchprinter"
                            exit_batchprinter()
                        Case $app_text = "load batchprinter"
                            load_batchprinter()
                        Case $app_text = "info batchprinter"
                            info_batchprinter()                             
                    EndSelect
                    
                EndIf

            Sleep(5000)
        Case $msg = $infoitem
            MsgBox (0,"Info","Version: " & FileGetVersion(@AutoItExe))
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)
TCPShutdown()

Exit

Func info_batchprinter()
    If WinActivate($app_to_watch) Then
        $temp[1] = ControlGetText($app_to_watch,"","Edit1");Datum letzter Druck
        $temp[2] = ControlGetText($app_to_watch,"","Edit2");Uhrzeit letzter Druck
        $temp[3] = StatusbarGetText($app_to_watch,""); Statuszeile
        
        $app_text = $crlf
        $app_text = $app_text & "Letztes Druckdatum: " & $temp[1] & $crlf
        $app_text = $app_text & "Letzte Druckzeit: " & $temp[2] & $crlf
        $app_text = $app_text & "Status: " & $temp[3] & $crlf
        $app_text = $app_text & $crlf
        $temp = MemGetStats()
        $app_text = $app_text & "Speicher gesamt: " & $temp[1] & $crlf
        $app_text = $app_text & "Speicher frei: " & $temp[2] & $crlf
        
        Run("net send " & $szIP_Accepted & " " & $app_text)
    Else
        $app_text = $crlf
        $app_text = $app_text & $app_to_watch & " reagiert nicht oder ist nicht gestartet." & $crlf & @extended
        Run("net send " & $szIP_Accepted & " " & $app_text)
    EndIf
EndFunc

Func start_batchprinter()
    $rv = WinActivate($app_to_watch)
    If $rv = 1 Then
        MouseClick("left",20,40,1,100); start
    EndIf
EndFunc

Func stop_batchprinter()
    $rv = WinActivate($app_to_watch)
    If $rv = 1 Then
        MouseClick("left",60,40,1,100); stop
    EndIf
EndFunc

Func config_batchprinter()
    $rv = WinActivate($app_to_watch)
    If $rv = 1 Then
        ControlCommand($app_to_watch,"Verkauf","Button2","Check")
        ControlCommand($app_to_watch,"Einkauf","Button4","Check")
        ControlCommand($app_to_watch,"Produktion","Button5","Check")
        ControlCommand($app_to_watch,"Andere","Button6","Check")

        ControlCommand($app_to_watch,"06:00","Button7","UnCheck")
        ControlCommand($app_to_watch,"09:00","Button8","UnCheck")
        ControlCommand($app_to_watch,"12:00","Button9","UnCheck")
        ControlCommand($app_to_watch,"15:00","Button10","UnCheck")
        ControlCommand($app_to_watch,"18:00","Button11","UnCheck")
        ControlCommand($app_to_watch,"21:00","Button12","UnCheck")

        ControlFocus($app_to_watch,"","Edit3")
        Send("l")
        send("{TAB}")
        
        while ControlGetText($app_to_watch,"","Edit4") <> "8"
            ControlFocus($app_to_watch,"","Edit4")
            If Number(ControlGetText($app_to_watch,"","Edit4")) > 8 Then
                Send("{DOWN}")
            Else
                Send("{UP}")
            EndIf
        WEnd
    EndIf
EndFunc 

Func load_batchprinter()
    If Not WinExists($app_to_watch) Then
        Run("P:\Prod\INERP\GUI\BatchPrinter_D.exe","P:\Prod\INERP\GUI",@SW_MAXIMIZE)
        WinWait("Anmeldung")
        ControlCommand("Anmeldung","OK","Button1","Check")
        WinWaitActive($app_to_watch,"")
        config_batchprinter()
        start_batchprinter()
    EndIf
EndFunc

Func exit_batchprinter()
    $rv = WinActivate($app_to_watch)
    If $rv = 1 Then
        WinClose($app_to_watch)
    EndIf
EndFunc

Func init()
    TCPStartup()
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)
    If $MainSocket = -1 Then
        MsgBox (0,"Fehler","Kommunikation konnte nicht aufgebaut werden.")
        Exit
    EndIf
    
    Opt("TrayMenuMode",1)  ; Default tray menu items (Script Paused/Exit) will not be shown. 
    $exititem      = TrayCreateItem("Exit")
    TrayCreateItem("")
    $infoitem      = TrayCreateItem("Info")
    TraySetState() ; Show the tray icon
    TraySetToolTip("InERP BatchPrint Watcher")
EndFunc

Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet
    
    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc  ;==>SocketToIP

Share this post


Link to post
Share on other sites
SmOke_N

So check and change the state of the window before hand:

pseudo:

Func _my_WinActivate($h_wnd, $s_text = "")
    If BitAnd(WinGetState($h_wnd, $s_text), 16) Then
        WinSetState($h_wnd, $s_text, @SW_RESTORE)
        Return WinActivate($h_wnd, $s_text)
    EndIf
    Return WinActivate($h_wnd, $s_text)
EndFunc

Edit:

Only had it activating window if it was minimized... ^_^

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
reikenberg

Hello,

yes it restore the window, but not give focus to it, even if it´s minimized.

It seems that a window on a desktop with no "focus" (no user using the desktop via rdp, etc.) cannot get focus.

Looks not like an autoit-problem, i have access runtime application that have problem with desktops without "focus" too.

I think some needed response from windows is not given if the desktop has no user.

I could try a workaround and open a rdp-session, but that´s worse solution.

Share this post


Link to post
Share on other sites
ebbie

Hello,

yes it restore the window, but not give focus to it, even if it´s minimized.

It seems that a window on a desktop with no "focus" (no user using the desktop via rdp, etc.) cannot get focus.

Looks not like an autoit-problem, i have access runtime application that have problem with desktops without "focus" too.

I think some needed response from windows is not given if the desktop has no user.

I could try a workaround and open a rdp-session, but that´s worse solution.

Years ago this problem had been a transient problem for me. I used a work around but I never understood how or why WinActivate failed. Just recently WinActivate became a problem again. WinActivate struggles with Window titles.

Lucky for me WinActivate struggled with a program that I wrote. I renamed the Window title of my program to a small simple word "runme" and just that change alone fixed the problem. No other changes. The former title was much longer and used an ampersand towards the end. I didn't explore beyond that but I wonder what was in that former title that caused problems? Perhaps there is a bug in WinActivate.

Ebbie

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.