google chrome extension - Communicate between Native Messaging Host and C# app -
i have chrome extension can communicate native messaging host created in c#.
however, how can communicate between native messaging host , c# app. think via native messaging host input/output streams, haven't been able find how this.
i couldn't find way of using process.standardinput because chrome launches native messaging host.
instead used wcf. here code:
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 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 = ""; (int = 0; < 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); }
Comments
Post a Comment