Tags: dear, drive, exnternal, implementing, interactively, pipe, program, programming, pure, tcl, usd
Implementing pipe in pure C
I used to usd the Tcl pipe to interactively drive an exnternal program.
Now, I may need to do it in pure C, without using tcl. Can any one give
me some hints how to do so on windows box? Maybe you can give me some
hints on how the tcl pipe is implemented on Windows box.
Thanks a lot!
Leave a comment...
- 1 Comments
- Jingzhao Ou wrote:
> I used to usd the Tcl pipe to interactively drive an exnternal program.
> Now, I may need to do it in pure C, without using tcl. Can any one give
> me some hints how to do so on windows box? Maybe you can give me some
> hints on how the tcl pipe is implemented on Windows box.
The source code for Tcl will give you all the hints you want about how
Tcl implements it, but there might be considerably more overhead there
than you need.
Not exactly a Tcl question, but...
1. Use CreatePipe() three times, once each for stdin, stdout, stderr.
2. Use DuplicateHandle() with bInheritHandle FALSE and
DUPLICATE_SAME_ACCESS on each of the pipes.
3. Close the inheritable (original) versions of the read end of the
stdout and stderr pipes.
4. Close the inheritable (original) version of the write end of the
5. Call CreateProcess() with startup info hStd(InOut)Put * hStdError
set to the remaining three inheritable handles.
6. Create three threads, one that will sit doing blocking reads on
the stdout pipe handle, on the stderr pipe handle, and one that
will write to the stdin pip handle.
When "done" with the pipe:
7. Close the stdin pipe handle. 
8. Close the stdout pipe handle. 
9. Close the stderr pipe handle. 
10. GetExitCodeProcess() the child's hProcess. TerminateProcess()
it if it's still running. 
11. Wait for threads to terminate, or end them. 
12. Close the process and thread handles to the child. 
 There might be reasons to close the stdin pipe handle earlier,
for example, if a pipeline is expecting stdin to close before it will
complete and flush output.
 Or not. There are reasons why you may want the child process to
 Or not. They might be re-used doing something else.
 Only if you expect it to exit, or desire no more direct control over
it if it does not.
MKS#1; Thu, 01 May 2008 00:11:00 GMT