Solved

interacting with DOS

Posted on 2001-07-21
5
261 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

775 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