console capturing

Posted on 2000-02-21
Medium Priority
Last Modified: 2010-04-04
I want to run an external console app (winexec...) from within Delphi and I want to capture in code the result (what the app displays). For example, do a PING and let the program know if the remote node responds or do a NET VIEW and let the program know the net resources.
BTW, how is it possible from delphi to know if an IP device is responding in the NET, as the PING does
Question by:gikam
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
  • 4
  • 2

Expert Comment

ID: 2545650
Hi gikam,

the simplest method is to have your program write a batch file to disk that
contains the necessary command line with redirection, e.g.

ping www.borland.com > c:\temp\logfile.txt
then WinExec that batch, wait until FindFirst returns > 0 bytes size for the
logfile and then simply open and read it.

eg :

Redirecting DOS App Output
                                                                                      D2 D3 D4
    A function to execute a DOS or Win32 consoloe mode application and wait for it to close
    before continuing. Input for the app can be directed from a file, and the output will be
    redirected to a file.


      Controls, Windows, SysUtils, Forms;


     Description    : executes a (DOS!) app defined in the CommandLine parameter

                      redirected to take input from InputFile (optional) and give

                      output to OutputFile

     Result         : True on success

     Parameters     : CommandLine : the command line for app, including full path

                      InputFile   : the ascii file where from the app takes input,

                                     empty if no input needed/required.

                      OutputFile  : the ascii file to which the output is redirected

                      ErrMsg      : additional error message string. Can be empty

     Error checking : YES

     Target         : Delphi 2, 3, 4

     Author         : Theodoros Bebekis, email bebekis@otenet.gr

     Notes          :

     Example call   : CreateDOSProcessRedirected('C:\MyDOSApp.exe',



                                                 'Please, record this message')


    function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile,

       ErrMsg :string): boolean;


      ROUTINE_ID = '[function: CreateDOSProcessRedirected]';


      OldCursor     : TCursor;

      pCommandLine  : array[0..MAX_PATH] of char;


      pOutPutFile   : array[0..MAX_PATH] of char;

      StartupInfo   : TStartupInfo;

      ProcessInfo   : TProcessInformation;

      SecAtrrs      : TSecurityAttributes;




      hOutputFile   : THandle;


      Result := FALSE;

      { check for InputFile existence }

      if (InputFile <> '') and (not FileExists(InputFile)) then

        raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +

           'Input file * %s *' + #10 +

           'does not exist' + #10 + #10 +

           ErrMsg, [InputFile]);

      hAppProcess := 0;

      hAppThread := 0;

      hInputFile := 0;

      hOutputFile := 0;

      { save the cursor }

      OldCursor     := Screen.Cursor;

      Screen.Cursor := crHourglass;


        { copy the parameter Pascal strings to null terminated strings }

        StrPCopy(pCommandLine, CommandLine);

        StrPCopy(pInputFile, InputFile);

        StrPCopy(pOutPutFile, OutputFile);

        { prepare SecAtrrs structure for the CreateFile calls.  This SecAttrs

          structure is needed in this case because we want the returned handle to

          be inherited by child process. This is true when running under WinNT.

          As for Win95, the parameter is ignored. }

        FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);

        SecAtrrs.nLength              := SizeOf(SecAtrrs);

        SecAtrrs.lpSecurityDescriptor := nil;

        SecAtrrs.bInheritHandle       := TRUE;

        if InputFile <> '' then


          { create the appropriate handle for the input file }

          hInputFile := CreateFile(

             pInputFile,                          { pointer to name of the file }

             GENERIC_READ or GENERIC_WRITE,       { access (read-write) mode }

             FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }

             @SecAtrrs,                           { pointer to security attributes }

             OPEN_ALWAYS,                         { how to create }


             or FILE_FLAG_WRITE_THROUGH,          { file attributes }

             0);                                 { handle to file with attrs to copy }

          { is hInputFile a valid handle? }

          if hInputFile = INVALID_HANDLE_VALUE then

            raise Exception.CreateFmt(ROUTINE_ID + #10 +  #10 +

               'WinApi function CreateFile returned an invalid handle value' + #10 +

               'for the input file * %s *' + #10 + #10 +

                ErrMsg, [InputFile]);

        end else

          { we aren't using an input file }

          hInputFile := 0;

        { create the appropriate handle for the output file }

        hOutputFile := CreateFile(

           pOutPutFile,                         { pointer to name of the file }

           GENERIC_READ or GENERIC_WRITE,       { access (read-write) mode }

           FILE_SHARE_READ or FILE_SHARE_WRITE, { share mode }

           @SecAtrrs,                           { pointer to security attributes }

           CREATE_ALWAYS,                       { how to create }


           or FILE_FLAG_WRITE_THROUGH,          { file attributes }

           0 );                                 { handle to file with attrs to copy }

        { is hOutputFile a valid handle? }

        if hOutputFile = INVALID_HANDLE_VALUE then

          raise Exception.CreateFmt(ROUTINE_ID + #10 +  #10 +

             'WinApi function CreateFile returned an invalid handle value'  + #10 +

             'for the output file * %s *' + #10 + #10 +

             ErrMsg, [OutputFile]);

        { prepare StartupInfo structure }

        FillChar(StartupInfo, SizeOf(StartupInfo), #0);

        StartupInfo.cb          := SizeOf(StartupInfo);


        StartupInfo.wShowWindow := SW_HIDE;

        StartupInfo.hStdOutput  := hOutputFile;

        StartupInfo.hStdInput   := hInputFile;

        { create the app }

        Result := CreateProcess(

           NIL,                           { pointer to name of executable module }

           pCommandLine,                  { pointer to command line string }

           NIL,                           { pointer to process security attributes }

           NIL,                           { pointer to thread security attributes }

           TRUE,                          { handle inheritance flag }

           HIGH_PRIORITY_CLASS,           { creation flags }

           NIL,                           { pointer to new environment block }

           NIL,                           { pointer to current directory name }

           StartupInfo,                   { pointer to STARTUPINFO }

           ProcessInfo);                  { pointer to PROCESS_INF }

        { wait for the app to finish its job and take the handles to free them later }

        if Result then


          WaitforSingleObject(ProcessInfo.hProcess, INFINITE);

          hAppProcess  := ProcessInfo.hProcess;

          hAppThread   := ProcessInfo.hThread;

        end else

          raise Exception.Create(ROUTINE_ID + #10 +  #10 +

             'Function failure'  + #10 +  #10 + ErrMsg);


        { close the handles

          Kernel objects, like the process and the files we created in this case,

          are maintained by a usage count.

          So, for cleaning up purposes we have to close the handles

          to inform the system that we don't need the objects anymore }

        if hOutputFile <> 0 then


        if hInputFile <> 0 then


        if hAppThread <> 0 then


        if hAppProcess <> 0 then


        { restore the old cursor }

        Screen.Cursor:= OldCursor;


    end;    { CreateDOSProcessRedirected }

here are other code examples (not tested or checked by myself)


Regards, Zif.

Author Comment

ID: 2549203
Adjusted points to 80

Author Comment

ID: 2549204
Thanx, but your answer is not acceptable since I don't want to write to an intermediate file. I did know this method.
I want to directly read the result of the command (some kind of redirection to the Readln command). What about the second part of the question?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

ZifNab earned 240 total points
ID: 2549580
oops, forgot about the second question. Can't you just use a ping component? See F.Piette's FWS components...

Expert Comment

ID: 2550075
Have you looked at :



I don't know if it helps, but for 60 points, it should do...


Author Comment

ID: 2556745
Zif, thanx
I found F.Piette's components. The TPing one does what I need even tough I had to change it a little bit to accomodate from Delphi 3 to 4

Author Comment

ID: 2556752
Attar, thanx
but I already payed 60 to see the example

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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