Library code snippets
Execute a Process and Fetch its Output
By James Crowley, published on 30 Jan 2005
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
Related articles
Related discussion
-
How can I execute server-side function using asp.net Ontextchanged orJavascript onchange?
by konikula (6 replies)
-
how to make smtp in vb.net
by konikula (1 replies)
-
String size limit and array upperbound limit
by konikula (3 replies)
-
hi... need help...........
by konikula (1 replies)
-
DataGridViewComboBoxColumn events
by konikula (1 replies)
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum)
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!
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;
I think that the WaitForExit must be called before StandardOutput.ReadToEnd.
This thread is for discussions of Execute a Process and Fetch its Output.