From d20aad136bfe3144fbaf28577e1a4f73192a7cbd Mon Sep 17 00:00:00 2001 From: poke30744 Date: Mon, 17 Aug 2020 14:19:17 +0900 Subject: [PATCH 1/3] Inherit the parent's console and wait the child --- LEProc/LoaderWrapper.cs | 65 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/LEProc/LoaderWrapper.cs b/LEProc/LoaderWrapper.cs index d53bf45..b6e3e91 100644 --- a/LEProc/LoaderWrapper.cs +++ b/LEProc/LoaderWrapper.cs @@ -179,13 +179,18 @@ namespace LEProc var locLEB = Marshal.AllocHGlobal(newLEB.Length); Marshal.Copy(newLEB, 0, locLEB, newLEB.Length); + AttachConsole(ATTACH_PARENT_PROCESS); + + var startupInfo = new STARTUPINFO(); + var processInformation = new PROCESS_INFORMATION(); + var ret = LeCreateProcess(locLEB, ApplicationName, CommandLine, CurrentDirectory, DebugMode ? CREATE_SUSPENDED : CREATE_NORMAL, - IntPtr.Zero, - IntPtr.Zero, + ref startupInfo, + out processInformation, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, @@ -193,6 +198,13 @@ namespace LEProc Marshal.FreeHGlobal(locLEB); + if (ret == 0) + { + WaitForSingleObject(processInformation.hProcess, INFINITE); + CloseHandle(processInformation.hProcess); + CloseHandle(processInformation.hThread); + } + return ret; } @@ -249,19 +261,64 @@ namespace LEProc return BytesToStruct<_REG_TZI_FORMAT>(tzi); } + [StructLayout(LayoutKind.Sequential)] + public struct PROCESS_INFORMATION + { + public IntPtr hProcess; + public IntPtr hThread; + public uint dwProcessId; + public uint dwThreadId; + } + + [StructLayout(LayoutKind.Sequential)] + public struct STARTUPINFO + { + public int cb; + public String lpReserved; + public String lpDesktop; + public String lpTitle; + public uint dwX; + public uint dwY; + public uint dwXSize; + public uint dwYSize; + public uint dwXCountChars; + public uint dwYCountChars; + public uint dwFillAttribute; + public uint dwFlags; + public short wShowWindow; + public short cbReserved2; + public IntPtr lpReserved2; + public IntPtr hStdInput; + public IntPtr hStdOutput; + public IntPtr hStdError; + } + [DllImport("LoaderDll.dll", CharSet = CharSet.Unicode)] public static extern uint LeCreateProcess(IntPtr leb, [MarshalAs(UnmanagedType.LPWStr), In] string applicationName, [MarshalAs(UnmanagedType.LPWStr), In] string commandLine, [MarshalAs(UnmanagedType.LPWStr), In] string currentDirectory, uint creationFlags, - IntPtr startupInfo, - IntPtr processInformation, + ref STARTUPINFO startupInfo, + out PROCESS_INFORMATION processInformation, IntPtr processAttributes, IntPtr threadAttributes, IntPtr environment, IntPtr token); + [DllImport("kernel32.dll", SetLastError = true)] + static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 dwMilliseconds); + + [DllImport("kernel32.dll", SetLastError = true)] + static extern bool CloseHandle(IntPtr hHandle); + + const uint INFINITE = 0xFFFFFFFF; + + [DllImport("kernel32.dll", SetLastError = true)] + static extern bool AttachConsole(uint dwProcessId); + + const uint ATTACH_PARENT_PROCESS = 0x0ffffffff; + [StructLayout(LayoutKind.Sequential)] internal struct LEB { From e1379908c36b1dc674df730741c5e125a9c19918 Mon Sep 17 00:00:00 2001 From: poke30744 Date: Mon, 17 Aug 2020 15:59:35 +0900 Subject: [PATCH 2/3] Keep quotes in args to support args like: -path "s pace" --- LEProc/Program.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/LEProc/Program.cs b/LEProc/Program.cs index c84163e..e990410 100644 --- a/LEProc/Program.cs +++ b/LEProc/Program.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.IO; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; using System.Windows.Forms; using LECommonLibrary; @@ -88,7 +89,14 @@ namespace LEProc try { - Args = args; + var argWithQuotesList = Regex.Matches(Environment.CommandLine, @"[\""].+?[\""]|[^ ]+") + .Cast() + .ToArray(); + Args = new string[argWithQuotesList.Length - 1]; + for (var i = 1; i < argWithQuotesList.Length; i += 1) + { + Args[i - 1] = argWithQuotesList[i].ToString(); + } switch (Args[0]) { @@ -112,9 +120,9 @@ namespace LEProc break; default: - if (File.Exists(Args[0])) + if (File.Exists(Args[0].Trim('"'))) { - RunWithDefaultProfile(Args[0]); + RunWithDefaultProfile(Args[0].Trim('"')); } break; } From 2ca1b5b7ebb8b5fe06ec5c234a8c46a0cf4ab1a9 Mon Sep 17 00:00:00 2001 From: poke30744 Date: Thu, 10 Sep 2020 11:16:12 +0900 Subject: [PATCH 3/3] To add comments --- LEProc/Program.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/LEProc/Program.cs b/LEProc/Program.cs index e990410..8b21d3b 100644 --- a/LEProc/Program.cs +++ b/LEProc/Program.cs @@ -89,6 +89,9 @@ namespace LEProc try { + // To keep original quotes in the command line. If we directly use args[] to make the command line, + // {_cmd1.exe / c _cmd2.exe "path with space"} will become {_cmd1.exe / c _cmd2.exe path with space} + // and will cause issues. var argWithQuotesList = Regex.Matches(Environment.CommandLine, @"[\""].+?[\""]|[^ ]+") .Cast() .ToArray();