Kfir Eichenblat
Kfir Eichenblat

Reputation: 469

C# Process StandardOutput. How to send Output from the ran EXE?

I have two programs, one is a game and one is a launcher for the game. I created the launcher, in the first place, to receive basic information from the game and detect any kind of exit (crashes, Task Manager process stop, etc)

I will attach my current code for the process runner, it seems like all solutions on the internet, but what I can't figure out is how to make the game send information to the launcher. I tried Console.WriteLine("login=..."); but it doesn't seem to send anything.

     private void button1_Click(object sender, EventArgs e)
     {
        using (Process exeProcess = Process.Start(new ProcessStartInfo() { UseShellExecute = false,
        FileName = "Game.exe",
        WorkingDirectory = Environment.CurrentDirectory,
        RedirectStandardOutput = true}))
        {
            string output = "";
            while (!exeProcess.HasExited)
            {
                try
                {
                    output += exeProcess.StandardOutput.ReadToEnd() + "\r\n";
                }
                catch (Exception exc)
                {
                    output += exc.Message + "::" + exc.InnerException + "\r\n";
                }
            }

            MessageBox.Show(output);
        }
    }

Upvotes: 1

Views: 696

Answers (1)

Patrick D'Souza
Patrick D'Souza

Reputation: 3573

With respect to your code, by adding the following line you can obtain error messages that were thrown by the game.

RedirectStandardError = true,

If you are developing your game in .NET you can return appropriate error codes as follows. Based on the error code you can then display appropriate messages in you launcher

    enum GameExitCodes
    {
        Normal=0,
        UnknownError=-1,
        OutOfMemory=-2
    }

    //Game Application
    static void Main(string[] args)
    {
        try
        {
            // Start game

            Environment.ExitCode = (int)GameExitCodes.Normal;
        }
        catch (OutOfMemoryException)
        {
            Environment.ExitCode = (int)GameExitCodes.OutOfMemory;
        }
        catch (Exception)
        {
            Environment.ExitCode = (int)GameExitCodes.UnknownError;
        }
    }

NOTE: You can take a look at this open source game launcher developed in C# as a reference or modify it as per your needs.

EDIT: Added info as per comment

There are multiple ways to enable communication between between 2 .NET processes. They are

  1. Anonymous Pipes
  2. Named Pipes
  3. Using Win32 WM_COPYDATA
  4. MSMQ

Upvotes: 1

Related Questions