Jump to content

Calling executables Best Practice


RC86
 Share

Recommended Posts

Afternoon!

Just a quick one as this has dawned on me recently when creating a little program.  When calling an executable I've created like Run(otherapp.exe) from within my executable is there a best practice to ensure things have gone smoothly?  So for example, should I monitor the PID to ensure it runs and closes within an acceptable timeframe?  Or within my other executable should I do EXITs in a certain way after functions and return codes etc?

Could be a silly question but thought I'd ask.

Link to comment
Share on other sites

  • Moderators

Most definitely you should be doing some level of error checking when calling an external application from your script. The precise method depends, of course, on the external executable being run, and what the result of its running produces (a new file on the hard drive, an entry in the Event Viewer, a new table in a database, etc.).

As for running an Exit, just realize that is a hard stop. Again, without a hard example of a script you're working on it is difficult to say what you should do in every case. The big question regarding exit code on the script is, are you going to be reporting on that code somewhere else? If there is benefit to it exiting with a specific code as identified by you, then by all means include it. Otherwise, again, you can look at writing to a log file or the Event Viewer for run history reporting.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

OK, noted that sounds good!  So I've inserted a bit of code below just as an example...I have 2 exes that I have created.  First one is used for creating performance monitor stuff and starting stopping etc.  Second one has audit functionality.  Every now and then I want to use one executable to call the other one.  So all I'm doing is something like a run.  Now of course, I have put error logging properly in both executables but I was just wondering if there's a good way to pass this between executables?  Also I was concerned if the performance one failed to respond or stop accordingly for any reason.  That type of thing really.  

<PerformanceCounters.au3> - compiled to performance.exe

Func _logmanStart()
    Local $iPID, $sCommand
    Local $sOutput = ""
    Dim $configIni, $outputPath
    $collectorName = "Test"

    $sCommand = "logman.exe start " & $collectorName
    $iPID = Run('"' & @ComSpec & '" /c' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

    While 1
        $sOutput = StdoutRead($iPID)
        ; Exit the loop if the process closes or StdoutRead returns an error.
        If @error Then ExitLoop
        Select
            Case StringInStr($sOutput, "Cannot create a file when that file already exists.")
                SetError(1,10)
            Case StringInStr($sOutput, "Data collector set was not found.")
                SetError(1,11)
            EndSelect
        WEnd
EndFunc

<Audit.au3> - compiled to audit.exe

Run("performance.exe" /start)

 

Link to comment
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
 Share

×
×
  • Create New...