Anonymous Pipes (Was do anonymous pipes normally work on NT?)

Florent Heyworth radbit at my-deja.com
Wed Jun 16 16:23:21 EDT 1999


Hi

looking at the test scripts Georg Mischler provided it seems the code is
failing
to pass on the standard handles to the created process. The following
method works:

file runproc.py
---------------

'''runproc.py

start a process with three inherited pipes.
Try to write to and read from those.
'''

import win32api
import win32pipe
import win32file
import win32process
import win32security
import win32event
import os

#Constants for stdandard handles
STD_ERR_HANDLE=-12
STD_OUTPUT_HANDLE=-11
STD_INPUT_HANDLE=-10

class Process:
    def run(self, cmdline):

        # security attributes for pipes
        sAttrs = win32security.SECURITY_ATTRIBUTES()
        sAttrs.bInheritHandle = 1

        # create pipes
        hStdin_r,self.hStdin_w=win32pipe.CreatePipe(sAttrs, 0)
        self.hStdout_r,hStdout_w=win32pipe.CreatePipe(sAttrs,0)
        self.hStderr_r,hStderr_w=win32pipe.CreatePipe(sAttrs, 0)

        #associate Standard output to previously open handle
        win32api.SetStdHandle(STD_OUTPUT_HANDLE,hStdout_w)

        # set the info structure for the new process.
        StartupInfo = win32process.STARTUPINFO()

        # start the process.
        hProcess, hThread, dwPid, dwTid=win32process.CreateProcess(
                None, #program
                cmdline,# command line
                sAttrs,  # process security attributes
                sAttrs,  # thread attributes
                1,      # inherit handles,
                win32process.NORMAL_PRIORITY_CLASS,
                None,  # no new environment
                None,  # current directory (stay where we are)
                StartupInfo)
        # normally, we would save the pid etc. here...


        res, str = win32file.ReadFile(self.hStdout_r,100)
        if res == 0: print 'read:', str
        else: 'read nothing.'


if __name__ == '__main__':
    p = Process()
    p.run('..\\python.exe testpipe.py')
-----------------
end of runproc.py

file testpipe.py - explicit assignement of standard handles
----------------
import win32api
import win32file

STD_ERR_HANDLE=-12
STD_OUTPUT_HANDLE=-11
STD_INPUT_HANDLE=-10

std_in=win32api.GetStdHandle(STD_INPUT_HANDLE)
std_err=win32api.GetStdHandle(STD_ERR_HANDLE)
std_out=win32api.GetStdHandle(STD_OUTPUT_HANDLE)

win32file.WriteFile(std_out, 'this method works....')

end of testpipe.py
------------------

file testpipe1.py implicit standard handles
------------------
print "this method works as well..."

end of testpipe1.py
-----------------

Cheers

Florent Heyworth







More information about the Python-list mailing list