Board index » delphi » Prob Executing MSDOS Pgm from Delphi3 - Can't Access Files Created

Prob Executing MSDOS Pgm from Delphi3 - Can't Access Files Created

I am building a shell for a DOS command-line utility
program using Delphi-3 (on a W98 machine). I use
dir/file listboxes to specify the source files and
destination directory, build a command-line and
use ShellExecute() to run the program either directly
or via a ".pif" shortcut. The DOS utility builds a
temporary text file (about 600k) which I then read,
parse, re-arrange and output to new files.

Problem ... I can usually make it work ONE time -
for the first of 'n' selected input files - but
the second time I cannot open the temporary
file. Assignfile() Reset() ... the program crashes
on the attempted Reset() claiming that the temp file
is not there -- but it IS there.

I've tried a number of variations on the theme
such as evolking a regular batch file, using
different names for each temp file, running
via shortcut, directly, different options for
ShellExec(), through 'command.com' ... nada.
I've also tried adding long delays between
starting the DOS program (it takes a few seconds
to finish) and trying to open the temp file ...
again nada.

If I run the utility directly, using the run-
minimized ShellExec() option, I sometimes get
unclosed DOS tasks appearing on the toolbar.
They show the utility starting - but then a
file-sharing violation (presumably when it
tries to open either the source or temp file
(can't tell which). Opening the window, I see
is the utility title line, the error message
and the old "Abort, Retry, Fail" request. If
I choose "Retry" at that point, the utility
will run to completion.

If the program crashes to the de{*word*81} ... and
I <f9> to resume execution, then the next attempt
to open the temp file will work. Just hateful ...

If I use FileOpen() with the 'fmShareDenyNone'
option I can open the temp file(s) without any
errors - but FileRead() and such are byte-oriented
and it's proving a pain and a mess to try and emulate
a ReadLn()-style procedure which would otherwise be
perfect for the temp file.

Am I doing something obviously wrong ? Is there a
cleaner solution than trying to write my own
ReadLn() and all the gawdawful off-by-one errors
that will entail ? Is my DOS task somehow not
terminating completely even when I run it via
a shortcut with "close on completion" set ? How
can I check to see if the DOS task is still
running (and close it) before trying to open the
temp file ?

And finally, is there any better documentation
(online ?) about running other programs from Delphi ?
The provided docs are awful and the one book I have
which mentions it is very sketchy.

Any clues helpful ... this was supposed to be
an EASY little program ......

-jim

 

Re:Prob Executing MSDOS Pgm from Delphi3 - Can't Access Files Created


You might try CreateProcess instead of ShellExecute. It returns a handle
that you can then pass to WaitForSingleObject. This will pause your program
until the DOS program finishes. These routines are documented in the Win API
help.

As far as processing the output, (presuming using WaitForSingleObject
doesn't resolve the access problems), you might consider opening a
tFileStream (you can specify the mode). Then use a
tStringList.LoadFromStream to get the input nicely broken into lines.

Quote
j <jma...@iu.net> wrote in message news:378501a4.25079079@news.iu.net...
> I am building a shell for a DOS command-line utility
> program using Delphi-3 (on a W98 machine). I use
> dir/file listboxes to specify the source files and
> destination directory, build a command-line and
> use ShellExecute() to run the program either directly
> or via a ".pif" shortcut. The DOS utility builds a
> temporary text file (about 600k) which I then read,
> parse, re-arrange and output to new files.

> Problem ... I can usually make it work ONE time -
> for the first of 'n' selected input files - but
> the second time I cannot open the temporary
> file. Assignfile() Reset() ... the program crashes
> on the attempted Reset() claiming that the temp file
> is not there -- but it IS there.

> I've tried a number of variations on the theme
> such as evolking a regular batch file, using
> different names for each temp file, running
> via shortcut, directly, different options for
> ShellExec(), through 'command.com' ... nada.
> I've also tried adding long delays between
> starting the DOS program (it takes a few seconds
> to finish) and trying to open the temp file ...
> again nada.

> If I run the utility directly, using the run-
> minimized ShellExec() option, I sometimes get
> unclosed DOS tasks appearing on the toolbar.
> They show the utility starting - but then a
> file-sharing violation (presumably when it
> tries to open either the source or temp file
> (can't tell which). Opening the window, I see
> is the utility title line, the error message
> and the old "Abort, Retry, Fail" request. If
> I choose "Retry" at that point, the utility
> will run to completion.

> If the program crashes to the de{*word*81} ... and
> I <f9> to resume execution, then the next attempt
> to open the temp file will work. Just hateful ...

> If I use FileOpen() with the 'fmShareDenyNone'
> option I can open the temp file(s) without any
> errors - but FileRead() and such are byte-oriented
> and it's proving a pain and a mess to try and emulate
> a ReadLn()-style procedure which would otherwise be
> perfect for the temp file.

> Am I doing something obviously wrong ? Is there a
> cleaner solution than trying to write my own
> ReadLn() and all the gawdawful off-by-one errors
> that will entail ? Is my DOS task somehow not
> terminating completely even when I run it via
> a shortcut with "close on completion" set ? How
> can I check to see if the DOS task is still
> running (and close it) before trying to open the
> temp file ?

> And finally, is there any better documentation
> (online ?) about running other programs from Delphi ?
> The provided docs are awful and the one book I have
> which mentions it is very sketchy.

> Any clues helpful ... this was supposed to be
> an EASY little program ......

> -jim

Re:Prob Executing MSDOS Pgm from Delphi3 - Can't Access Files Created


Quote
"Bruce Roberts" <no.junk.please....@attcanada.net> wrote:
>You might try CreateProcess instead of ShellExecute. It returns a handle
>that you can then pass to WaitForSingleObject. This will pause your program
>until the DOS program finishes. These routines are documented in the Win API
>help.

>As far as processing the output, (presuming using WaitForSingleObject
>doesn't resolve the access problems), you might consider opening a
>tFileStream (you can specify the mode). Then use a
>tStringList.LoadFromStream to get the input nicely broken into lines.

   Good ideas ... I'll try 'em tomorrow. I use streams so
   infrequently that I'de completely spaced them out of
   my mind.

   And if NONE of this works ... I'll write a BP7 dos
   program and just use the winders program to send
   it file lists ... *

   -jim

Other Threads