error available from cmd prompt, but not in PureBasic

Just starting out? Need help? Post your questions and find answers here.
Poltergeist
User
User
Posts: 26
Joined: Fri Jul 14, 2006 10:27 pm

error available from cmd prompt, but not in PureBasic

Post by Poltergeist »

Hi,

when running a program (in this case Cmail.exe) from the commandprompt, I can redirect the output using the commandline

Code: Select all

 cmail.exe -config:configfile.txt -d > %temp%\cmail.log 2>&1
to a file. This includes the data from -d (which is written to stdout) and the error (which is written to stderr)

However, when I try to do the same with

Code: Select all

cmail=runprogram("cmail.exe","-config:configfile.txt -d","",#PB_Program_Read|#PB_Program_Open|#PB_Program_Error)
output.s=""
If cmail
  While ProgramRunning(cmail)
   error.s=ReadProgramError(cmail)
    If AvailableProgramOutput(cmail)
      output+ReadProgramString(cmail)+#CRLF$
    EndIf
  Wend
  exitcode=ProgramExitCode(cmail)
  If exitcode=0
    Debug("Done")
  Else
    Debug("ERROR:"+Str(exitcode)+":"+error.s)
  EndIf
Else
  Debug("Could not start cmail")
EndIf
I do get an errorcode (1), but error.s does not contain data, while output.s contains the debug (-d) data.

What am I doing wrong here? The errormessage is the very last message given.
Poltergeist
User
User
Posts: 26
Joined: Fri Jul 14, 2006 10:27 pm

Re: error available from cmd prompt, but not in PureBasic

Post by Poltergeist »

Can I answer my own question? Ter Leering ende vermaeck, as we say in dutch:

It was solved by placing the line

Code: Select all

error.s=ReadProgramError(cmail)
after the while-wend loop...

Isn't that strange? I thought ReadProgramError only works if ProgramRunning <> 0
User avatar
NicTheQuick
Addict
Addict
Posts: 1226
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: error available from cmd prompt, but not in PureBasic

Post by NicTheQuick »

It's mostly depending on the program you are running. If not explicitly done all the text written to stdout or stderr gets only flushed after a specific bunch of text or at the end of the program. Only flushed text can be read from a pipe.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: error available from cmd prompt, but not in PureBasic

Post by kenmo »

You are overwriting error.s every loop!

ReadProgramError() is not set-once and then retains its text value. It is a data stream, like ReadProgramString() (I wish the PB functions handled these streams equally). So once you read an error string, it's gone the next time you check it.

Check that ReadProgramError() is getting some message:

Code: Select all

; in your loop...
error.s=ReadProgramError(cmail)
If error
  Debug error
EndIf
Assuming it is... use something like this to catch all Error output:

Code: Select all

; in your loop...
newError.s=ReadProgramError(cmail)
If newError
  error = error + newError  ; append the new text
EndIf
Also you might want to insert a very small sleep, like a Delay(1), in your loop. So it doesn't consume full CPU core while the external program is running.
Poltergeist
User
User
Posts: 26
Joined: Fri Jul 14, 2006 10:27 pm

Re: error available from cmd prompt, but not in PureBasic

Post by Poltergeist »

You are so right. Thank you for the insight!
Post Reply