Solved

Start commandline-application from Windows-app?

Posted on 2001-09-12
9
124 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Accepted Solution

by:
Madshi earned 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now