0

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.

Rob S.
  • 3,599
  • 6
  • 30
  • 39
  • possible duplicate of [ProcessInfo and RedirectStandardOutput](http://stackoverflow.com/questions/1145969/processinfo-and-redirectstandardoutput) – Hans Passant Mar 03 '12 at 19:44
  • @Hans Passant, I looked at that question prior to posting and attempted enabling the recommended properties but then the "dir" command wasn't even run at all. Perhaps that questions being C# and this being C++ had something to do with it? – Rob S. Mar 03 '12 at 19:47
  • Not mentioning that you are actually aware of the RedirectStandardOutput directory is going to get you a lot of useless answers. Of course nobody can help you with code you didn't post. – Hans Passant Mar 03 '12 at 19:51
  • @HansPassant I did post all of my code and I did receive the answer I was looking for in only two answers replied. Thank you for suggestions though. Cheers! – Rob S. Mar 03 '12 at 20:08

2 Answers2

2

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.

Joe
  • 2,946
  • 18
  • 17
0

Use RedirectStandardOutput property in the ProcessStartInfo class. It's then written to Process.StandardOutput.

Ollie
  • 1
  • Hi! I modified the startInfo properties to: CreateNoWindow = true; RedirectStandardOutput = true; UseShellExecute = false; (It crashed without UseShellExecute set to false) and now I don't get any output at from Process.Start() – Rob S. Mar 03 '12 at 19:52