473,416 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

winnt win32process.createProcess with stdout to file ?

os:winnt
python2.3.2

I have a exe that dumps info to the command line. I want to run this
process and capture the stdout into a file. I think i'm close... any
help appreciated.

dh
--------------------------------------------------------------------------
import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE, 0,
sa, win32file.OPEN_EXISTING, win32file.FILE_FLAG_SEQUENTIAL_SCAN |
win32file.FILE_FLAG_OVERLAPPED , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, None, None, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)

Jul 18 '05 #1
6 9202
da*************@gmail.com wrote:
os:winnt
python2.3.2

I have a exe that dumps info to the command line. I want to run this
process and capture the stdout into a file. I think i'm close... any
help appreciated.


Use the standard "subprocess" module that comes in Python2.4.

Or, if you can't upgrade for some reason, it sounds to me
like os.popen() will do what you want... that's the older
standard technique.

No need to resort to lots of win32 stuff.

-Peter
Jul 18 '05 #2
You'll need to pass security attributes with inherit=True
to CreateProcess also, and the file has to be opened with sharing.
(win32file.FILE_SHARE_READ|win32file.FILE_SHARE_WR ITE)
Also, you shouldn't have FILE_FLAG_OVERLAPPED set if you're
not passing an overlapped object into CreateFile.

hth
Roger
<da*************@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
os:winnt
python2.3.2

I have a exe that dumps info to the command line. I want to run this
process and capture the stdout into a file. I think i'm close... any
help appreciated.

dh
--------------------------------------------------------------------------
import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE, 0,
sa, win32file.OPEN_EXISTING, win32file.FILE_FLAG_SEQUENTIAL_SCAN |
win32file.FILE_FLAG_OVERLAPPED , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, None, None, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 18 '05 #3
Roger, I updated the script (below).. but now I get errors...
many thanks for your help.

import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ|win32file.FILE_SHARE_WRI TE, sa,
win32file.OPEN_ALWAYS, win32file.FILE_FLAG_SEQUENTIAL_SCAN , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, sa, sa, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)
ERROR:
pywintypes.error:(2, 'createProcess', ' the system cannot find the file
specified.;)

it's strange that it gets this error even though i have the
win32file.OPEN_ALWAYS flag set..

any suggestions... (soo... close!! )
thanks, thanks!
david

Jul 18 '05 #4
If it got past the CreateFile call, the problem's not with the log file.
This error from CreateProcess means it can't find your executable.

Roger

<da*************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Roger, I updated the script (below).. but now I get errors...
many thanks for your help.

import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ|win32file.FILE_SHARE_WRI TE, sa,
win32file.OPEN_ALWAYS, win32file.FILE_FLAG_SEQUENTIAL_SCAN , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, sa, sa, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)
ERROR:
pywintypes.error:(2, 'createProcess', ' the system cannot find the file
specified.;)

it's strange that it gets this error even though i have the
win32file.OPEN_ALWAYS flag set..

any suggestions... (soo... close!! )
thanks, thanks!
david


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 18 '05 #5
Aweseome! Many many thanks Roger ! You've made my day.

The last thing that you may be able to help with...
I'm using
win32api.TerminateProcess(hProcess,3)
to kill this process if it gets outta hand...
but when i do so.. it seems that the stdout/stderr don't capture the
output.

here's my last chunk of code.
again.. thank you so much.

hProcess, hThread, dwProcessId, dwThreadId =
win32process.CreateProcess \
( None, self.RENDER_STRING, sa, sa, 1,
win32con.NORMAL_PRIORITY_CLASS, None, None, startInfo)

while self.exitCode == 259:
if self.stop:
print "kill da process"
win32api.TerminateProcess(hProcess,3)

self.exitCode = win32process.GetExitCodeProcess(hProcess)
time.sleep(2)

Jul 18 '05 #6
TerminateProcess doesn't give it a chance to exit normally
and do any cleanup that would happen if it exited itself.
It may not have been able to flush its file buffers, etc.
Does the executable have any way to signal it to exit ?

Roger
<da*************@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Aweseome! Many many thanks Roger ! You've made my day.

The last thing that you may be able to help with...
I'm using
win32api.TerminateProcess(hProcess,3)
to kill this process if it gets outta hand...
but when i do so.. it seems that the stdout/stderr don't capture the
output.

here's my last chunk of code.
again.. thank you so much.

hProcess, hThread, dwProcessId, dwThreadId =
win32process.CreateProcess \
( None, self.RENDER_STRING, sa, sa, 1,
win32con.NORMAL_PRIORITY_CLASS, None, None, startInfo)

while self.exitCode == 259:
if self.stop:
print "kill da process"
win32api.TerminateProcess(hProcess,3)

self.exitCode = win32process.GetExitCodeProcess(hProcess)
time.sleep(2)



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kal | last post by:
Hi, I am trying to write an application that will launch a second application using CreateProcess... SECURITY_ATTRIBUTES sa; STARTUPINFO si; PROCESS_INFORMATION pi; ::ZeroMemory( &sa,...
1
by: Ivan | last post by:
Hi, I have moved an entire solution from .netframework 1.0 to 1.1; also I have migrated the solution to VS2003. After this I have added a new project (Web) to previous solution; I'm referencing...
9
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for...
1
by: Jai | last post by:
Please provide sample code for using Createprocess in VB.NET. The code I am using is given below. But I get "Error 91: Object referrence not set to an instance of the object" at CreateProcess...
6
by: ss.teo | last post by:
I want to fork a new process and my executable binary is stored inside somewhere. Is there any possible way to fork a process using CreateProcess API without writing to the disk first? Like let's...
2
by: Don Rich | last post by:
Please share with me any ideas you may have for troubleshooting and resolving the subject problem. I can give more details as necessary. (Please advise if I should post this problem to a more...
3
by: wtfdan | last post by:
When I call the method perform_single_upload() in this loop: ---------------------------- private: System::Void start_uploads(int i) { i = 0; while(i < master_list->Length) { //Create a...
5
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z | last post by:
I wanna print the log to both the screen and file, so I simulatered a 'tee' class Tee(file): def __init__(self, name, mode): file.__init__(self, name, mode) self.stdout = sys.stdout...
1
by: mrpetegroups | last post by:
Hi - Problem: The file below has become zero length \winnt\assembly\GAC_MSIL\system.deployment\2.0.0.0_b03f5f7f11d50a3a \system.deployment.dll Details follow ... I've been programming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.