CreateProcess and redirect StdOut to a text file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: CreateProcess and redirect StdOut to a text file

  1. #1
    Join Date
    Sep 2000
    Location
    UK
    Posts
    122

    CreateProcess and redirect StdOut to a text file

    I am using CreateProcess to execute a command line call
    I want to redirect the StdOut and StdErr to a text file
    All the examples I can find show how to send StdOut to a pipe, by using CreatePipe and assigning the handle to STARTUPINFO.hStdOut

    I dont want to do this as the output is huge and can exceed max buffer size...

    I tried to use CreateFile to give me a handle to a new file, and gave this to both hStdOut and hStdErr of my STARTUPINFO, but the file always comes up empty!

    Any ideas how to redirect CreateProcess StdOut and StdErr to a new text file!
    All code on this page is protected by an SEP field!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,946

    Re: CreateProcess and redirect StdOut to a text file

    Why not just PIPE (>>) it? One pipe is the same as APPEND.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Shell ("C:\Windows\System32\cmd.exe /c  " & _
    	 "C:\Windows\System32\ipconfig >> d:\myip.txt"), vbNormalFocus
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Sep 2000
    Location
    UK
    Posts
    122

    Re: CreateProcess and redirect StdOut to a text file

    I dont want to use Shell, as I want to be able to test for System Errors!
    i.e. CreateProcess throws LastError for things like "File not found" and other system errors etc...

    CreateProcess does not allow redirects in the command line, it uses the STARTINFO to direct the StdOut and StdError to file handles
    All code on this page is protected by an SEP field!!!

  4. #4
    Join Date
    Nov 2006
    Posts
    9

    Re: CreateProcess and redirect StdOut to a text file

    Sorry, can't give you a code right now, but you need to use the CreatePipe function to create, erm, pipes, then pass the STARTF_USESTDHANDLES flag to dwFlags in the STARTINFO structure, as well as setting the hStdOutput and hStdError to the pipes you created with CreatePipe. The pipes can then be read with ReadFile.

    Sorry, I can't give you a code right now, but I hope that helped.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center