Jump to content
wxinix

Is there a Delphi "subprocess" library?

Recommended Posts

I am looking for a Delphi subprocess library, something like Python's subprocess -  is there one?

Share this post


Link to post
Posted (edited)
43 minutes ago, wxinix said:

I am looking for a Delphi subprocess library, something like Python's subprocess -  is there one?

If I understood this correctly, a Python subprocess starts an external program, connects its stdin/out/err to pipes and gets the return code when it exits.

 

There is nothing like this in the standard Delphi RTL but there are multiple third party options. If I remember correctly one is in the JCL, another is my own TAsyncExec class in unit u_dzAsyncExec which is part of my dzlib library.

Edited by dummzeuch
  • Like 1
  • Thanks 1

Share this post


Link to post

There is also TProcess, ported from Lazarus lcl. I've never used it and the last update was 7 years ago.

 

I suspect most Delphi developers use CreateProcess, as long as the project is Windows only. See Darian's link above. Here are a few others:

https://www.delphibasics.info/home/delphibasicssnippets/createprocessandwaitforexit

http://www.delphicorner.f9.co.uk/articles/wapi4.htm

https://riptutorial.com/delphi/example/18340/createprocess

Share this post


Link to post
7 hours ago, Patrick PREMARTIN said:

Isn't it what TDosCommand does ? (for Windows only)

That's what I was thinking. DosCommand works great and is pretty easy to use--no need to get into the intricacies of CreateProcess.

  • Like 1

Share this post


Link to post
22 minutes ago, corneliusdavid said:

DosCommand works great and is pretty easy to use--no need to get into the intricacies of CreateProcess.

Python's subprocess does more than DOS / command line stuff. How well does TDOSCommand work with Windows executables?

Share this post


Link to post

AutoGetIt uses it to launch batch files. The only problem I've had is that sometimes partial lines are returned as full lines, then the next NewLine event returns the full line. Not sure if this is a problem in DosCommand or some OS-level buffering--never took the time to look deeply into it.

image.thumb.png.023b1871ea330470a624eb117966dd05.png

Share this post


Link to post
3 minutes ago, corneliusdavid said:

The only problem I've had is that sometimes partial lines are returned as full lines, then the next NewLine event returns the full line.

Well, I feel silly. I just noticed a parameter in DosCommand's NewLine event that indicates whether it's a full line or not. Checking that now gives a cleaner log--no partial lines.

 

So, yeah, DosCommand works great! :classic_cool:

 

  • Haha 2

Share this post


Link to post
Posted (edited)
29 minutes ago, JonRobertson said:

Python's subprocess does more than DOS / command line stuff.

You can always use the this python module from Delphi using P4D :classic_smile:.

 

One of the most advanced such Delphi functions in the Jcl unit JclSysUtils.  Look at ExecuteCmdProcess and the various Execute overloads.   It uses overlapped IO for super efficient redirection of the produced output, instead of using a special thread or a timer. These functions are also suitable for execution from a task/thread, if you do not want to block the main thread.

 

The unit pyscripter/Source/uSysUtils.pas at master · pyscripter/pyscripter (github.com) extends the jcl functions to allow for an stdin pipe, customized environment variables and a buffered raw output.

Edited by pyscripter

Share this post


Link to post
2 hours ago, JonRobertson said:

Python's subprocess does more than DOS / command line stuff. How well does TDOSCommand work with Windows executables?

That was the problem with this topic from the beginning.

What exactly is a "subprocess in python"? What does it do, if it is "more than DOS / command line stuff"?

Share this post


Link to post
2 minutes ago, dummzeuch said:

What exactly is a "subprocess in python"? What does it do, if it is "more than DOS / command line stuff"?

What is Python Subprocess

Quote

you can use the subprocess module to run a shell command, like "ls" or "ping", and get the output of that command in your Python code. You can also use it to run other Python scripts or executables, like .exe files on Windows.

That's why I asked when others suggested TDOSCommand. From the github page, it only mentions command line apps.

Share this post


Link to post
8 minutes ago, JonRobertson said:
9 minutes ago, JonRobertson said:

you can use the subprocess module to run a shell command, like "ls" or "ping", and get the output of that command in your Python code. You can also use it to run other Python scripts or executables, like .exe files on Windows.

What is Python Subprocess

That's why I asked when others suggested TDOSCommand. From the github page, it only mentions command line apps.

TDosCommand can run other Delphi programs and executables.

It can even run python scripts, if the interpreter for that is installed.

So I guess that fits the bill.

Share this post


Link to post

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

×