From 65f42de6007abd0727a92c75d13380c2c45e775f Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 26 Jan 2017 21:47:58 +0300 Subject: [PATCH] Moved helper methods to HelpersLib --- ShareX.Chrome/Program.cs | 25 +++---------------- ShareX.HelpersLib/Helpers/Helpers.cs | 5 ++++ ShareX.HelpersLib/Native/NativeMethods.cs | 2 +- .../Native/NativeMethods_Helpers.cs | 12 +++++++++ 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/ShareX.Chrome/Program.cs b/ShareX.Chrome/Program.cs index 5194de0a7..bee13c896 100644 --- a/ShareX.Chrome/Program.cs +++ b/ShareX.Chrome/Program.cs @@ -27,7 +27,6 @@ You should have received a copy of the GNU General Public License using ShareX.HelpersLib; using System; using System.IO; -using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; @@ -61,20 +60,20 @@ private static void Run() if (!string.IsNullOrEmpty(chromeInput.URL)) { - argument = EscapeText(chromeInput.URL); + argument = Helpers.EscapeCLIText(chromeInput.URL); } else if (!string.IsNullOrEmpty(chromeInput.Text)) { string filepath = Helpers.GetTempPath("txt"); File.WriteAllText(filepath, chromeInput.Text, Encoding.UTF8); - argument = EscapeText(filepath); + argument = Helpers.EscapeCLIText(filepath); } if (!string.IsNullOrEmpty(argument)) { string path = Helpers.GetAbsolutePath("ShareX.exe"); - CreateProcess(path, argument); + NativeMethods.CreateProcess(path, argument, CreateProcessFlags.CREATE_BREAKAWAY_FROM_JOB); } } } @@ -92,23 +91,5 @@ private static string GetInput() inputStream.Read(bytesInput, 0, bytesInput.Length); return Encoding.UTF8.GetString(bytesInput); } - - private static string EscapeText(string text) - { - return string.Format("\"{0}\"", text.Replace("\\", "\\\\").Replace("\"", "\\\"")); - } - - private static bool CreateProcess(string path, string arguments) - { - PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION(); - STARTUPINFO sInfo = new STARTUPINFO(); - SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES(); - SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES(); - pSec.nLength = Marshal.SizeOf(pSec); - tSec.nLength = Marshal.SizeOf(tSec); - - return NativeMethods.CreateProcess(null, $"\"{path}\" {arguments}", ref pSec, ref tSec, false, (uint)CreateProcessFlags.CREATE_BREAKAWAY_FROM_JOB, - IntPtr.Zero, null, ref sInfo, out pInfo); - } } } \ No newline at end of file diff --git a/ShareX.HelpersLib/Helpers/Helpers.cs b/ShareX.HelpersLib/Helpers/Helpers.cs index c5a1d95a8..45a747b28 100644 --- a/ShareX.HelpersLib/Helpers/Helpers.cs +++ b/ShareX.HelpersLib/Helpers/Helpers.cs @@ -1159,5 +1159,10 @@ public static Cursor CreateCursor(byte[] data) return new Cursor(ms); } } + + public static string EscapeCLIText(string text) + { + return string.Format("\"{0}\"", text.Replace("\\", "\\\\").Replace("\"", "\\\"")); + } } } \ No newline at end of file diff --git a/ShareX.HelpersLib/Native/NativeMethods.cs b/ShareX.HelpersLib/Native/NativeMethods.cs index 892eae1dc..7ba381717 100644 --- a/ShareX.HelpersLib/Native/NativeMethods.cs +++ b/ShareX.HelpersLib/Native/NativeMethods.cs @@ -284,7 +284,7 @@ public static partial class NativeMethods #region kernel32.dll - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] + [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, ref SECURITY_ATTRIBUTES lpProcessAttributes, ref SECURITY_ATTRIBUTES lpThreadAttributes, bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation); diff --git a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs index f2a6d1844..7f135d474 100644 --- a/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs +++ b/ShareX.HelpersLib/Native/NativeMethods_Helpers.cs @@ -493,5 +493,17 @@ public static void OpenFolderAndSelectFile(string filePath) ILFree(pidl); } } + + public static bool CreateProcess(string path, string arguments, CreateProcessFlags flags = CreateProcessFlags.NORMAL_PRIORITY_CLASS) + { + PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION(); + STARTUPINFO sInfo = new STARTUPINFO(); + SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES(); + SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES(); + pSec.nLength = Marshal.SizeOf(pSec); + tSec.nLength = Marshal.SizeOf(tSec); + + return CreateProcess(null, $"\"{path}\" {arguments}", ref pSec, ref tSec, false, (uint)flags, IntPtr.Zero, null, ref sInfo, out pInfo); + } } } \ No newline at end of file