Library code snippets

Execute a Process and Fetch its Output

Ever wondered how Visual Studio executes a process - such as a compiler - and displays the text it returns in its own window? It's actually really easy - and here's a simple example as to how.

Shared Function GetProcessText(ByVal process As String, ByVal param As String, ByVal workingDir As String) As String
 Dim p As Process = New Process
 ' this is the name of the process we want to execute
 p.StartInfo.FileName = process
 If Not (workingDir = "") Then
   p.StartInfo.WorkingDirectory = workingDir
 End If
 .StartInfo.Arguments = param
 ' need to set this to false to redirect output
 p.StartInfo.UseShellExecute = False
 p.StartInfo.RedirectStandardOutput = True
 ' start the process
 p.Start
 ' read all the output
 ' here we could just read line by line and display it
 ' in an output window
 Dim output As String = p.StandardOutput.ReadToEnd
 ' wait for the process to terminate
 p.WaitForExit
 Return output
End Function

Comments

  1. 07 Aug 2006 at 07:18

    How can you do that in C++?

    28-11-06:

    LOL figured it out here it is:

    You'll need to include windows.h and string, and use the standard namespace ie.

    #include <windows.h>
    #include <string>

    using namespace std;


    This can go anywhere:

     // output handle
     HANDLE stdOut = NULL;

     // startup information
     STARTUPINFO sai;
     ZeroMemory( &sai, sizeof( STARTUPINFO ) );
     sai.hStdOutput = stdOut; // all that is needed is to change the StdOut handle
     sai.cb = sizeof( sai );



     // process information structure
     PROCESS_INFORMATION pi;

     // create a test process
     CreateProcess( "[program to execute]", "[arguments, or nothing]", NULL, NULL, FALSE, 0, NULL, "[directory for program to run in]", &sai, &pi );

     // wait for it to finish
     WaitForSingleObject( pi.hProcess, INFINITE );

     // string to hold final data
     string output = "";

     // buffer for data - 32 characters is nice and small - won't use up a lot of memory
     char* buff = new char[ 32 ];

     // number of characters read
     DWORD numRead;

     // result of the read operation
     BOOL bRes;

     // get all of the data
     while( !bRes && numRead != 0 )
     {
      // read the handle
      bRes = ReadFile( stdOut, buff, 32, &numRead, NULL );



      // append to the string
      output += buff;
     }

     // output it
     cout << output << endl;

    // delete the buffer
    delete[32] buff;

  2. 02 Sep 2005 at 01:48

    I think that the WaitForExit must be called before StandardOutput.ReadToEnd.
               

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Execute a Process and Fetch its Output.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related jobs

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!