Page 1 of 1

How can I read stdout from console application using pipes?

Posted: Mon Nov 04, 2013 10:08 am
by Nituvious
I want to read data from a console application that I did not make.
I've tried RunProgram with #PB_Program_Open and #PB_Program_Read flags, but it halts the program and prevents it from continuing execution.

Would anyone happen to have any examples for such a thing? Ideally, I would like to read and write to the program from my own program.
I've tried googling, but I don't think I'm searching for the right terms because everything seems to assume I have the source to both programs.

Re: How can I read stdout from console application using pip

Posted: Mon Nov 04, 2013 4:18 pm
by Lothar Schirm
May the description of ReadConsoleData() and WriteConsoleData() in the PureBasic help file be usefull for you?

Re: How can I read stdout from console application using pip

Posted: Mon Nov 04, 2013 5:02 pm
by skywalk
Can you post more code and substitute a generic exe(like robocopy.exe or ipconfig.exe) for your RunProgram() call?
I communicate with external exe's using ReadProgramString() and ReadProgramError(). Haven't used the Console functions, since I keep the RunProgram()'s short and hidden.

Re: How can I read stdout from console application using pip

Posted: Tue Nov 05, 2013 12:46 pm
by auser
I remember some issue with a blocking console application as well. As far I remember I've fixed that blocking via adding some further flags (#PB_Program_Write|#PB_Program_Error). I did not need the "write"-flag myself but as far I remember right the other console application required it from my own.

Re: How can I read stdout from console application using pip

Posted: Wed Nov 06, 2013 3:38 am
by Nituvious
skywalk wrote:Can you post more code and substitute a generic exe(like robocopy.exe or ipconfig.exe) for your RunProgram() call?
I communicate with external exe's using ReadProgramString() and ReadProgramError(). Haven't used the Console functions, since I keep the RunProgram()'s short and hidden.
My code to launch the program is very simple:

Code: Select all

program = RunProgram("D:/path/server.exe", "", "D:/path/", #PB_Program_Open | #PB_Program_Read)
debug ReadProgramString(program) ; never reached
The program I am trying to communicate with is a game server. It can be downloaded here.
auser wrote:I remember some issue with a blocking console application as well. As far I remember I've fixed that blocking via adding some further flags (#PB_Program_Write|#PB_Program_Error). I did not need the "write"-flag myself but as far I remember right the other console application required it from my own.
This didn't work for me. :(

Re: How can I read stdout from console application using pip

Posted: Wed Nov 06, 2013 8:30 am
by netmaestro
There needs to be available program output before you call ReadProgramString. Here's an excerpt from an early version of one of my programs that talks to a chess engine:

Code: Select all

engine = RunProgram("Houdini_15a_w32.exe", "", "", #PB_Program_Open | #PB_Program_Read |#PB_Program_Write | #PB_Program_Hide)
WriteProgramStringN(engine, "position startpos")
Delay(1000)
WriteProgramStringN(engine, "go movetime 100")

Repeat
  EventID = WaitWindowEvent(1)
  If engine
    If ProgramRunning(engine)
      If AvailableProgramOutput(engine)
        e$ = ReadProgramString(engine)
        If FindString(e$, "bestmove")
          best$ = Mid(e$, 10, 4)
          ponder$ = Right(e$, 4)
          Move(best$)
        EndIf
      EndIf
      If human_hasmoved
        estring$ = "position startpos moves "
        ForEach movelist()
          estring$+movelist()+" "
        Next
        WriteProgramStringN(engine, estring$)
        Delay(100)
        WriteProgramStringN(engine, "go movetime 100")
        human_hasmoved=0
        humanmove$=""
      EndIf
      Delay(1)
    EndIf
  EndIf
Until EventID = #PB_Event_CloseWindow
Maybe it could be of help to you, dunno. But without checking AvailableProgramOutput() first, ReadProgramString() should be expected to fail most of the time imho. This is the critical sequence:

Code: Select all

Repeat
  EventID = WaitWindowEvent(1)
  If engine
    If ProgramRunning(engine)
      If AvailableProgramOutput(engine)
        e$ = ReadProgramString(engine)

Re: How can I read stdout from console application using pip

Posted: Thu Nov 07, 2013 1:12 am
by Nituvious
No dice, can't get a return on AvailableProgramOutput()