I have some pretty basic C++ code:
#include <iostream>
#include <string.h>
#include <msclr\/marshal_cppstd.h>
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;
using namespace msclr::interop;
int main(int argc, char *argv[])
{
Process myprocess;
ProcessStartInfo^ proc1 = gcnew ProcessStartInfo;
std::cout << "Hello World!" << std::endl;
proc1->FileName = gcnew String("dir");
myprocess.StartInfo = proc1;
myprocess.StartInfo->CreateNoWindow = true;
myprocess.Start();
system("pause");
return 0;
}
When I run this code it executes the “dir” command in a new window and then quickly closes that window.
My question is simple: How can I have the output from this process directed into the same, original window in which “Hello World” appears?
Here is a relevant link describing the System.Diagnostics::Process() class I am using: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
Thank you very much in advance for your time and consideration.
Edit/Update:
It’s been suggested to me try modifying the StartInfo properties to:
myprocess.StartInfo->CreateNoWindow = true;
myprocess.StartInfo->RedirectStandardOutput = true;
myprocess.StartInfo->UseShellExecute = false;
Doing this appears to give me no output at all.
By default Process.Start() uses ShellExecute. Also CreateNoWindow is more like ‘detached’ than ‘use the parent’s console’.
The problem is that start tries to wrap both ShellExecute and CreateProcess.
Turn off CreateNoWindows.
Turn off UseShellExecute.
Don’t mess with the standard handles unless you want to do redirection.