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 { diff --git a/LEProc/Program.cs b/LEProc/Program.cs index c84163e..8b21d3b 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,17 @@ namespace LEProc try { - Args = args; + // 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(); + 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 +123,9 @@ namespace LEProc break; default: - if (File.Exists(Args[0])) + if (File.Exists(Args[0].Trim('"'))) { - RunWithDefaultProfile(Args[0]); + RunWithDefaultProfile(Args[0].Trim('"')); } break; }