Sign in to follow this  
Followers 0
fusion400

Script that presses OK when error dialog window pops up

7 posts in this topic

How do i write a script that sits and listens in the background and

when a windowtitle called "Microsoft Visual C++ Runtime Library" pops up in front of all windows it automatically presses OK in the dialog window.

Maybe it can check every 30 seconds for this windowtitle is it possible?

This is the standard error message Runtime error! abnormal program termination.

There is a problem with a software that i am using that can not be fixed because the source code is no longer available. And this message pops up every day after several hours of use of the software.

But there is a watcher so if ok is pressed in the dialog box the software will automatically restart.

Thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

There is a million ways to do something like this. Something very simple could be like this..

While 1
   If WinExists("Microsoft Visual C++ Runtime Library") then
      WinClose("Microsoft Visual C++ Runtime Library")
   Endif
   sleep(5000)
Wend
Edited by Beege

Share this post


Link to post
Share on other sites

There is a million ways to do something like this. Something very simple could be like this..

While 1
   If WinExists("Microsoft Visual C++ Runtime Library") then
      WinClose("Microsoft Visual C++ Runtime Library")
   Endif
   sleep(5000)
Wend

Thanks alot this works perfectly.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I have an Autoit mailbox monitor script that runs to monitor for emails. If this script errors out for whatever reason, Autoit would generate an error. At that point I would need to click ok and then restart the script. The way I handled it is with another script to monitor the mailbox monitor. If it sees the mailbox monitor script/process running, it will not do anything, but if it does NOT see that process it will click the ok button and restart the script. Here is the code:

;The purpose of this script is to restart the Mailbox Monitor.exe script if something causes it to error out.
;When this happens, it is most likely due to Microsoft Exchange Server going offline for some reason.

Global $i
Global $begin
Global $dif
Global $ProcessName
Global $Notifications_Sent_Counter

AutoItSetOption("WinTitleMatchMode", 4)

$Notifications_Sent_Counter = 0

;Initiates an infinite loop
$i = "InfiniteLoop"
While $i <= 1
    ;Timer is reset to 0.
    $begin = 0
    ;Initializes timer
    $begin = TimerInit()

    If ProcessExists($ProcessName) Then
        ;MsgBox(0,"Running",$ProcessName & " is running.")
        ;Nothing
    Else
        ;MsgBox(0,"Not Running",$ProcessName & " is not running.")
        ;Runs the Mailbox Monitor.exe file.
        Run ("G:\Reporting\Autoit\AUTOMATED REPORTS\EXEs\Mailbox Monitor.exe")
    EndIf
    
    ;MsgBox(0, "Value of $i is:", $i)
    ;Waits on the "AutoIt Error" in case the Mailbox Script error.  If it error, this will be the title of the window
    ;When prompt pops up, the "Yes" button is clicked
    WinWait("AutoIt Error")
    ;Activates the window
    WinActivate("AutoIt Error")
    sleep(1000)
    AutoItSetOption("WinTitleMatchMode", 4)
    ;Sends click to the OK box.  This will close the window so that the Mailbox Monitor.exe exits.
    ;Send("{ENTER}")
    controlclick("AutoIt Error","","Button1")
    sleep(2000)

    ;$ProcessName = "Notepad.exe"
    $ProcessName = "Mailbox Monitor.exe"
    ;Tests to see if Mailbox Monitor script is running.  If it is, then it does not need to be restarted.
    If ProcessExists($ProcessName) Then
        ;MsgBox(0,"Running",$ProcessName & " is running.")
        ;Nothing
    Else
        ;MsgBox(0,"Not Running",$ProcessName & " is not running.")
        ;Runs the Mailbox Monitor.exe file.
        Run ("G:\Reporting\Autoit\AUTOMATED REPORTS\EXEs\Mailbox Monitor.exe")
        sleep(2000)
        
        ;Retrieves amount of time timer has been running
        $dif = TimerDiff($begin)
        ;MsgBox(0,"Time Difference",$dif)
        ;Tests to see if timer has been running for more than 30 minutes.  If it has, it means a notification email has not been sent for at least the last 30 minutes
        If $dif > 1800000 Then
            ;Since so much time has elapsed, even if a notification email has been sent, this is a very acceptable amount of time to have elapsed to prevent spamming of the mailbox
            ;Since this is very acceptable, the $Notifications_Sent_Counter is set back to 0.  This is necesarry because over time, the $Notifications_Sent_Counter will accumulate counts and eventually cause the limit to be triggered.
            $Notifications_Sent_Counter = 0
        Else
            ;Nothing - because this means the last email notification went out within the last 30 minutes.  This is an unacceptable amount of time so the $Notifications_Sent_Counter is incremented.
        EndIf
        
        ;Tests the $Notifications_Sent_Counter variable.  If less than 30 emails have been sent, the ScriptRestartEmailNotification procedure is called.
        ;After the email is sent, the loop will pause for 2 minutes (120000 miliseconds = 2 minutes) to allow for Exchange server to come back online.
        ;At the current delay rate of 2 minutes and a limit of 30, the program can error out before 1 hour before reaching the limit.
        ;Once the limit is reached, email notifications cease, however the script will continue to loop.  This will prevent the mailbox from being flooded
        If $Notifications_Sent_Counter < 30 Then
            ;The below procedure will send out a notification that the Mailbox Monitor.exe script has errored and been restarted.
            Call("ScriptRestartEmailNotification")
        Else
                ;Nothing - This means the notification email has been sent more than 30 times, so no more will be sent until more than 30 minutes elapses from an error.
        EndIf
        ;Increments the $Notifications_Sent_Counter variable +1.  This way it is possible to track how many emails have been sent in.
        ;This is important so that a limit can be set on the number of emails sent in order to prevent spamming the CAR/TN Reporting mailbox.
        $Notifications_Sent_Counter = $Notifications_Sent_Counter + 1
        ;Waits 2 minutes before resuming.  This is to make sure that if some unhandled error occurs, the mailbox will not be spammed with emails.
        sleep(120000)
    EndIf
WEnd

func ScriptRestartEmailNotification()
        ;Creates and sends Notification email letting us know that the script errored and restarted
        $Application = ObjCreate("Outlook.Application")
        $Session = objCreate("Redemption.RDOSession")
        $Session.MAPIOBJECT = $Application.Session.MAPIOBJECT

        $Drafts = $Session.GetDefaultFolder(16)
        $Msg = $Drafts.Items.Add
        ;$Msg.To = "blank@blank.com"
        $Msg.To = "blank@blank.com"
        $Msg.Recipients.ResolveAll(true)
        $Msg.Subject = "Mailbox Monitor Script Restarted"
        $Msg.HTMLBody = "An error occured with the Autoit Mailbox Monitor script.  Script has been restarted.<br>"
        ;$Msg.VotingOptions = "Chinese;Italian;Mexican;Don't care"
            ;The attachments code may not work
            ;$Msg.Attachments.add ("C:\vcredist.bmp",$olByValue,1,"picture fail")
        $Msg.Importance = 2
        $Msg.Send
        
        ;Sets the $ACIR_NewMonth_Created_Ind to 0
        $ACIR_NewMonth_Created_Ind = 0
endfunc
Edited by hutteto

Share this post


Link to post
Share on other sites

Please use AutoIt tags around your code. They are easily accessed in the post editor by the little blue button on the left.

Ah I see. Thanks. I was wondering why my code wasnt keeping its formatting and looking like the others I've seen.

Share this post


Link to post
Share on other sites

You're welcome.

I Do not see where your script would crash, but I do see a couple places where it will not do as intended.

If $dif > 1800000 Then

The timer will have elapsed about 5 seconds every time this is reached, because you initialize the timer inside the while loop, and wait for nothing but pre-defined Sleep calls.

You probably want to use...

RunWait ("G:\Reporting\Autoit\AUTOMATED REPORTS\EXEs\Mailbox Monitor.exe")

Instead of...

Run ("G:\Reporting\Autoit\AUTOMATED REPORTS\EXEs\Mailbox Monitor.exe")

because runwait waits for the process to close.

I also noticed some bad Practice in the code

I don't mean to be nit-picky, but knowing about some of these will save you coding time in the future.

$i = "InfiniteLoop"
While $i <= 1

You should instead use..

While 1

You are already initializing the timer using TimerInit right after this, you do not need to do this (as it has no effect)

;Timer is reset to 0.
    $begin = 0

You do not need to use Call to call an internal function.

You can just do...

ScriptRestartEmailNotification()

Instead of...

Call("ScriptRestartEmailNotification")

Replace...

If ProcessExists($ProcessName) Then
        ;MsgBox(0,"Running",$ProcessName & " is running.")
        ;Nothing
    Else

with...

If Not ProcessExists($ProcessName) Then

And in a couple places...

Else
                ;no code here...

You do not need an else condition on all If Statements

-Matt


_________[u]UDFs[/u]_________-Mouse UDF-Math UDF-Misc Constants-Uninstaller Shell

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  
Followers 0