win32process.CreateProcess inherit handles not working ?
David Bolen
db3l at fitlinxx.com
Tue Mar 26 23:23:04 EST 2002
teyc at cognoware.com (Chui Tey) writes:
> 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
I don't know that this is the most elegant solution, but I've used
DuplicateHandle rather than security attributes to create inheritable
handles in the past successfully. It was a while ago, but I seem to
recall getting into trouble with SAs because they also include the
DACL which defaults to no access unless you explicitly assign it a
NULL DACL, and it's just sort of a pain in the neck in terms of extra
steps just to inherit the darn handle properly.
I don't know that it's what is happening here, but perhaps Win98 is
ignoring the security information whereas NT/W2K aren't. I tried
modifying your code to use DuplicateHandle and it seems to work fine
on my NT4 SP5 system.
I'm sure there's a way to make it work with a security attribute as
well (and avoid the extra file handle), but this isn't all that bad.
So something like:
- - - - - - - - - - - - - - - - - - - - - - - - -
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 win32event
import win32api
import win32con
u_filein = pywintypes.Unicode(filein)
u_fileout = pywintypes.Unicode(fileout)
SI = win32process.GetStartupInfo()
SI.dwFlags = win32process.STARTF_USESTDHANDLES
hInput = win32file.CreateFile(
u_filein, # filename
win32file.GENERIC_READ, # mode
win32file.FILE_SHARE_READ, # share
None, # security attributes
win32file.OPEN_EXISTING, # creation disposition
win32file.FILE_ATTRIBUTE_NORMAL, # flags
0 # hTemplate file
)
hOutput = win32file.CreateFile(
u_fileout, # filename
win32file.GENERIC_WRITE, # mode
win32file.FILE_SHARE_WRITE, # share
None, # security attributes
win32file.CREATE_ALWAYS, # creation disposition
win32file.FILE_ATTRIBUTE_NORMAL, # flags
0 # hTemplate file
)
hStdInput = win32api.DuplicateHandle(win32api.GetCurrentProcess(),
hInput,
win32api.GetCurrentProcess(),
0,1,
win32con.DUPLICATE_SAME_ACCESS)
hStdOutput = win32api.DuplicateHandle(win32api.GetCurrentProcess(),
hOutput,
win32api.GetCurrentProcess(),
0,1,
win32con.DUPLICATE_SAME_ACCESS)
SI.hStdInput = hStdInput
SI.hStdOutput = hStdOutput
SI.hStdError = hStdOutput
cmdline = app + " " + " ".join(args)
hProcess, hThread, pid, threadid = \
win32process.CreateProcess(
app, # lpApplicationName
cmdline, # command line
None, # Process security attributes
None, # Thread security attributes
1, # Inherit handles
win32process.DETACHED_PROCESS, #
None,
None,
SI)
win32event.WaitForSingleObject(hProcess, win32event.INFINITE)
win32file.CloseHandle(hInput)
win32file.CloseHandle(hOutput)
win32file.CloseHandle(hStdInput)
win32file.CloseHandle(hStdOutput)
return pid
- - - - - - - - - - - - - - - - - - - - - - - - -
--
-- David
--
/-----------------------------------------------------------------------\
\ David Bolen \ E-mail: db3l at fitlinxx.com /
| FitLinxx, Inc. \ Phone: (203) 708-5192 |
/ 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
\-----------------------------------------------------------------------/
More information about the Python-list
mailing list