(Edited in response to comments below.)
There are a couple of steps to this: first, actually run the script; second, check if it successfully ran (both that it managed to start a new process and that that process succeeded.)
Part 1, running the script:
To run the script from your Delphi app, use ShellExecuteEx
with the "open
" verb should work (more details in flags below.) You could execute the script itself if it's associated with your Perl executable, or run your Perl exe with the script as a command line argument.
This article looks like a good starting point for using this API and its simpler cousin ShellExecute
.
The ShellExecute[Ex]
functions usually expect your application to run a message loop afterwards. It's unlikely your application isn't, so long as after the user clicks the button to start this your app sits idle, but if it isn't going to for some reason you should pass the SEE_MASK_NOASYNC
flag. MSDN has lots of info.
Part 2, checking if it executed successfully and succeeded in its task:
To see if the script executed, you should get a process handle to the started process and, if there was a new process, check the exit code from that. You should modify your script to return a non-zero code if its task fails. (I'm not familiar with Perl so I can't specifically tell you how, but generally it's the return value from the "main" function or other first/startup function.) You can do this by:
- Pass in the
SEE_MASK_NOCLOSEPROCESS
flag to ShellExecuteEx
. That will fill the hProcess
member of the struct you pass in with a process handle, if it successfully started a process (if not, check GetLastError
to find out why.) If this is all you need to know (just if it started, nothing about if it worked) then you're done.
- Optional: wait for the process to end by using
WaitForSingleObject(YourParamStruct.hProcess, INFINITE)
(if you got a valid hProcess, of course)
- Call
GetExitCodeProcess
to find the exit code. (Check for it returning STILL_ACTIVE
, and if it is still running wait and try again.) Normally, it should exit with 0
to indicate success. You should modify your script so that if something goes wrong it returns non-zero. You can make the codes as simple or complex as you want - eg, return 1
for any error, or make a number of codes broken up by the type of error. (Avoid using 259
, because that's the value of STILL_ACTIVE
- doing so might lead your Delphi code into an infinite loop, depending how it's coded, thinking it's still waiting to get an exit code because the Perl script is still running when it's not.)
Your Delphi app can then check this code to see if the Perl script worked or not and do something in response if it failed - log a message, tell the user, etc.
One other thing - you say you just want to know if the script executed or not and the above should provide that. Given it's a Perl script and runs on the command line, one other thing you might want to do is get its output, either to scan for errors if you don't return an error code, or to log or show the user. You can do this by running it via CreateProcess
and capturing the output. From what you say this is probably overkill for what you're after, though!