C# 本机消息响应扩展

C# 本机消息响应扩展,c#,google-chrome,google-chrome-extension,chrome-native-messaging,C#,Google Chrome,Google Chrome Extension,Chrome Native Messaging,我使用这个代码 // background.js chrome.runtime.sendNativeMessage( "com.example.native", { text: "test" }, function(response) { console.log("Received " + response); }); C代码 private static void OpenStandardStreamOut(string stringData) { string ms

我使用这个代码

// background.js
chrome.runtime.sendNativeMessage( "com.example.native",
  { text: "test" },
  function(response) {
    console.log("Received " + response);
});
C代码

private 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);
}

private static List<LoginPack> OpenStandardStreamIn()
{
    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();
    }
    JObject Read=(JObject)JsonConvert.DeserializeObject<JObject>(input);
    //string dataPackStr = JsonConvert.SerializeObject(Read);
    Chrome chromeClass = new Chrome();
    List<LoginPack> lp = new List<LoginPack>();
    if (Read!=null)
        if (Read.Count != 0)
            lp = chromeClass.getInfoFromChrome(Read["text"].ToString());
    if (lp.Count == 0)
        return null;
    return lp;
}
//铬类

public class Chrome
{
    public class Data
    {
        public string key { get; set; }
        public string value { get; set; }
    }

    public List<LoginPack> getInfoFromChrome(string colName)
    {
        try
        {
            // string filename = "my_chrome_passwords.html";
            //  StreamWriter Writer = new StreamWriter(filename, false, Encoding.UTF8);
            string db_way = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
                + "/Google/Chrome/User Data/Profile 1/Login Data1";
            Console.WriteLine("DB file = " + db_way);
            string db_field = "logins";
            List<LoginPack> lp = new List<LoginPack>();
            byte[] entropy = null;
            string description;
            string ConnectionString = "data source=" + db_way + ";New=True;UseUTF16Encoding=True";
            DataTable DB = new DataTable();
            string sql = string.Format("SELECT * FROM {0} where action_url=\"{1}\" or origin_url=\"{2}\"", db_field, colName, colName);
            // System.IO.StreamWriter file1 = new System.IO.StreamWriter("c:\\test.txt");
            // file1.WriteLine(sql);
            // file1.Close();
            using (SQLiteConnection connect = new SQLiteConnection(ConnectionString))
            {
                SQLiteCommand command = new SQLiteCommand(sql, connect);
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
                adapter.Fill(DB);
                int rows = DB.Rows.Count;
                for (int i = 0; i < rows; i++)
                {
                    byte[] byteArray = (byte[])DB.Rows[i][5];
                    byte[] decrypted = DPAPI.Decrypt(byteArray, entropy, out description);
                    lp.Add(new LoginPack { userNameElement = (string)DB.Rows[i][2], userName = (string)DB.Rows[i][3], passElement = (string)DB.Rows[i][4], pass = new UTF8Encoding(true).GetString(decrypted) });
                    //System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test2.txt");
                    //file.WriteLine(lp[i].userName);
                    //file.Close();
                }
            }
            // Writer.Close();
            return lp;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            ex = ex.InnerException;
            return null;
        }
    }
}
应用程序从扩展程序提供数据,但扩展程序无法从应用程序获得任何响应 若我首先在C应用程序中使用OpenStandardStreamOut函数,那个么扩展可以从中得到响应
有什么问题吗?

我认为你的“Chrome”课程效果不好。再次检查,确保使用标准流而不是第三方流。同时删除Console.WriteLineDB file=+db\u-way;请排队再试。

我已经把你的问题读了20遍了,但仍然不确定到底发生了什么。仅供参考,当扩展打开端口时,Chrome会使用重定向的输入和输出流执行本机应用程序,因此扩展始终是通信的发起方。实际上,扩展不会在发送后从应用程序接收数据。在这种情况下,您需要显示如何从扩展接收数据。你发布的代码没有这个部分。好的,我把OpenStandardStream放在函数中了抱歉,我不明白问题出在哪里。以防万一,我重复一遍:您的本机应用程序不能作为启动器,因为它是在您的扩展调用runtime.connectNative时由Chrome启动的。此外,消息传递是异步的,但您的代码似乎期望同步响应。您可以在C中找到本机消息传递的示例,并对其进行调整。