473,666 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

winnt win32process.cr eateProcess 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.S ECURITY_ATTRIBU TES()
sa.bInheritHand le = 1

startInfo = win32process.ST ARTUPINFO()
startInfo.dwFla gs = win32process.ST ARTF_USESTDHAND LES

fh = win32file.Creat eFile("c:/mylog.log", win32file.GENER IC_WRITE, 0,
sa, win32file.OPEN_ EXISTING, win32file.FILE_ FLAG_SEQUENTIAL _SCAN |
win32file.FILE_ FLAG_OVERLAPPED , 0)
startInfo.hStdO utput = fh
startInfo.hStdE rror = win32api.GetStd Handle(win32api .STD_ERROR_HAND LE)
startInfo.hStdI nput = win32api.GetStd Handle(win32api .STD_INPUT_HAND LE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.Cr eateProcess
\
( None, cmd, None, None, 1, win32con.NORMAL _PRIORITY_CLASS , None, None,
startInfo)

Jul 18 '05 #1
6 9240
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|win 32file.FILE_SHA RE_WRITE)
Also, you shouldn't have FILE_FLAG_OVERL APPED set if you're
not passing an overlapped object into CreateFile.

hth
Roger
<da************ *@gmail.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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.S ECURITY_ATTRIBU TES()
sa.bInheritHand le = 1

startInfo = win32process.ST ARTUPINFO()
startInfo.dwFla gs = win32process.ST ARTF_USESTDHAND LES

fh = win32file.Creat eFile("c:/mylog.log", win32file.GENER IC_WRITE, 0,
sa, win32file.OPEN_ EXISTING, win32file.FILE_ FLAG_SEQUENTIAL _SCAN |
win32file.FILE_ FLAG_OVERLAPPED , 0)
startInfo.hStdO utput = fh
startInfo.hStdE rror = win32api.GetStd Handle(win32api .STD_ERROR_HAND LE)
startInfo.hStdI nput = win32api.GetStd Handle(win32api .STD_INPUT_HAND LE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.Cr eateProcess
\
( 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.S ECURITY_ATTRIBU TES()
sa.bInheritHand le = 1

startInfo = win32process.ST ARTUPINFO()
startInfo.dwFla gs = win32process.ST ARTF_USESTDHAND LES

fh = win32file.Creat eFile("c:/mylog.log", win32file.GENER IC_WRITE,
win32file.FILE_ SHARE_READ|win3 2file.FILE_SHAR E_WRITE, sa,
win32file.OPEN_ ALWAYS, win32file.FILE_ FLAG_SEQUENTIAL _SCAN , 0)
startInfo.hStdO utput = fh
startInfo.hStdE rror = win32api.GetStd Handle(win32api .STD_ERROR_HAND LE)
startInfo.hStdI nput = win32api.GetStd Handle(win32api .STD_INPUT_HAND LE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.Cr eateProcess
\
( None, cmd, sa, sa, 1, win32con.NORMAL _PRIORITY_CLASS , None, None,
startInfo)
ERROR:
pywintypes.erro r:(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.goo glegroups.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.S ECURITY_ATTRIBU TES()
sa.bInheritHand le = 1

startInfo = win32process.ST ARTUPINFO()
startInfo.dwFla gs = win32process.ST ARTF_USESTDHAND LES

fh = win32file.Creat eFile("c:/mylog.log", win32file.GENER IC_WRITE,
win32file.FILE_ SHARE_READ|win3 2file.FILE_SHAR E_WRITE, sa,
win32file.OPEN_ ALWAYS, win32file.FILE_ FLAG_SEQUENTIAL _SCAN , 0)
startInfo.hStdO utput = fh
startInfo.hStdE rror = win32api.GetStd Handle(win32api .STD_ERROR_HAND LE)
startInfo.hStdI nput = win32api.GetStd Handle(win32api .STD_INPUT_HAND LE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.Cr eateProcess
\
( None, cmd, sa, sa, 1, win32con.NORMAL _PRIORITY_CLASS , None, None,
startInfo)
ERROR:
pywintypes.erro r:(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.Termin ateProcess(hPro cess,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.Cr eateProcess \
( None, self.RENDER_STR ING, sa, sa, 1,
win32con.NORMAL _PRIORITY_CLASS , None, None, startInfo)

while self.exitCode == 259:
if self.stop:
print "kill da process"
win32api.Termin ateProcess(hPro cess,3)

self.exitCode = win32process.Ge tExitCodeProces s(hProcess)
time.sleep(2)

Jul 18 '05 #6
TerminateProces s 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******** *************@l 41g2000cwc.goog legroups.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.Termin ateProcess(hPro cess,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.Cr eateProcess \
( None, self.RENDER_STR ING, sa, sa, 1,
win32con.NORMAL _PRIORITY_CLASS , None, None, startInfo)

while self.exitCode == 259:
if self.stop:
print "kill da process"
win32api.Termin ateProcess(hPro cess,3)

self.exitCode = win32process.Ge tExitCodeProces s(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
3462
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, sizeof(sa) );
1
5683
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 a DLL from other project within the solution and including some user controls within my new project, but whenever I try to execute a new aspx I have coded I receive next error message (see below); the problem is that some times the Parser Error...
9
18282
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 unsuccessful test. int Test_writeHello() {
1
10838
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 calling line. Please provide some input. CODE ==== Structure STARTUPINFO Public cb As Long
6
4108
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 say I want to create a process from an array of bytes or memory stream containing the executable data cause I want to prevent my program executable module from being stolen..
2
1496
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 specific group.) Here's the situation in a nutshell. We're using WinXP Pro workstations configured to a common standard on a nation-wide intra-network and we recently migrated one operation involving networking with a WinNT server (with...
3
5227
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 single batch file containing the upload instructions create_single_upload(i);
5
2606
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 sys.stdout = self
1
2081
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 computers for a long, long time, but VB/.NET/ etc. is not my specialty.
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.