winnt win32process.createProcess with stdout to file ?

Discussion in 'Python' started by david.humpherys@gmail.com, Mar 3, 2005.

  1. Guest

    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)
     
    , Mar 3, 2005
    #1
    1. Advertising

  2. Peter Hansen Guest

    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
     
    Peter Hansen, Mar 3, 2005
    #2
    1. Advertising

  3. Roger Upole Guest

    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_WRITE)
    Also, you shouldn't have FILE_FLAG_OVERLAPPED set if you're
    not passing an overlapped object into CreateFile.

    hth
    Roger


    <> wrote in message
    news:...
    > 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 =----
     
    Roger Upole, Mar 4, 2005
    #3
  4. Guest

    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_WRITE, 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
     
    , Mar 4, 2005
    #4
  5. Roger Upole Guest

    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

    <> wrote in message
    news:...
    > 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_WRITE, 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 =----
     
    Roger Upole, Mar 4, 2005
    #5
  6. Guest

    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)
     
    , Mar 4, 2005
    #6
  7. Roger Upole Guest

    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


    <> wrote in message
    news:...
    > 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 =---
     
    Roger Upole, Mar 4, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lukas
    Replies:
    1
    Views:
    515
    Mothra
    May 19, 2004
  2. Kroum Grigorov
    Replies:
    1
    Views:
    712
    Christopher Benson-Manica
    Apr 26, 2004
  3. Elly Sokol

    CreateProcess

    Elly Sokol, Jun 17, 2004, in forum: C++
    Replies:
    13
    Views:
    4,443
    Frederic Banaszak
    Jun 18, 2004
  4. lukas
    Replies:
    1
    Views:
    361
    Harald Massa
    May 18, 2004
  5. addinall

    WinNT ActiveState STDERR and STDOUT

    addinall, Aug 25, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    215
    Ben Morrow
    Aug 26, 2006
Loading...

Share This Page