argumentum

/ErrorStdOut from within ?

2 posts in this topic

#1 ·  Posted (edited)

Let's start with my code, that by my many years of coding, can not crash.

main()
Func main()
    OnAutoItExitRegister("__OnAutoItExit")
    Local $n, $a[6] = [5, 5, 4, 3, 2, 1]
    For $n = 1 To 100
        ToolTip('..crashing in ' & $a[$n], 50, 50, "CrashTest")
        ConsoleWrite('..crashing in ' & $a[$n] & @CRLF)
        Sleep(500)
    Next
EndFunc   ;==>main
Func __OnAutoItExit()
    Local $sExitMethod
    Switch @exitMethod
        Case 0
            $sExitMethod = "closed by natural closing."
        Case 1
            $sExitMethod = "closed by Exit function."
        Case 2
            $sExitMethod = "closed by clicking on exit of the systray."
        Case 3
            $sExitMethod = "closed by user logoff."
        Case 4
            $sExitMethod = "closed by Windows shutdown."
        Case Else
            $sExitMethod = "closed by no clue."
    EndSwitch
    MsgBox(262144, @ScriptName, "I could do something with the error from here" & @CR & _
            @CR & "What is known is that was " & $sExitMethod & _
            @CR & "and the Exit was " & @exitCode & _
            @CR & @CR & " if I had the error string from ""/ErrorStdOut"", I could do more", 60)
EndFunc   ;==>__OnAutoItExit

How can I get the output from "/ErrorStdOut" from within the script ?
I know how to capture from another process ( another script or CMD > redirect.txt )
but that runs another process and there should be a simple way. Help ?, anyone ? 

Edit: maybe a way to read the error type as integer and line# as integer from memory ? not necessarily the output string ? 

Edited by argumentum

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

this is not kosher, but, I can't come up with a more elegant solution

#include <WinAPIProc.au3>

OnAutoItExitRegister("__OnAutoItExit")
If StringInStr($CmdLineRaw, "/ShowMeTheError") Then ShowMeTheError()
If Not StringInStr($CmdLineRaw, "/whatever") Then RunSelf("/whatever", 1) ; rerun to catch errors without a popup gui

Func ShowMeTheError() ; for this demonstration only, I'd handle this event in another way
    MsgBox(262144, @ScriptName & " - ShowMeTheError() example", FileRead(@ScriptDir & '\' & StringTrimRight(@ScriptName, 4) & '.txt'))
    Exit 0
EndFunc   ;==>ShowMeTheError

Func killParentPid_ifCMD()
    Local $iParent, $sParent, $aParent[100][2], $i = 0
    Do
        $iParent = _WinAPI_GetParentProcess($iParent)
        If Not $iParent Then ExitLoop
        $i += 1
        $sParent = _WinAPI_GetProcessName($iParent)
        If $sParent = "cmd.exe" Then
            ProcessClose($iParent) ; because the redirect remains even if killed
        EndIf
        $aParent[$i][0] = $iParent
        $aParent[$i][1] = $sParent
        If $i = 99 Then ExitLoop
    Until Not $iParent
    ReDim $aParent[$i + 1][2]
    $aParent[0][0] = $i
    Return $aParent
EndFunc   ;==>killParentPid_ifCMD

If StringInStr($CmdLineRaw, "/whatever") Then CrashExample()
Func CrashExample()
    killParentPid_ifCMD()
    Local $n, $a[10]
    For $n = 1 To 100
        Sleep(100)
        ToolTip(@MIN & ":" & @SEC & @CR & $a[$n] & " ", 50, 50)
    Next
EndFunc   ;==>CrashExample

Func RunSelf($Cmd = "", $iErrorStdOut = 0)
    If $iErrorStdOut Then
        If @Compiled Then
            $Cmd = StringReplace($Cmd, '/ErrorStdOut ', '')
            $Cmd = '/ErrorStdOut ' & $Cmd & ' >"' & @ScriptDir & '\' & StringTrimRight(@ScriptName, 4) & '.txt"'
        Else
            $Cmd = StringReplace($Cmd, '/ErrorStdOut ', '')
            $Cmd = $Cmd & ' >"' & @ScriptDir & '\' & StringTrimRight(@ScriptName, 4) & '.txt"'
        EndIf
    EndIf
    Local $i = 0
    If @Compiled Then ; Compiled.exe [/ErrorStdOut] [params ...]
        $i = Run(@ComSpec & ' /c ""' & @ScriptFullPath & '" ' & $Cmd & '"', @ScriptDir, @SW_HIDE)
    Else
        $i = Run(@ComSpec & ' /c ""' & @AutoItExe & '" /ErrorStdOut "' & @ScriptFullPath & '" ' & $Cmd & '"', @ScriptDir, @SW_HIDE)
    EndIf
    Return SetError(@error, @extended, $i)
EndFunc   ;==>RunSelf

Func __OnAutoItExit()
    Local $sExitMethod
    Switch @exitMethod
        Case 0
            $sExitMethod = "closed by natural closing."
        Case 1
            $sExitMethod = "closed by Exit function."
        Case 2
            $sExitMethod = "closed by clicking on exit of the systray."
        Case 3
            $sExitMethod = "closed by user logoff."
        Case 4
            $sExitMethod = "closed by Windows shutdown."
        Case Else
            $sExitMethod = "closed by no clue."
    EndSwitch
    ConsoleWrite('OnAutoItExit: ' & @exitCode & ', ' & @exitMethod & ' (' & $sExitMethod & ')' & @CRLF)
    If @exitCode Then RunSelf("/ShowMeTheError")
EndFunc   ;==>__OnAutoItExit

all I've got left is to wait for someone who knows how to use _WinAPI_ReadProcessMemory() to do something nicer. 

Edit: this "solution" creates problems, so, this is not a solution =/

As far as the _WinAPI_ReadProcessMemory() part I'd need a programmer to look at, all the strings can be read from the executable, or memory ?, no clue.

If Not StringInStr($CmdLineRaw, "/ErrorStdOut") Then Exit MsgBox(262144, @ScriptName, "...please run from SciTE editor.", 3)
#include <WinAPI.au3>
;~ Example_WinAPI_LoadString()
Func Example_WinAPI_LoadString() ; part of _WinAPI_LoadString() Example
    Local $hInstance
    For $n = 1 To 100000
        $hInstance = _WinAPI_LoadLibraryEx(@AutoItExe, $LOAD_LIBRARY_AS_DATAFILE)
        If $hInstance Then
            $sText = _WinAPI_LoadString($hInstance, $n)
            If Not @error Then ConsoleWrite($n & @TAB & '(chars: ' & @extended & ')' & @TAB & ' >' & $sText & '<' & @CRLF)
            _WinAPI_FreeLibrary($hInstance)
        EndIf
    Next
    Exit
EndFunc   ;==>Example_WinAPI_LoadString

 

Edited by argumentum
after further experimentation

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