3

I'm creatin application(C#) that need run another app. This. app is game(C++,Directx7,GDI, I don't have source) that show console window for debugging from dll(static). For show console window this dll has this lines:

AllocConsole();
freopen("CONIN$","rb",stdin);   
freopen("CONOUT$","wb",stdout);
freopen("CONOUT$","wb",stderr);

In my c# app. i want hide console window and redirect text from console window to textbox. For hide console window i'm using winapi FindWindow, ShowWindow is not problem. But how i can redirect text(output) from console window to textbox?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

1

You can run your game using following code:

     Process process = new Process();
     process.StartInfo.FileName = "\"" + pathToGame + "\"";
     //process.StartInfo.Arguments = args;
     process.StartInfo.RedirectStandardOutput = true;
     process.StartInfo.RedirectStandardError = true;
     process.StartInfo.UseShellExecute = false;
     process.OutputDataReceived += new DataReceivedEventHandler(ReadOutput);
     process.ErrorDataReceived += new DataReceivedEventHandler(ReadOutput);

     process.Start();
     process.BeginOutputReadLine();
     process.BeginErrorReadLine();

     //process.WaitForExit();

CL output and errors will go here

  private static void ReadOutput(object sender, DataReceivedEventArgs e)
  {
     if (e.Data != null)
     {
        //your output here
     }
  }
VladL
  • 12,769
  • 10
  • 63
  • 83
  • I'm already use this code. Is not work. Programm is stop when i'm setup breakpoint in ReadOutput but e.Data is null alltime. – user1950843 Jan 05 '13 at 12:44
  • @user1950843 This event fires even without text output sometimes. If you really see the output as you said in the comment, so it will be not null at some time. Instead of breakpoint write e.Data to the file. – VladL Jan 05 '13 at 12:48
  • I'm write to file but it's empty. Maybe problem in OS? I have Windows 8 Enterprise(with installed updates). Maybe this code will be work in Windows XP, but it's too old OS. – user1950843 Jan 05 '13 at 13:16
  • @user1950843 so one more time. You run your game from the command line, for example C:\Program Files\MyGame>mygame.exe . Do you see the desired output in the same command line? – VladL Jan 05 '13 at 13:19
  • No, i see desired output in new window that create a game. – user1950843 Jan 05 '13 at 13:37
  • @user1950843 well, I'm also interested if it's possible to catch output from custom window, will vote on your question – VladL Jan 05 '13 at 13:40
  • I'm found interesting web pages: http://msdn.microsoft.com/en-us/library/ms682073(v=vs.85).aspx Will be research tomorrow. Currently i'm tired. – user1950843 Jan 05 '13 at 13:55