Moved helper methods to HelpersLib

This commit is contained in:
Jaex 2017-01-26 21:47:58 +03:00
parent c935af515d
commit 65f42de600
4 changed files with 21 additions and 23 deletions

View file

@ -27,7 +27,6 @@ You should have received a copy of the GNU General Public License
using ShareX.HelpersLib; using ShareX.HelpersLib;
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
@ -61,20 +60,20 @@ private static void Run()
if (!string.IsNullOrEmpty(chromeInput.URL)) if (!string.IsNullOrEmpty(chromeInput.URL))
{ {
argument = EscapeText(chromeInput.URL); argument = Helpers.EscapeCLIText(chromeInput.URL);
} }
else if (!string.IsNullOrEmpty(chromeInput.Text)) else if (!string.IsNullOrEmpty(chromeInput.Text))
{ {
string filepath = Helpers.GetTempPath("txt"); string filepath = Helpers.GetTempPath("txt");
File.WriteAllText(filepath, chromeInput.Text, Encoding.UTF8); File.WriteAllText(filepath, chromeInput.Text, Encoding.UTF8);
argument = EscapeText(filepath); argument = Helpers.EscapeCLIText(filepath);
} }
if (!string.IsNullOrEmpty(argument)) if (!string.IsNullOrEmpty(argument))
{ {
string path = Helpers.GetAbsolutePath("ShareX.exe"); 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); inputStream.Read(bytesInput, 0, bytesInput.Length);
return Encoding.UTF8.GetString(bytesInput); 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);
}
} }
} }

View file

@ -1159,5 +1159,10 @@ public static Cursor CreateCursor(byte[] data)
return new Cursor(ms); return new Cursor(ms);
} }
} }
public static string EscapeCLIText(string text)
{
return string.Format("\"{0}\"", text.Replace("\\", "\\\\").Replace("\"", "\\\""));
}
} }
} }

View file

@ -284,7 +284,7 @@ public static partial class NativeMethods
#region kernel32.dll #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, 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); bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

View file

@ -493,5 +493,17 @@ public static void OpenFolderAndSelectFile(string filePath)
ILFree(pidl); 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);
}
} }
} }