win32process.CreateProcess inherit handles not working ?
Chui Tey
teyc at cognoware.com
Wed Mar 20 22:12:33 EST 2002
Hi,
I've been trying to get this going for a while and I'd appreciate it
if
someone can point out what's wrong with the following test? It works
fine on Win98 but fails on NT and W2K. I am using Python 2.1
# test.py
#
def setup():
open("slave.py","w").write("""
import sys
while 1:
line = sys.stdin.readline()
if not line: break
sys.stdout.write("read: %s" % line)
""")
open("slavein.txt","w").write("""
The
Quick
Brown
Fox
""")
def test():
"Does the equivalent of C:\\python21\\python.exe slave.py <
slavein.txt > slaveout.txt"
_CreateProcess( \
"C:\\python21\\pythonw.exe", # app
['slave.py'], # args
'slavein.txt', # input file
'slaveout.txt') # output file
if open('slaveout.txt','r').read() == "":
print "Error: stdout not redirected!"
def _CreateProcess(app, args, filein, fileout):
"""Returns the pid.
app = executable name
args = list of arguments
filein = name of file to redirect to stdin
fileout = name of file to redirect the stdout to
"""
import win32process
import win32file
import pywintypes
import win32security
import win32event
import win32api
u_filein = pywintypes.Unicode(filein)
u_fileout = pywintypes.Unicode(fileout)
SA_proc = win32security.SECURITY_ATTRIBUTES()
SA_thrd = win32security.SECURITY_ATTRIBUTES()
SA_proc.bInheritHandle = 1
SA_thrd.bInheritHandle = 1
#SA = None
SI = win32process.GetStartupInfo()
SI.dwFlags = win32process.STARTF_USESTDHANDLES
hStdInput = win32file.CreateFile(
u_filein, # filename
win32file.GENERIC_READ, # mode
win32file.FILE_SHARE_READ, # share
SA_proc, # security attributes
win32file.OPEN_EXISTING, # creation disposition
win32file.FILE_ATTRIBUTE_NORMAL, # flags
0 # hTemplate file
)
hStdOutput = win32file.CreateFile(
u_fileout, # filename
win32file.GENERIC_WRITE, # mode
win32file.FILE_SHARE_WRITE, # share
SA_proc, # security attributes
win32file.CREATE_ALWAYS, # creation disposition
win32file.FILE_ATTRIBUTE_NORMAL, # flags
0 # hTemplate file
)
SI.hStdInput = SI.hStdInput
SI.hStdOutput = SI.hStdOutput
SI.hStdError = SI.hStdOutput
cmdline = app + " " + " ".join(args)
hProcess, hThread, pid, threadid = \
win32process.CreateProcess(
app, # lpApplicationName
cmdline, # command line
SA_proc, # Process security attributes
SA_thrd, # Thread security attributes
1, # Inherit handles
win32process.DETACHED_PROCESS, #
None,
None,
SI)
win32event.WaitForSingleObject(hProcess, win32event.INFINITE)
win32file.CloseHandle(hStdInput)
win32file.CloseHandle(hStdOutput)
return pid
if __name__=="__main__":
setup()
test()
More information about the Python-list
mailing list