Page 1 of 1
Catching Output Redirection
Posted: Fri Mar 09, 2007 8:41 pm
by Karbon
Something.exe > My_PureBasic_Program.exe
I need to write something to catch the output of another program and do something with it. How do I read the data that "Something.exe" outputs ?
The program that *receives* the output redirection will be written in PureBasic.
Posted: Sat Mar 10, 2007 12:37 am
by netmaestro
The process commands are probably what you will find useful. If you run the program from PureBasic using RunProgram and pass the #PB_Program_Open, -Read, and -Write flags you can communicate effectively with any program. Here's a small example:
Code: Select all
prog$ = "cmd.exe"
dosbox = RunProgram(prog$, "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Hide)
If dosbox
OpenWindow(0,0,0,400,500,"Results from DosBox",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
StartDrawing(WindowOutput(0))
DrawText(65,200, "Pinging now, Stand by for results...", #Black, GetSysColor_(#COLOR_BTNFACE))
StopDrawing()
NewList results.s()
WriteProgramStringN(dosbox, "ping google.com")
WriteProgramData(dosbox, #PB_Program_Eof, 0)
While ProgramRunning(dosbox)
If AvailableProgramOutput(dosbox)
AddElement(results())
results() = ReadProgramString(dosbox)
Else
Delay(1)
EndIf
Wend
AddElement(results())
AddElement(results())
results() = "Program finished with exit code = " + Str(ProgramExitCode(dosbox))
CloseProgram(dosbox)
CreateGadgetList(WindowID(0))
ListViewGadget(0,0,0,400,500)
ForEach results()
AddGadgetItem(0, -1, results())
Next
Repeat:Until WaitWindowEvent()=#WM_CLOSE
Else
MessageRequester("OOPS!", "Can't find " + prog$ )
EndIf
Posted: Sat Mar 10, 2007 4:52 am
by Karbon
Hmm, no, that's not what I'm talking about.
I want to take the output from one program using the output redirection features of the command shell ( '>') *into* a program written in PB, but need to know how to read the data passed *in* to the program, in PB, by the use of the ">" operator.
Something like :
dir > my_pb_program.exe
But I'm not trying to redirect dir, that's just an example.
Posted: Sat Mar 10, 2007 6:52 am
by netmaestro
You can pipe the output of the other program through your program via ReadConsoleData to achieve that. This sample directs the output of a dos dir command to your program and then you can do stuff with it, in this case, just list it. (I know it's not really dir you're after but the example has to do
something 
) It is based on the demo program in the doc which reads in a picture file but this is converted to expect text. To use it, you have to set compiler options to "Console Executable" or it won't work. Compile it to test.exe, then pipe the dir command from a command console:
dir | test.exe
and it will list the directory in the listview.
Code: Select all
; ** Doc code **
OpenConsole()
TotalSize = 0
BufferFree = 10000
*Buffer = AllocateMemory(BufferFree)
Repeat
ReadSize = ReadConsoleData(*Buffer+TotalSize, BufferFree) ; read a block of data
TotalSize + ReadSize
BufferFree - ReadSize
If BufferFree < 100 ; resize the buffer if it is not large enough
BufferFree = 10000
*Buffer = ReAllocateMemory(*Buffer, TotalSize+10000)
EndIf
Until ReadSize = 0 ; once 0 is returned, there is nothing else to read
; ** End doc code **
;
For i=*buffer To *buffer+MemorySize(*buffer)-bufferfree
If PeekB(i) = 13
PokeB(i, 0)
EndIf
Next
*cursor = *buffer
NewList strings.s()
Repeat
tmp$=PeekS(*cursor)
AddElement(strings())
strings()=tmp$
*cursor+Len(tmp$)+2 ; jump over the 13 and the 10
Until *cursor >= *buffer+MemorySize(*Buffer)-BufferFree
OpenWindow(0,0,0,400,500,"Redirected Output to PB program",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(0))
ListViewGadget(0,0,0,400,500)
ForEach strings()
AddGadgetItem(0, -1, strings())
Next
Repeat:Until WaitWindowEvent() = #WM_CLOSE
Posted: Sat Mar 10, 2007 5:46 pm
by Karbon
I was working under the assumption that the pike (|) wouldn't work with the program I'll be outputting from (SQLite's console manager). I'll test it and see.
*edit
From
http://www.microsoft.com/resources/docu ... x?mfr=true
I now see the difference.
Thanks!