Solved

interacting with DOS

Posted on 2001-07-21
5
262 Views
Last Modified: 2010-04-06
Hi.  How does one interact with DOS from Delphi/Pascal.  I want to (for example) be able to interact with a program run externally (by e.g WinExec), and do different things based on its output.

1) run dos command using winexec
THEN
if *this* happens, do this
if *that* happens, do this,
so my question is basically how to do this - how can I use Delphi to read the output of another program run in e.g DOS?

Thanks for the help
0
Comment
Question by:Lmoen
5 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 120 total points
ID: 6304607
Hello

  here's an example to how capture the dos output, this will add the  output for the chkdsk to memo

//=====
{
The example runs 'chkdsk.exe c:\' and displays the output to Memo1

Put a TMemo (Memo1) and a TButton (Button1) in your form
Put this code in the OnCLick of Button1:
}

procedure TForm1.Button1Click(Sender: TObject);

 procedure RunDosInMemo(DosApp:String;AMemo:TMemo);
 const
    ReadBuffer = 2400;
 var
  Security            : TSecurityAttributes;
  ReadPipe,WritePipe  : THandle;
  start               : TStartUpInfo;
  ProcessInfo         : TProcessInformation;
  Buffer              : Pchar;
  BytesRead           : DWord;
  Apprunning          : DWord;
 begin
  With Security do begin
   nlength              := SizeOf(TSecurityAttributes);
   binherithandle       := true;
   lpsecuritydescriptor := nil;
  end;
  if Createpipe (ReadPipe, WritePipe,
                 @Security, 0) then begin
   Buffer  := AllocMem(ReadBuffer + 1);
   FillChar(Start,Sizeof(Start),#0);
   start.cb          := SizeOf(start);
   start.hStdOutput  := WritePipe;
   start.hStdInput   := ReadPipe;
   start.dwFlags     := STARTF_USESTDHANDLES +
                        STARTF_USESHOWWINDOW;
   start.wShowWindow := SW_HIDE;

   if CreateProcess(nil,
          PChar(DosApp),
          @Security,
          @Security,
          true,
          NORMAL_PRIORITY_CLASS,
          nil,
          nil,
          start,
          ProcessInfo)
   then
   begin
    repeat
     Apprunning := WaitForSingleObject
                  (ProcessInfo.hProcess,100);
     Application.ProcessMessages;
    until (Apprunning <> WAIT_TIMEOUT);
     Repeat
       BytesRead := 0;
       ReadFile(ReadPipe,Buffer[0],
                 ReadBuffer,BytesRead,nil);
       Buffer[BytesRead]:= #0;
       OemToAnsi(Buffer,Buffer);
       AMemo.Text := AMemo.text + String(Buffer);
     until (BytesRead < ReadBuffer);
  end;
  FreeMem(Buffer);
  CloseHandle(ProcessInfo.hProcess);
  CloseHandle(ProcessInfo.hThread);
  CloseHandle(ReadPipe);
  CloseHandle(WritePipe);
  end;
 end;

 begin {button 1 code}
   RunDosInMemo('chkdsk.exe c:\',Memo1);
 end;
 
Best regards
Mohammed Nasman
0
 

Expert Comment

by:teacher_mod
ID: 6305243
Irrelevant and offensive comments have been deleted from this Question by:

teacher_mod
Community Support Moderator
Experts-Exchange
teacher_mod@experts-exchange.com
0
 

Expert Comment

by:juncanonigox
ID: 8867403
hi,

i've just implemented the above code named "RunDosInMemo" in my app, and it works fine when the DOS prog returns something but it hangs up when it does not return anything.  

the DOS outputs depends on the parameter being passed when called.

is there a way to determine if the DOS prog does not return anything?

thanks.
--juncanonigo
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert a string into a TDateTime 5 63
tvirtualstringtree freeze when load too manny images 10 71
Adoquery sql  left join does not work 25 98
Base1 Encode/Decode 3 81
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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