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
-
MSSQL Query in VB.Net Fails
by 7upsk (1 replies)
-
bar graphs in visual basic.net
by bhabybash (1 replies)
-
How to write the category attribut in a class dynamically
by converter2009 (1 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
VB.Net Button Problem
by pysdex (0 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
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;
This thread is for discussions of Execute a Process and Fetch its Output.