Advertisement
Guest User

PipeServer.cs

a guest
Jul 22nd, 2013
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. class PipeServer
  2. {
  3.     static
  4.     int
  5.     Main(string[] args)
  6.     {
  7.         if(args.Length < 2
  8.         ||(System.String.Compare(args[0], "in")  != 0
  9.         && System.String.Compare(args[0], "out") != 0)) {
  10.             System.Console.WriteLine("Usage: PipeServer <in | out> <process> <args>");
  11.            
  12.             return 1;
  13.         }
  14.  
  15.         ///////////////////////////////////
  16.         // // // Process arguments // // //
  17.         ///////////////////////////////////
  18.         // Convert pipe direction
  19.         System.IO.Pipes.PipeDirection pipe_dir = 0;
  20.         if(System.String.Compare(args[0], "in") == 0) {
  21.             pipe_dir = System.IO.Pipes.PipeDirection.In;
  22.         }
  23.         if(System.String.Compare(args[0], "out") == 0) {
  24.             pipe_dir = System.IO.Pipes.PipeDirection.Out;
  25.         }
  26.        
  27.         // Find process name to start
  28.         string proc_name = args[1];
  29.        
  30.         // Build commandline argument string
  31.         string proc_args = "";
  32.         for(System.UInt16 i = 2; i < args.Length; i++) {
  33.             if(args[i].IndexOf(" ") > -1) {
  34.                 proc_args += "\"" + args[i].Replace("\"", "\\\"") + "\" ";
  35.             } else {
  36.                 proc_args += args[i] + " ";
  37.             }
  38.         }
  39.        
  40.         // Create server
  41.         string                                pipe_name   = "";
  42.         System.IO.Pipes.NamedPipeServerStream pipe_stream = null;
  43.         for(System.UInt16 i = 1; i < 65535; i++) {
  44.             // Generate new pipe name
  45.             pipe_name = "pipeserver_" + System.Convert.ToString(i);
  46.            
  47.             try {
  48.                 // Start server
  49.                 pipe_stream = new System.IO.Pipes.NamedPipeServerStream(pipe_name, pipe_dir, 1);
  50.                
  51.                 break;
  52.             } catch(System.IO.IOException _) {
  53.                 continue;
  54.             }
  55.         }
  56.         if(pipe_stream == null) {
  57.             System.Console.WriteLine("Could not create pipe");
  58.            
  59.             return 1;
  60.         }
  61.        
  62.         // Make sure the process knows about the pipe name
  63.         proc_args = proc_args.Replace("{pipe}", "\\\\.\\pipe\\" + pipe_name);
  64.        
  65.         // Run process
  66.         System.Diagnostics.Process proc = new System.Diagnostics.Process();
  67.         proc.StartInfo.FileName  = proc_name;
  68.         proc.StartInfo.Arguments = proc_args;
  69.         proc.Start();
  70.        
  71.         // Connect pipes and wait until EOF
  72.         pipe_stream.WaitForConnection();
  73.         try {
  74.             if(pipe_dir == System.IO.Pipes.PipeDirection.In) {
  75.                 pipe_stream.CopyTo(System.Console.OpenStandardOutput());
  76.             }
  77.             if(pipe_dir == System.IO.Pipes.PipeDirection.Out) {
  78.                 System.Console.OpenStandardInput().CopyTo(pipe_stream);
  79.             }
  80.         } catch (System.IO.IOException e) {
  81.             System.Console.WriteLine("error: {0}", e.Message);
  82.            
  83.             return 1;
  84.         }
  85.        
  86.         // Wait for process termination
  87.         while(!proc.HasExited) {
  88.             proc.WaitForExit();
  89.         }
  90.        
  91.         // Return correct exit code
  92.         return proc.ExitCode;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement