Need some help debugging this issue. I tried searching online and stackoverflow but could not find a solution for this. I am fairly new to the windows api so please pardon my ignorance.
Basically, I am trying to run a c++ application that outputs to stdout and stderr from python using a specific user. For this purpose, I am invoking the binary using the LogonUser API and CreateProcessAsUser apis and using Pipes for capturing the output and error streams. Have picked up the code mostly from a demo code in python win32 module. However, for some reason when my process is dumping to error stream it gets hung at WriteFile. Looking into the crash dump I see that the filehandle is 2 (stderr). Can someone help me with the issue?
Thanks in advance
Here is the code snippet --
userToken = win32security.LogonUser(user, domain, password,
win32con.LOGON32_LOGON_BATCH,
win32con.LOGON32_PROVIDER_DEFAULT)
secAttrs = win32security.SECURITY_ATTRIBUTES()
secAttrs.bInheritHandle = 1
stdout_r, stdout_w = win32pipe.CreatePipe(secAttrs, 0)
stderr_r, stderr_w = win32pipe.CreatePipe(secAttrs, 0)
startupInfo = win32process.STARTUPINFO()
startupInfo.dwFlags = win32con.STARTF_USESTDHANDLES
startupInfo.hStdOutput = stdout_w
startupInfo.hStdError = stderr_w
ppid = win32api.GetCurrentProcess()
tmp = win32api.DuplicateHandle(ppid, stdout_r, ppid, 0, 0,
win32con.DUPLICATE_SAME_ACCESS)
win32file.CloseHandle(stdout_r)
stdout_r = tmp
procArgs = (None, # appName
cmd_line, # commandLine
None, # processAttributes
None, # threadAttributes
1, # bInheritHandles
win32process.CREATE_NEW_CONSOLE, # dwCreationFlags
None, # newEnvironment
None, # currentDirectory
startupInfo)
procHandles = win32process.CreateProcessAsUser(userToken, *procArgs)
win32file.CloseHandle(stderr_w)
win32file.CloseHandle(stdout_w)
win32security.RevertToSelf()
# Wait for process to complete
hProcess, hThread, pid, tid = procHandles
stdout_buf = os.fdopen(msvcrt.open_osfhandle(stdout_r, 0), "rb")
stdout = stdout_buf.read()
stderr_buf = os.fdopen(msvcrt.open_osfhandle(stderr_r, 0), "rb")
stderr = stderr_buf.read()
win32event.WaitForSingleObject(hProcess, 3600*1000)
rc = win32process.GetExitCodeProcess(hProcess)