Link to home
Start Free TrialLog in
Avatar of Gamba
Gamba

asked on

Start commandline-application from Windows-app?

How to start a command-line application (e.g. java)
from a Delphi windows-application?
How to get the output from the DOS-application
back into a Memo?
Do I have to care about/configure the
memory-management of the started application?

Thanks for help,

Gamba

Avatar of Madshi
Madshi

Don't know about java. Here is how to start a DOS application and get the output back:

function StartPiped(cmdLine: string; timeout: dword = INFINITE) : string;
var sa     : TSecurityAttributes;
    si     : TStartupInfo;
    pi     : TProcessInformation;
    pr, pw : dword;
    dw1    : dword;
begin
  result := '';
  with sa do begin
    nLength              := sizeOf(sa);
    bInheritHandle       := true;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(pr, pw, @sa, 0);
  try
    ZeroMemory(@si, sizeOf(si));
    with si do begin
      cb          := sizeOf(si);
      dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput   := GetStdHandle(STD_INPUT_HANDLE);
      hStdOutput  := pw;
      hStdError   := pw;
    end;
    if CreateProcess(nil, pchar(cmdLine), nil, nil, true, 0, nil, nil, si, pi) then
      try
        WaitForSingleObject(pi.hProcess, timeout);
        if PeekNamedPipe(pr, nil, 0, nil, @dw1, nil) and (dw1 > 0) then begin
          SetLength(result, dw1);
          if (not ReadFile(pr, pointer(result)^, dw1, dw1, nil)) or (dw1 = 0) then
            result := '';
        end;
      finally
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
      end;
  finally
    CloseHandle(pr);
    CloseHandle(pw);
  end;
end;

Please note, that this function only gives you the complete output after the DOS program has terminated. You can probably change the code so that you get the output while the DOS program is still running already - if you need that.

You don't care about memory management.

Regards, Madshi.
Avatar of Gamba

ASKER

Thanks, Madshi!
The point are yours.

Would you agree to tell me,how getting the output
while the program still is running (and maybe how
to get the formatted output direct into the memo-component)
for another 50 points?

Thank you,
Gamba
Getting the output into the memo is easy:

Memo1.Text := StartPiped(...);

Getting the function to already catch output while the program is still running, is a tougher one. I once tried that and was not able to make it run reliably. But I tried quite short, because I didn't really need it, so I wrote the StartPiped function and was satisfied with it.
I saw others posting a function which catches output while the program is still running, but I tried that function and it was not reliable enough, at least not for me. However, I'm quite sure that it is possible to make such a function really reliable, but unfortunately I've not the time to do that (I've so many other things to do). I'm sorry. Perhaps you should wait, maybe another expert has a good function, or you could try to modify my function yourself.

Regards, Madshi.
Avatar of Gamba

ASKER

maybe two points:

1. I tried the function, and it works fine for
   most commands. But one java-call, which should
   start an application, did hang at WaitForSingleObject.
   Do you know about limitations?

2. There is one problem with Memo1.Text:=StartPiped(..);
   some applications return crlf, and others seem
   to return only cr (like java), so formatting
   does not work. Do you know about this?

Gamba



ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gamba

ASKER

Thanks for solving this complicate problem!

Gamba
Just for my interest: Did (1) and (2) both work?
Avatar of Gamba

ASKER

Sure, for further interest:

I tried (1) and unfortunately it did work, but
not for the java-application.
Then I checked for a C++ - implementation (cause
I also want to implement the function in CBuilder)
and found a class implementation:

http://www.doc.ic.ac.uk/~phjk/OperatingSystemsConcepts/Exercises/Tutorial-02/WebVersion-CreatingProcessesV1.htm

This also did not work, and I think, maybe it is
a problem with DOS/batch-applications, but i could not
find out more details until now.


Gamba
Avatar of Gamba

ASKER

one more thing:
I guess, Windows-applications are stable, I did
not have any problems.