1

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);
        }
    }
Kfir Eichenblat
  • 449
  • 2
  • 8
  • 27
  • 1
    Hi, Have you developed the game or do you want it to work with any games? – Patrick D'Souza Apr 21 '13 at 07:05
  • See this: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/192b6df7-9437-42cf-81c1-c125021735ba and http://stackoverflow.com/questions/6655613/why-does-standardoutput-read-block-when-startinfo-redirectstandardinput-is-set for some ideas – jordanhill123 Apr 21 '13 at 07:07
  • @Romulus I am developing the game at the time being. It could be any program I'm creating, it's the first time I'm making a "launcher" as this time, it's important for me to know of any kind of exit. So basically, I'm going to use the launcher as a "container" for the game, to keep important data and use it after any kind of exit. – Kfir Eichenblat Apr 21 '13 at 12:52
  • 1
    Console redirection only works on console mode programs, not the kind of target you'd select for a game. It is a pretty miserable way to to process interop in general, many possible failure modes. Do uplift this to one of the IPC mechanisms supported by .NET. In a case like this, a Socket tends to fit the needs well. Works across a network, you'll get a running start on a game server that supports multiplayer scenarios. – Hans Passant Apr 21 '13 at 14:56
  • @Kfirprods: Can you have a look at my post. – Patrick D'Souza Apr 21 '13 at 16:33

1 Answers1

1

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
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39