How do you get pub.freeprocess to read i/o ??

BlitzMax Forums/BlitzMax Programming/How do you get pub.freeprocess to read i/o ??

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Aha! - What you have here is that net.exe wants to output an error, so instead of writing to the normal pipe stream, it outputs to a dedicated error pipe stream... This error stream can be used in exactly the same way as term_process.pipe, except it's term_process.err:

SuperStrict

Framework brl.standardio
Import pub.freeprocess

?Win32

	'Lets use OO for the hell of it, although you can use CreateProcess() if you prefer
	'There is only 1 flag available - HIDECONSOLE that is supposed to hide console output ;-) but it doesn't make much difference either way on my Windows machine
	Local temp_process:TProcess = TProcess.Create("net start THIS_SERVICE_DOESNT_EXIST",HIDECONSOLE)
	
	Print ""
	Print "Net Start Status: " + temp_process.Status()	'Show that the program is running
	Print "================================"
	
	While temp_process.Status()	'Checks to see if the process is still running
		
		'Temp variable that stores output from the pipe before it is converted to a string
		Local tmpBytes:Byte[]
		'Check if the pipe contains any data that hasn't been read and if so, load data into variable array, and convert array to string
		If temp_process.pipe.ReadAvail() Then tmpBytes = temp_process.pipe.ReadPipe();Print String.FromBytes( Varptr tmpBytes[0], tmpBytes.length )
		If temp_process.err.ReadAvail() Then tmpBytes = temp_process.err.ReadPipe();Print String.FromBytes( Varptr tmpBytes[0], tmpBytes.length )
		
	Wend
	

		
	'Terminate process manually (the fact that is has escaped the above loop means it has already closed but put it in as an example)
	temp_process.Close()
	'This is a quick way you can terminate all processes without bothering to loop through them all (again not needed)
	TProcess.TerminateAll()
	
	Print "================================"
	Print "Net Start Status: " + temp_process.Status()	'Show that the program has ended
	Print ""

?MacOs

	Print "Mac OS X doesn't have Net Start."

?Linux

	Print "Linux doesn't have Net Start."

?


Notice the additional line I've added to catch error messages! It's useful that you can separate out error messages from standard output without even having to parse - usually if a process outputs there then this is an important error that would affect the normal output and it can be identified immediately.

Sorry I forgot to mention this, I don't think there is anything else you have to look out for.

;-)