在Native Messaging Host和C#app之间进行通信

时间:2015-07-01 23:47:20

标签: c# google-chrome-extension chrome-native-messaging

我有一个chrome扩展,可以与我在C#中创建的Native Messaging Host进行通信。

但是,如何在Native Messaging Host和C#app之间进行通信。我认为它将通过Native Messaging Host输入/输出流,但我还没有找到如何做到这一点。

1 个答案:

答案 0 :(得分:1)

我找不到使用Process.StandardInput的方法,因为chrome启动了Native Messaging Host。

相反,我使用了WCF。这是代码:

    private static object SyncRoot = new object();
    static string returnValue = null;

    [ServiceContract]
    public interface IGetChromeString
    {
        [OperationContract]
        string GetString(string value);
    }

    public class CGetChromeString : IGetChromeString
    {
        public string GetString(string value)
        {
            OpenStandardStreamOut(value);
            // Wait until OpenStandardStreamIn() sets returnValue in the Main Thread
            lock (SyncRoot)
            {
                Monitor.Wait(SyncRoot);
            }
            return returnValue;
        }
    }

    static void Main(string[] args)
    {
        using (ServiceHost host = new ServiceHost(typeof(CGetChromeString), new Uri[] { new Uri("net.pipe://localhost") }))
        {
            host.AddServiceEndpoint(typeof(IGetChromeString), new NetNamedPipeBinding(), "PipeReverse");

            host.Open();
            while (returnValue != "")
            {
                returnValue = OpenStandardStreamIn();
                // returnValue has been set, GetUrl can resume.
                lock (SyncRoot)
                {
                    Monitor.Pulse(SyncRoot);
                }
            }

            host.Close();
        }
    }

    static string OpenStandardStreamIn()
    {
        Stream stdin = Console.OpenStandardInput();
        byte[] bytes = new byte[4];
        stdin.Read(bytes, 0, 4);
        int length = 0;

        length = System.BitConverter.ToInt32(bytes, 0);

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

    static void OpenStandardStreamOut(string stringData)
    {
        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));
        Console.Write(msgdata);
    }