Jump to content
RC86

Calling executables Best Practice

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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)

 

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

  • Similar Content

    • By Pike
      Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG!
      To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1"
              Case 1                                                                                                                                             
                  Local $sRead = IniRead("config.ini", "Config", "One", "Default")                                
                  SoundPlay($sRead, @HotKeyPressed)                                                                     <-------------------------------
                  Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
      See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.
       
      Much Respect,
      Pike
       
      Func _HandleButton() $iClickCount += 1 $iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or $iTimer = 0 Then Return Switch $iClickCount Case 1 Local $sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One", $sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch $iClickCount = 0 AdlibUnRegister() EndFunc  

    • By Skeletor
      Hey All,
      Yes, this is an odd request. 
      But is there a way to bulk up the size of the compiled program? 
      I know many want to reduce it's size, but I want to increase it.
      What methods do you suggest?
    • By dimm
      Is it possible to compile script without any icon resources included?
      When I make exe it uses default system icon of current OS even if I compile as console application.
      On other versions of Windows it looks a little strange

       
      Update:
      It seems that the only way is remove icons from exe after compilation using resource editors like ResHacker.

       
      another bad solution:
      Using corrupted 35 bytes icon file. Windows can't load it and shows default system icon.
      #pragma compile(Icon, broken.ico) broken.ico
    • By Pricehacker
      Hello!
      I want to make a script that changes the filesize of program files while still having them run as usual.
      I tried FileWrite() but then the program wouldnt be recognized as a runnable program, could you guys help me to the right path?
    • By HariKara
      Hi AutoIters!
       
      Im trying to launch a .exe file that is nested within the program files (x86) folder structure. i have already used the standard RunAs Syntax and found that it fails to launch the application. I have switched to Run and that seems to work. My issue is I have to use RunAs as the applicaton would need to run under a completely different account. The Current logged in user is a Local User on the machine, however, the application must be run as a domain user. The Machine is domain connected.
       
       have tried the following:
      RunAs("username","logonpassword", $RUN_LOGON_PROFILE, "D:\Program Files (x86)\Vendor\Application Name\Exe Location\Executable.exe") The above fails to launch, there are no errors or syntax issues, it just does nothing when the variables are replaced for the correct values.
      I did the same using the Run command
      Run("D:\Program Files (x86)\Vendor\Application Name\Exe Location\Executable.exe") That seems to work fine, but runs in local user context. Any thoughts? Could it be a local Machine rights issue? Or have i missed something glaring in my script
×
×
  • Create New...