Jump to content
Juvigy

How to do an action if Autoit is closed by taskmanager?

Recommended Posts

Juvigy

Any way i can detect if my exe is 'end tasked' ? I would like to display a msgbox or write a log file just before the task is closed.

Share this post


Link to post
Share on other sites
javiwhite

OnAutoITExitRegister sets @ExitCode & @ExitMethod. I would suggest playing around with that, And seeing how you get on.

Cheers

Javi


give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Share this post


Link to post
Share on other sites
Jewtus

If you close something with task manager, it halts the process and wouldn't register an exit message if I'm not mistaken.

Share this post


Link to post
Share on other sites
Juvigy

If you close something with task manager, it halts the process and wouldn't register an exit message if I'm not mistaken.

​Yes , indeed . So how to do it then?

Share this post


Link to post
Share on other sites
javiwhite

Ahh i see your problem now.

A quick google of the subject shows that OnAutoITExitRegister func does not handle script crashes or force closures (Such as task manager). I would advise you have a sub process that monitors your script and is called on startup, Then add an OnExit event that creates a flag with the exit method. If the process no longer exists and the monitor picks it up, It can check for the flag, and if the flag does not exist, The script has either crashed, or been forcefully closed.

A working example:

Main.exe:

OnAutoItExitRegister("exitfunc")

ShellExecute(@WorkingDir & "\TaskMonitor.exe",@AutoItPID)
sleep(10000)

func exitFunc()
    FileWriteLine(@WorkingDir & "\ExitMethod.flg",@exitMethod)
EndFunc

TaskMonitor.exe:

if $CMDLINE[0] = 0 then Exit

while 1
    sleep(10)
    if ProcessExists($CMDLINE[1]) then ContinueLoop
    If FileExists(@WorkingDir & "\ExitMethod.flg") then
        MsgBox(0,0,"Exited via normal method")
        FileDelete(@workingDir & "\ExitMethod.flg")
    Else
        Msgbox(0,0,"Force closed or crashed")
    EndIf
    
    Exit
WEnd

 

Obviously that's a rudimentary example, But hopefully it helps get you on the right tracks.

 

Cheers

Javi

 

EDIT: added file names for example.

Edited by javiwhite

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

Share this post


Link to post
Share on other sites
Jewtus

I'd say @javiwhite has the right idea, but doing it from within the script itself I don't think is possible. I have some scripts on my computer that monitor for when my services crash and automatically restart them (if the process doesn't exist, then it sends a force close to the process name just in case its stuck, then delays 10 seconds and restarts the service and waits 2 minutes).

Share this post


Link to post
Share on other sites
Juvigy

I think i remember that it was done some time ago , but cant find the thread. It was using some kind of interception of windows messages.

Share this post


Link to post
Share on other sites
Jewtus

Something like this?

This would do essentially what you are talking about, but again, its a different application because when you end a thread on task manager, you would need another application to catch the message because the thread has been halted and can no longer perform any functions. Its pretty easy to pack each script as an exe then create a launcher so the launcher can run in the background and capture exit codes and errors. I've even got compile commands set in some of my scripts to compile the subapps so I can deploy everything all at once. If you want an example I'll post it.

Share this post


Link to post
Share on other sites
UEZ

@Jewtus: the functions from the link are now integrated to the _WinAPI_* lib. E.g. _WinAPI_GetParentProcess.

 

  • Like 1

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Juvigy

@Jewtus: I will have a look. Basically what the elegant way was doing was to intercept the 'end task' , and prevent it from executing. Then write a log / display msgbox and shutdown the app.

Share this post


Link to post
Share on other sites
zreo15

@Jewtus: I will have a look. Basically what the elegant way was doing was to intercept the 'end task' , and prevent it from executing. Then write a log / display msgbox and shutdown the app.

​well i guess this is how you create virus if you can manage to interupt it and prevent from executing end task...... curious anyone willing to share how it can be done

Edited by zreo15

Share this post


Link to post
Share on other sites
Jewtus

Isn't using autoit for game bots and malware against the rules?

Juvi, out of boredom I did some searching and from what I see about other programming languages and executions on windows, the only things that cannot be end tasked are windows services (native windows services... so creating a service might not work) since windows 98.

<snip>

Edited by Melba23
Suggestion removed

Share this post


Link to post
Share on other sites
Melba23

Juvigy,

Basically what the elegant way was doing was to intercept the 'end task' , and prevent it from executing.

 Not something we want to discuss here - thread locked.

M23

  • Like 1

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Similar Content

    • ur
      By ur
      I have created an exit function and registered using OnAutoItExitRegister.But it is not running when I close the app from taskbar or even with normal exit during end.
       
      Below is the part of code and the included files.
      #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <WinAPIFiles.au3> #include <Date.au3> #include <file.au3> #include <Array.au3> #include <String.au3> OnAutoItExitRegister("Exit_Program") Func Exit_Program() LoggingMainScr("Entered Exit_Program") Local $exitmethod = ["Natural closing", "close by Exit function", "close by clicking on exit of the systray","close by user logoff","close by Windows shutdown"] ExitProgram("Exit with exit code: "& @exitCode & " And because of: " & $exitmethod[@exitmethod],@exitmethod) EndFunc Func ExitProgram($sReason="Exit by user",$sCode=0) LoggingMainScr($sReason) if $sCode <> 0 then LoggingMainScr("stopped before completion") FileDelete(@ScriptDir&"\Machines*.txt") FileDelete(@ScriptDir&"\PsExec.exe") FileDelete(@ScriptDir&"\HyperVMachines.ps1") FileDelete(@ScriptDir&"\VMwareMachines.ps1") EndFunc Func LoggingMainScr($sMessage) LoggingDefault($sMessage,@ScriptDir&"\MainScr_Logging.log") EndFunc Func LoggingDefault($sMessage,$sLogFile=@ScriptDir&"\Logging.log") If $sMessage = "" Then FileWriteLine($sLogFile, "") Else FileWrite($sLogFile,_NowCalc() & " :: " & $sMessage&@CRLF) EndIf EndFunc Any suggestion.
       
    • AndreyS
      By AndreyS
      Tell me why when I shut down Windows, OnAutoItExitRegister function is activated twice?
      This is an example of the reference
      #include <MsgBoxConstants.au3> OnAutoItExitRegister("MyTestFunc") OnAutoItExitRegister("MyTestFunc2") Sleep(1000) Func MyTestFunc()     MsgBox($MB_SYSTEMMODAL, "Exit Results 1", 'Exit Message from MyTestFunc()') EndFunc   ;==>MyTestFunc Func MyTestFunc2()     MsgBox($MB_SYSTEMMODAL, "Exit Results 2", 'Exit Message from MyTestFunc()') EndFunc   ;==>MyTestFunc2
       
    • fosil
      By fosil
      Hi all,

      I'm working on a relatively complex automation program and I need it to call a specific function when it is closed by a user. Ive resorted to "OnAutoItExitRegister" as i believe that's the only option available.

      This command works perfectly fine in a small scale test application I wrote, but once implemented in the final program that's much larger and divided over many files in many directories it no longer works. I don't get any error messages.

      My question is has anyone encountered an issue where implementation of this command in a large scale program gave them trouble? Does it matter where "OnAutoItExitRegister" is called and where the function is located? Currently they are both places in the main file that runs the whole program but it still doesn't work.

      Any help is appreciated
    • Daeth
      By Daeth
      When I try to compile the following code:
      #NoTrayIcon #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Run_Au3Stripper=y #Au3Stripper_Parameters=/rm #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** OnAutoItExitRegister("_exit") Func _exit() ConsoleWrite("kek") EndFunc The stripped version has the _exit() function stripped. The stripped code:
      #NoTrayIcon OnAutoItExitRegister("_exit") When I check the Au3Stripper log it shows:
      0.00 AutoIt3 Source Au3Stripper v16.612.1119.0 Copyright © Jos van der Zande June 12,2016 0.00 CommandLine Params$: 0.00 ============================== Reading Source For Directive ====================================================== 0.00 Ignore Function: onautoitstart 0.00 Ignore Function: onautoitexit 0.00 Directive Params$:/rm 0.00 lCMD$:/rm 0.00 /RenameMinimum Is this a bug? I know I can fix this with '#Au3Stripper_Ignore_Funcs=_exit'.
      This error also occurs with HotKeySet. The function that HotKeySet points to is stripped.
×