Board index » delphi » End of Dos-Tool

End of Dos-Tool

Hi,

I've got a silly problem:

I call a DOS-Tool out of Delphi that creates a text-file on my harddisk.
Unfortunately Delphi is faster than my drive.

Let's do an example:

I call
DIR c:\ /s >c:\temp\test.txt
out of Delphi.

Delphi raises an error, that the file is not available. This is true,
because the harddisk was not fast enough to write it. How can I import or
redirect a DOS-output into a memo field?

Greetings from Switzerland,
Oliver

 

Re:End of Dos-Tool


Why not use Delphi's functions instead?
Use FindFirst, FindNext to create your textfile/process the results and you
don't have to
even bother about if your textfile is closed etc.

var sr : TSearchRec;
  res : Integer;
Begin
  res:=FindFirst('C:\*.*', faAnyFile, sr);
  While res=0 do
  Begin
    memo1.Lines.add(sr.Name);
    res:=FindNext(sr);
  end;
  FindClose(sr);
end;

--------------------------
Regards,

Mathias Nyman
Sweden
http://www.se-datateknik.se/delphi
zio...@hem.passagen.se.nospam
--------------------------

Oliver Jenni <private...@yahoo.com> skrev i
diskussionsgruppsmeddelandet:389735c...@news.datacomm.ch...

Quote
> Hi,

> I've got a silly problem:

> I call a DOS-Tool out of Delphi that creates a text-file on my harddisk.
> Unfortunately Delphi is faster than my drive.

> Let's do an example:

> I call
> DIR c:\ /s >c:\temp\test.txt
> out of Delphi.

> Delphi raises an error, that the file is not available. This is true,
> because the harddisk was not fast enough to write it. How can I import or
> redirect a DOS-output into a memo field?

> Greetings from Switzerland,
> Oliver

Re:End of Dos-Tool


Mathias,

Thank you. It seems that I've chosen a bad example. It is not the DIR I try
to import into a memo. It is the console output of a old dos programm
(DOSPROG.EXE).

Do you have a clue?

Oliver

Mathias Nyman <zio...@hem1.passagen.se> schrieb in im Newsbeitrag:
877fvp$1b...@cubacola.tninet.se...

Quote
> Why not use Delphi's functions instead?
> Use FindFirst, FindNext to create your textfile/process the results and
you
> don't have to
> even bother about if your textfile is closed etc.

> var sr : TSearchRec;
>   res : Integer;
> Begin
>   res:=FindFirst('C:\*.*', faAnyFile, sr);
>   While res=0 do
>   Begin
>     memo1.Lines.add(sr.Name);
>     res:=FindNext(sr);
>   end;
>   FindClose(sr);
> end;

> --------------------------
> Regards,

> Mathias Nyman
> Sweden
> http://www.se-datateknik.se/delphi
> zio...@hem.passagen.se.nospam
> --------------------------

> Oliver Jenni <private...@yahoo.com> skrev i
> diskussionsgruppsmeddelandet:389735c...@news.datacomm.ch...
> > Hi,

> > I've got a silly problem:

> > I call a DOS-Tool out of Delphi that creates a text-file on my harddisk.
> > Unfortunately Delphi is faster than my drive.

> > Let's do an example:

> > I call
> > DIR c:\ /s >c:\temp\test.txt
> > out of Delphi.

> > Delphi raises an error, that the file is not available. This is true,
> > because the harddisk was not fast enough to write it. How can I import
or
> > redirect a DOS-output into a memo field?

> > Greetings from Switzerland,
> > Oliver

Re:End of Dos-Tool


Okay... Then maybe this is the thing you want. It's not my code and
perhaps you could shorten it a little bit to suit you purpose.

function CreateDOSProcessRedirected(const CommandLine, InputFile,
     OutputFile, ErrMsg :string):boolean;
const
  ROUTINE_ID = '[function: CreateDOSProcessRedirected ]';
var
  OldCursor     : TCursor;
  pCommandLine  : array[0..MAX_PATH] of char;
  pInputFile,
  pOutPutFile   : array[0..MAX_PATH] of char;
  StartupInfo   : TStartupInfo;
  ProcessInfo   : TProcessInformation;
  SecAtrrs      : TSecurityAttributes;
  hAppProcess,
  hAppThread,
  hInputFile,
  hOutputFile   : THandle;
begin

  Result := False;

  { Check for InputFile existence }
  if not FileExists(InputFile)
  then
    raise Exception.CreateFmt(ROUTINE_ID          + #10 +  #10 +
                              'Input file * %s *' + #10 +
                              'does not exist'    + #10 +  #10 +
                              ErrMsg, [InputFile]);

  { 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);

  TRY

    { Prepare SecAtrrs structure for the CreateFile calls.
      This SecAttrs structure is needed in this case because
      we want the returned handle can be inherited by child
      process. This is true when running under WinNT.
      As for Win95, the documentation is quite ambiguous }
    FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
    SecAtrrs.nLength              := SizeOf(SecAtrrs);
    SecAtrrs.lpSecurityDescriptor := nil;
    SecAtrrs.bInheritHandle       := True;

    { 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 }
         FILE_ATTRIBUTE_NORMAL
         or FILE_FLAG_WRITE_THROUGH,
            { file attributes }
         0 ); { handle to file with attributes 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]);

    { 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 }
          FILE_ATTRIBUTE_NORMAL
          or FILE_FLAG_WRITE_THROUGH,
             {file attributes }
          0 );
             {handle to file with attributes 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.dwFlags     := STARTF_USESHOWWINDOW or
                               STARTF_USESTDHANDLES;
    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
      begin
        WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
        hAppProcess  := ProcessInfo.hProcess;
        hAppThread   := ProcessInfo.hThread;
      end
    else
      raise Exception.Create(ROUTINE_ID          + #10 +  #10 +
                             'Function failure'  + #10 +  #10 +
                             ErrMsg);

  FINALLY
    { 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 CloseHandle(hOutputFile);
    if hInputFile <> 0 then CloseHandle(hInputFile);
    if hAppThread <> 0 then CloseHandle(hAppThread);
    if hAppProcess <> 0 then CloseHandle(hAppProcess);
    { Restore the old cursor }
    Screen.Cursor:= OldCursor;
  END;

end;    { CreateDOSProcessRedirected }

--
--------------------------
Regards,

Mathias Nyman
Sweden
http://www.se-datateknik.se/delphi
zio...@hem.passagen.se.nospam
--------------------------

Oliver Jenni <private...@yahoo.com> skrev i
diskussionsgruppsmeddelandet:38974...@news.datacomm.ch...

Quote
> Mathias,

> Thank you. It seems that I've chosen a bad example. It is not the DIR I
try
> to import into a memo. It is the console output of a old dos programm
> (DOSPROG.EXE).

> Do you have a clue?

> Oliver

Re:End of Dos-Tool


On Tue, 1 Feb 2000 20:33:04 +0100, "Oliver Jenni"

Quote
<private...@yahoo.com> wrote:
>Hi,

>I've got a silly problem:

>I call a DOS-Tool out of Delphi that creates a text-file on my harddisk.
>Unfortunately Delphi is faster than my drive.

>Let's do an example:

>I call
>DIR c:\ /s >c:\temp\test.txt
>out of Delphi.

>Delphi raises an error, that the file is not available. This is true,
>because the harddisk was not fast enough to write it. How can I import or
>redirect a DOS-output into a memo field?

>Greetings from Switzerland,
>Oliver

Hi Oliver,

Use the CreateProcess API call to execute the DOS program.  The result
of the function is the handle of the newly created process, on which
you can use the WaitForSingleObject API.

Other Threads