diff --git a/ShareX.HelpersLib/NativeMessagingHost.cs b/ShareX.HelpersLib/NativeMessagingHost.cs new file mode 100644 index 000000000..4304d5fdb --- /dev/null +++ b/ShareX.HelpersLib/NativeMessagingHost.cs @@ -0,0 +1,71 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2023 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using System; +using System.IO; +using System.Text; + +namespace ShareX.HelpersLib +{ + public class NativeMessagingHost + { + public string Read() + { + string input = null; + + Stream inputStream = Console.OpenStandardInput(); + + byte[] bytesLength = new byte[4]; + inputStream.Read(bytesLength, 0, bytesLength.Length); + int inputLength = BitConverter.ToInt32(bytesLength, 0); + + if (inputLength > 0) + { + byte[] bytesInput = new byte[inputLength]; + inputStream.Read(bytesInput, 0, bytesInput.Length); + input = Encoding.UTF8.GetString(bytesInput); + } + + return input; + } + + public void Write(string data) + { + Stream outputStream = Console.OpenStandardOutput(); + + byte[] bytesData = Encoding.UTF8.GetBytes(data); + byte[] bytesLength = BitConverter.GetBytes(bytesData.Length); + + outputStream.Write(bytesLength, 0, bytesLength.Length); + + if (bytesData.Length > 0) + { + outputStream.Write(bytesData, 0, bytesData.Length); + } + + outputStream.Flush(); + } + } +} \ No newline at end of file diff --git a/ShareX.HelpersLib/ShareX.HelpersLib.csproj b/ShareX.HelpersLib/ShareX.HelpersLib.csproj index 0a180933e..7deee01bb 100644 --- a/ShareX.HelpersLib/ShareX.HelpersLib.csproj +++ b/ShareX.HelpersLib/ShareX.HelpersLib.csproj @@ -169,6 +169,7 @@ + diff --git a/ShareX.NativeMessagingHost/Program.cs b/ShareX.NativeMessagingHost/Program.cs index caa6efd9d..4c3caf546 100644 --- a/ShareX.NativeMessagingHost/Program.cs +++ b/ShareX.NativeMessagingHost/Program.cs @@ -39,10 +39,13 @@ private static void Main(string[] args) { try { - string input = GetNativeMessagingInput(); + HelpersLib.NativeMessagingHost host = new HelpersLib.NativeMessagingHost(); + string input = host.Read(); if (!string.IsNullOrEmpty(input)) { + host.Write(input); + string filePath = FileHelpers.GetAbsolutePath("ShareX.exe"); string tempFilePath = FileHelpers.GetTempFilePath("json"); File.WriteAllText(tempFilePath, input, Encoding.UTF8); @@ -61,18 +64,5 @@ private static void Main(string[] args) "ShareX NativeMessagingHost", MessageBoxButtons.OK, MessageBoxIcon.Information); } } - - private static string GetNativeMessagingInput() - { - Stream inputStream = Console.OpenStandardInput(); - - byte[] bytesLength = new byte[4]; - inputStream.Read(bytesLength, 0, bytesLength.Length); - int inputLength = BitConverter.ToInt32(bytesLength, 0); - - byte[] bytesInput = new byte[inputLength]; - inputStream.Read(bytesInput, 0, bytesInput.Length); - return Encoding.UTF8.GetString(bytesInput); - } } } \ No newline at end of file