0

I'm trying to pipe information between excel and another console for learning.

I have a simple pipe example that works between 2 separate C# console programs. I'm now trying to get it to work with one end being Excel, and another being just a regular console application. I am fairly new to Named Pipes.

For the set up in Excel, a form will appear when a ribbon button is clicked and then a console will appear when a button on that form is clicked. When this console appears, there is no output on the console which is problem one. I've looked at process.StartInfo.RedirectStandardOutput = true; but I don't think that is the answer since its not working. I feel like this is an easy fix.

I haven't found any documentation on trying to pipe between applications like excel. I've only found simple examples on piping between 2 simple console programs

The second issue I'm having is the error: pipe is broken after the first message is sent.

Exception thrown: 'System.IO.IOException' in System.Core.dll
An unhandled exception of type 'System.IO.IOException' occurred in System.Core.dll
Additional information: Pipe is broken.

Any and all help/direction is appreciated.

ExcelForm

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnPipeA_Click(object sender, EventArgs e)
        {
            using (Process process = new Process())
            {
                process.StartInfo.FileName = "cmd.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;       
                process.Start();

                //Client
                var client = new NamedPipeClientStream("PipesOfPiece");
                if (client.IsConnected == false)
                {
                    Console.WriteLine("Waiting to connect to Server...");
                }
                client.Connect();
                StreamReader reader = new StreamReader(client);
                StreamWriter writer = new StreamWriter(client);

                Console.Write("Found a sever to connect to. Send a greeting to the server: ");


                while (true)
                {
                    string input = Console.ReadLine();
                    if (String.IsNullOrEmpty(input)) break;
                    writer.WriteLine(input);
                    writer.Flush();
                    Console.WriteLine("PipeB said: " + reader.ReadLine());

                }
            }
        }

            private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }

ConsoleAppPipe

class Program
    {
        static void Main(string[] args)
        {
            StartServer();
        }
        static void StartServer()
        {
            /*Task.Factory.StartNew(() =>
            {*/
            var server = new NamedPipeServerStream("PipesOfPiece");

            if (server.IsConnected == false)
            {
                Console.WriteLine("Currently waiting for a client to connect...");
            }

            server.WaitForConnection();
            
            StreamReader reader = new StreamReader(server);
            StreamWriter writer = new StreamWriter(server);

            Console.Write("A client has connected, awaiting greeting from client... \n");


            while (true)
            {
                var line = reader.ReadLine();
                Console.WriteLine("PipeA said: " + line);
                writer.WriteLine(Console.ReadLine());
                writer.Flush();
            }
        }
    }

This is what shows when I run the the Excel code: enter image description here

This is what shows when I run the console application code: enter image description here

-----------------------Update--------------------

button update

private void btnPipeA_Click(object sender, EventArgs e)
        {
            Program.Main();
        }

program

namespace TestNamedPIpeA
{
    public class Program
    {
        
        public static void Main(/*string[] args*/)
        {
            //Client
            var client = new NamedPipeClientStream("PipesOfPiece");
            if (client.IsConnected == false)
            {
                Console.WriteLine("Waiting to connect to Server...");
            }
            client.Connect();
            StreamReader reader = new StreamReader(client);
            StreamWriter writer = new StreamWriter(client);

            Console.Write("Found a sever to connect to. Send a greeting to the server: ");


            while (true)
            {
                string input = Console.ReadLine();
                if (String.IsNullOrEmpty(input)) break;
                writer.WriteLine(input);
                writer.Flush();
                Console.WriteLine("PipeB said: " + reader.ReadLine());

            }
        }
    }
}
Cflux
  • 1,423
  • 3
  • 19
  • 39
  • What are you expecting to appear on the console, when Excel starts it? – Ann L. Jul 22 '20 at 18:07
  • @AnnL. My b. I forgot a piece of code in there which shows a greeting once the server has come online. I've updated my code. to answer your question. I am expecting a greeting but only a regular console appears showing the typical `C\:Windows>` prompt – Cflux Jul 22 '20 at 18:21
  • @AnnL. I also just uploaded photos to show what happens when I run the excel code vs when when I run the console application code. – Cflux Jul 22 '20 at 18:26
  • I may be missing something, because I haven't worked much with .NET interactions between processes ... but, first, `cmd.exe` is not a .NET process, as far as I know, so communicating with it via .NET would be ... challenging, and it also isn't where the results of `Console.WriteLine` are going to appear: it just looks the same. – Ann L. Jul 22 '20 at 18:47
  • Can you write a custom .NET program that you start from the form, instead of using `Cmd.exe`? – Ann L. Jul 22 '20 at 18:48
  • @AnnL. I updated my code with the new "update" portion at the button of the question with what I think you mean. I know the `program.cs` code works by itself. when I run it as its own console app. it is able to connect to pipe information between itself and another console application. However, When I bring it into the excel add-in and click the button, the form button just stays highlighted but no console shows up. I end up having to stop the debug process since Excel is now trying to run the Program.Main() code with no way of exiting. – Cflux Jul 22 '20 at 19:09

0 Answers0