[python-win32] Inter Process Communication Troubles
Michael Lieschnegg
michael.lieschnegg at virtuellesfahrzeug.at
Thu Jul 28 13:05:43 CEST 2005
hello!
I wrote a python parent script which starts a separate script in a new
process (under Windows XP Pro SP2, Python 2.4.1). Through anonymous
pipes I have made a bidirectional communication to the child. I also
embedded the parent script in cpp and it works too! Now I have exported
the parent functions in a dll, but when I call the dll in visual basic
6, the client script does not start and I cannot communicate over the
pipes. When I use the dll in CPP it works too!. Perhaps you can help me
and can tell me the problem why my program does not run in visual basic
6.
Thank you! Regards, Michael lieschnegg
I embedded following code in the dll for creating the pipes and starting
the new process:
class master_interface:
def __init__(self, iPort):
command='C:\Programme\Python24\python.exe'
#win32api.GetModuleFileName(0)
print command
args=['-u', 'dispatcher.py', str(iPort)]
environment=None
path=None
# 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)
# set the info structure for the new process.
StartupInfo = win32process.STARTUPINFO()
StartupInfo.hStdInput = hStdin_r
StartupInfo.hStdOutput = hStdout_w
StartupInfo.hStdError = hStderr_w
StartupInfo.dwFlags = win32process.STARTF_USESTDHANDLES
# Create new output read handles and the input write handle. Set
# the inheritance properties to FALSE. Otherwise, the child
inherits
# these handles;
pid = win32api.GetCurrentProcess()
tmp = win32api.DuplicateHandle(
pid,
self.hStdin_w,
pid,
0,
0, # non-inheritable!!
win32con.DUPLICATE_SAME_ACCESS)
# Close the inhertible version of the handle
win32file.CloseHandle(self.hStdin_w)
self.hStdin_w = tmp
tmp = win32api.DuplicateHandle(
pid,
self.hStdout_r,
pid,
0,
0, # non-inheritable!
win32con.DUPLICATE_SAME_ACCESS)
# Close the inhertible version of the handle
win32file.CloseHandle(self.hStdout_r)
self.hStdout_r = tmp
tmp = win32api.DuplicateHandle(
pid,
self.hStderr_r,
pid,
0,
0, # non-inheritable!
win32con.DUPLICATE_SAME_ACCESS)
# Close the inhertible version of the handle
win32file.CloseHandle(self.hStderr_r)
self.hStderr_r = tmp
# start the process.
print "creating process"
cmdline = "%s %s" % (command, string.join(args, ' '))
hProcess, hThread, dwPid, dwTid = win32process.CreateProcess(
None, # program
cmdline,# command line
None, # process security attributes
None, # thread attributes
1, # inherit handles, or USESTDHANDLES won't work.
# creation flags. Don't access the console.
0, # Don't need anything here.
# If you're in a GUI app, you should use
# CREATE_NEW_CONSOLE here, or any subprocesses
# might fall victim to the problem described in:
# KB article: Q156755, cmd.exe requires
# an NT console in order to perform
redirection..
environment, # new environment
path, # new directory
StartupInfo)
# normally, we would save the pid etc. here...
print "process created"
# Child is launched. Close the parents copy of those pipe
handles
# that only the child should have open.
# You need to make sure that no handles to the write end of the
# output pipe are maintained in this process or else the pipe
will
# not close when the child process exits and the ReadFile will
hang.
win32file.CloseHandle(hStderr_w)
win32file.CloseHandle(hStdout_w)
win32file.CloseHandle(hStdin_r)
self.outQueue = Queue.Queue()
self.closed = 0
self.stdoutClosed = 0
self.stderrClosed = 0
..........and so on.........
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20050728/65898fed/attachment-0001.htm
More information about the Python-win32
mailing list