'Chrome Native messaging with C# Application

I am trying to communicate with a C# app through chrome native messaging. I am totally new to C#. So I'll create a sample app through following code. How do I add this DisplayMessage function to display the incoming message in the UI?. I have used forms for this UI.

nativeMessage.exe

using System;
using System.IO;
using System.Windows.Forms;

namespace NativeMsgApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string message = txt_inputBox.Text;
            label1.Text = "Send Message "+ message;
            OpenStandardStreamOut(message);

           
        }

        private static string OpenStandardStreamIn()
        {
            //// We need to read first 4 bytes for length information
            Stream stdin = Console.OpenStandardInput();
            int length = 0;
            byte[] bytes = new byte[4];
            stdin.Read(bytes, 0, 4);
            length = System.BitConverter.ToInt32(bytes, 0);

            string input = "";
            for (int i = 0; i < length; i++)
            {
                input += (char)stdin.ReadByte();
            }

            return input;
        }

        private static void OpenStandardStreamOut(string stringData)
        {
            //// We need to send the 4 btyes of length information
            string msgdata = "{\"text\":\"" + stringData + "\"}";
            int DataLength = msgdata.Length;
            Stream stdout = Console.OpenStandardOutput();
            stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
            stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
            stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
            stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
            //Available total length : 4,294,967,295 ( FF FF FF FF )
            Console.Write(msgdata);

        }

        private static void DisplayMessage()
        {
            while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
            {
                OpenStandardStreamOut("Received to Native App: " + OpenStandardStreamIn());
                OpenStandardStreamOut("Recieved: " + OpenStandardStreamIn());
            }
        }
    }
}

The UI of the App

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source