Capture Output of Programm,redirected the pipes

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Capture Output of Programm,redirected the pipes

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Code: Select all

; Redirect Outputs into Memory
; coded by Siegfried Rings march 2002
; redirected the pipes
;
;see 
;

mCommand.s="help"

;Structure used by the CreateProcessA function
;another then that Fred implemented !
Structure MySTARTUPINFO
cb.l
lpReserved.l
lpDesktop.l
lpTitle.l
dwX.l
dwY.l
dwXSize.l
dwYSize.l
dwXCountChars.l
dwYCountChars.l
dwFillAttribute.l
dwFlags.l
wShowWindow.w
cbReserved2.w
lpReserved2.l
hStdInput.l
hStdOutput.l
hStdError.l
EndStructure

proc.PROCESS_INFORMATION ;Process info filled by CreateProcessA
ret.l ;long variable For get the Return value of the
start.MySTARTUPINFO ;StartUp Info passed To the CreateProceeeA
sa.SECURITY_ATTRIBUTES ;Security Attributes passeed To the
hReadPipe.l ;Read Pipe handle created by CreatePipe
hWritePipe.l ;Write Pite handle created by CreatePipe
lngBytesread.l ;Amount of byte Read from the Read Pipe handle
strBuff.s=Space(256) ;String buffer reading the Pipe

;Consts For functions
#NORMAL_PRIORITY_CLASS = $20
#STARTF_USESTDHANDLES = $100
#STARTF_USESHOWWINDOW = $1

;Create the Pipe
sa\nLength =SizeOf(SECURITY_ATTRIBUTES) ;Len(sa)
sa\bInheritHandle = 1
sa\lpSecurityDescriptor = 0
ret = CreatePipe_(@hReadPipe, @hWritePipe, @sa, 0)
If ret = 0
;If an error occur during the Pipe creation exit
MessageRequester("info", "CreatePipe failed. Error: ",0)
End
EndIf


start\cb = SizeOf(MySTARTUPINFO)
start\dwFlags = #STARTF_USESHOWWINDOW | #STARTF_USESTDHANDLES

;set the StdOutput And the StdError output To the same Write Pipe handle
start\hStdOutput = hWritePipe
start\hStdError = hWritePipe

;Execute the command
ret = CreateProcess_(0, mCommand, sa, sa, 1, #NORMAL_PRIORITY_CLASS, 0, 0, @start, @proc)

If ret  1
MessageRequester("Info","File Or command not found", 0)
End
Else
;MessageRequester("Info","PRG started..:",0)
EndIf


;Now We can ... must close the hWritePipe
ret = CloseHandle_(hWritePipe)

mOutputs.s = ""

;Read the ReadPipe handle
While ret0
ret = ReadFile_(hReadPipe, strBuff, 255, @lngBytesread, 0)
If lngBytesread>0
  mOutputs = mOutputs + Left(strBuff, lngBytesread)
EndIf
Wend

;Close the opened handles
ret = CloseHandle_(proc\hProcess)
ret = CloseHandle_(proc\hThread)
ret = CloseHandle_(hReadPipe)
;ret=CloseHandle_(hWritePipe)

;Return the Outputs property with the entire DOS output
MessageRequester("Info",mOutputs,0)

Getting better with a little help from my friends....thx Siggi
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by MrVainSCL.

Hi Rings
Mhhh.. Seems there is something worng with your example!? Check the following line of your example... Thanks and keep on your work

Code: Select all

     strBuff.s=Space(256) ;String buffer reading the Pipe
What is "Space()" for a command or procedure!? Bye


PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win98SE + Updates...

greetz
MrVainSCL! aka Thorsten
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
What is "Space()" for a command or procedure!? Bye
Space is a command from Mr.Skunks lib 'String2'
In his program is 'strBuff.s' filled with 256 spaces (Char 32 ?!)
Don't know why, but I have seen this a few times in some PureBasic code...


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Oh i'm sorry about that(String2.lib).I've download all those libs times ago.
As I'm a VB-Coder at work, Space(count) is a native Basic-Command to fill a string with spaces.There are a few workarounds for Space on this site (Or Pauls).


Edited by - Rings on 23 March 2002 08:18:27
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post by Froggerprogger »

On Win98 I had to replace a part of this code by the following:

Code: Select all

;Read the ReadPipe handle
readmax = 255
Repeat 
  Returnval = ReadFile_(hReadPipe, strBuff, readmax, @lngBytesread, 0)
  If lngBytesread>0
    mOutputs = mOutputs + Left(strBuff, lngBytesread)
  EndIf
Until lngBytesread < readmax
Win98-API-ReadFile_ seems to return the number of read bytes in 'readmax', so you have to loop until it is lower than 255 to get the whole string.
Win2000-API-ReadFile_ seems to return true in 'readmax' until the string is over and 'readmax' gets false.

So we have to include an OS-request. :!:
%1>>1+1*1/1-1!1|1&1<<$1=1
deano1987
User
User
Posts: 19
Joined: Thu Jul 30, 2009 11:39 pm
Location: england, cannock, west midlands

Re: Capture Output of Programm,redirected the pipes

Post by deano1987 »

This is not working in PureBasic 4.30? It just says command or file not found and does not give me a line or anything to go on :s
User avatar
Sveinung
Enthusiast
Enthusiast
Posts: 142
Joined: Tue Oct 07, 2003 11:03 am
Location: Bergen, Norway

Re: Capture Output of Programm,redirected the pipes

Post by Sveinung »

can you use ReadProgramString() from the process commands?

Regards
Sveinung
deano1987
User
User
Posts: 19
Joined: Thu Jul 30, 2009 11:39 pm
Location: england, cannock, west midlands

Re: Capture Output of Programm,redirected the pipes

Post by deano1987 »

Well this is the actual problem I am having, have any suggestions?

http://www.purebasic.fr/english/viewtop ... 13&t=46433
Post Reply