How can I read stdout from console application using pipes?

Just starting out? Need help? Post your questions and find answers here.
Nituvious
Addict
Addict
Posts: 1032
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

How can I read stdout from console application using pipes?

Post 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.
▓▓▓▓▓▒▒▒▒▒░░░░░
Lothar Schirm
User
User
Posts: 54
Joined: Mon Nov 26, 2012 4:57 pm

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

Post by Lothar Schirm »

May the description of ReadConsoleData() and WriteConsoleData() in the PureBasic help file be usefull for you?
User avatar
skywalk
Addict
Addict
Posts: 4299
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

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

Post 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.
Nituvious
Addict
Addict
Posts: 1032
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post 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. :(
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8453
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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

Post 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)
BERESHEIT
Nituvious
Addict
Addict
Posts: 1032
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

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

Post by Nituvious »

No dice, can't get a return on AvailableProgramOutput()
▓▓▓▓▓▒▒▒▒▒░░░░░
Post Reply