Thanks Rob,

But actually, I did get one to work.  It finally gelled for the following
code in my OnFormCreate handler:

  D := GetStartDir;
  ShellExecute( 0, 'open', PChar('command.com'),
                PChar('/c '+'ipconfig/all > harbor.txt'),
                nil, SW_HIDE );
  Waiting := 100;
  while ( (Waiting>0) and (not FileExists(D+'HARBOR.TXT')))
  do begin
        Sleep(250);
        Waiting := Waiting-1;
     end;

  if   (not FileExists(D+'HARBOR.TXT'))
  then raise Exception.Create('Login Error 1.');

But there is still a problem.  The 'Waiting' loop discovers that the
file exists, but not whether it has been completely written and let
loose to be read.  In my OnFormActivate handler, I use:

   if   FirstActivation
   then begin
          D := GetStartDir;
          Application.ProcessMessages;
          Sleep(500);

          if   FileExists(D+'HARBOR.TXT')
          then begin
                  meHarbor.Lines.LoadFromFile(D+'HARBOR.TXT');

which works MOST of the time.  But occasionally, (5%) it causes
an exception related to trying to read a file that is still being written.

Does anyone have a way to determine whether the file HARBOR.TXT
has been released for reading after being completely written?  I would
be able to fix this problem by replacing the Sleep(500) with a loop
that tests till the HARBOR.TXT file is ready.  

Thanks,
Rich


Rob Kennedy wrote

> Rich Cooper wrote:
>> I'm trying to pipe a DOS command to a file using a ShellExecute,
>> but it doesn't create  the output file.  Here's the code:
>> 
>> var D : string;
>> ...
>>   D := GetStartDir;
>>   ShellExecute(0,pChar('ipconfig/all > 
>> '),pchar('harbor.txt'),nil,pChar(D),SW_SHOWNORMAL);
>> ...
>> 
>> but no file named 'harbor.txt' gets created.  Does anyone know how to
>> fix this?
> 
> First, "ipconfig/all >" is not a shell verb. Did you read the 
> documentation for ShellExecute before composing the code above?
> 
> Second, you're telling ShellExecute to look in the registry for the 
> "ipconfig/all >" key for files named *.txt, and then execute the command 
> it finds there on the harbor.txt file, which I'm guessing doesn't even 
> exist.
> 
> Third, note that command-line redirection is performed by the 
> command-line interpreter. ShellExecute is not a command-line 
> interpreter. Read this:
> 
> http://blogs.msdn.com/oldnewthing/archive/2006/05/16/598893.aspx
> 
> You should be able to find lots of example code to solve your problem 
> with the following search:
> 
> http://groups.google.com/groups?q=pipe+dos+command&as_ugroup=*delphi*
> 
> Fourth, ipconfig is not a DOS command. It's a console program, but it's 
> a fully fledged Windows program. Try to run it in DOS (if you even have 
> a computer with DOS installed anymore), and you'll simply be told that 
> it needs to run in Win32 mode.
> 
> Finally, type-casting a string literal to PChar is not necessary and can 
> sometimes lead to problems. A string literal can be used as any 
> string-related type, including AnsiString, WideString, PAnsiChar, and 
> PWideChar. The compiler will choose based on what it needs. You don't 
> need to tell it.
> 
> -- 
> Rob
> _______________________________________________
> Delphi mailing list -> Delphi@elists.org
> http://www.elists.org/mailman/listinfo/delphi
>
_______________________________________________
Delphi mailing list -> Delphi@elists.org
http://www.elists.org/mailman/listinfo/delphi

Reply via email to