?
Solved

Start commandline-application from Windows-app?

Posted on 2001-09-12
9
Medium Priority
?
136 Views
Last Modified: 2010-04-06
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

0
Comment
Question by:Gamba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 6478606
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.
0
 

Author Comment

by:Gamba
ID: 6478747
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
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6478803
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Gamba
ID: 6478825
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



0
 
LVL 20

Accepted Solution

by:
Madshi earned 600 total points
ID: 6478972
(1)
Maybe the Java program wants to talk to your main thread or something like that. Try replacing the WaitForSingleObject line with this one:

while WaitForSingleObject(pi.hProcess, 50) = WAIT_TIMEOUT do
  Application.ProcessMessages;

(2)
Okay, then use this:

str := StartPiped(...);
madStrings.ReplaceStr(str, #$D, #$D#$A);
memo1.Text := str;

Or perhaps "Replace(str, #$A, #$D#$A);"?

The unit "madStrings" is part of the free package "madBasic", which you can download from my homepage (including sources).

Regards, Madshi.

www.madshi.net
http://help.madshi.net/Data/StringManipulate.htm#ReplaceStr
0
 

Author Comment

by:Gamba
ID: 6480267
Thanks for solving this complicate problem!

Gamba
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6480338
Just for my interest: Did (1) and (2) both work?
0
 

Author Comment

by:Gamba
ID: 6480769
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
0
 

Author Comment

by:Gamba
ID: 6480770
one more thing:
I guess, Windows-applications are stable, I did
not have any problems.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question