Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

interacting with DOS

Posted on 2001-07-21
5
Medium Priority
?
268 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
[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 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 480 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

705 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