Solved

interacting with DOS

Posted on 2001-07-21
5
256 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:
mnasman 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

23 Experts available now in Live!

Get 1:1 Help Now