Compare commits

..

4 Commits

3 changed files with 26 additions and 11 deletions

View File

@@ -15,7 +15,7 @@ namespace MontoyaTech.Process.Net.Example
RedirectStandardInput = true, RedirectStandardInput = true,
}; };
var process = WinProcess.Create(startInfo, creationFlags: WinProcess.CreationFlags.CREATE_NEW_CONSOLE); var process = WinProcess.Start(startInfo, creationFlags: WinProcess.CreationFlags.CREATE_NEW_CONSOLE);
process.StandardInput.WriteLine("This is a test"); process.StandardInput.WriteLine("This is a test");
process.StandardInput.WriteLine("Does this work?"); process.StandardInput.WriteLine("Does this work?");

View File

@@ -15,7 +15,7 @@
<PackageTags>MontoyaTech;Process.Net</PackageTags> <PackageTags>MontoyaTech;Process.Net</PackageTags>
<AssemblyName>MontoyaTech.Process.Net</AssemblyName> <AssemblyName>MontoyaTech.Process.Net</AssemblyName>
<RootNamespace>MontoyaTech.Process.Net</RootNamespace> <RootNamespace>MontoyaTech.Process.Net</RootNamespace>
<Version>1.0.0</Version> <Version>1.0.3</Version>
<Company>MontoyaTech</Company> <Company>MontoyaTech</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>

View File

@@ -49,7 +49,7 @@ namespace MontoyaTech.Process.Net
/// <summary> /// <summary>
/// Indicates that any windows created by the process cannot be pinned on the taskbar. /// Indicates that any windows created by the process cannot be pinned on the taskbar.
// This flag must be combined with STARTF_TITLEISAPPID. /// This flag must be combined with STARTF_TITLEISAPPID.
/// </summary> /// </summary>
STARTF_PREVENTPINNING = 0x00002000, STARTF_PREVENTPINNING = 0x00002000,
@@ -406,14 +406,30 @@ namespace MontoyaTech.Process.Net
/// <param name="creationFlags">If supplied this overrides all the creation flags for the process. Default is null.</param> /// <param name="creationFlags">If supplied this overrides all the creation flags for the process. Default is null.</param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="Exception"></exception> /// <exception cref="Exception"></exception>
public unsafe static System.Diagnostics.Process Create(System.Diagnostics.ProcessStartInfo startInfo, StartupFlags? startupFlags = null, CreationFlags? creationFlags = null) public unsafe static System.Diagnostics.Process Start(System.Diagnostics.ProcessStartInfo startInfo, StartupFlags? startupFlags = null, CreationFlags? creationFlags = null)
{ {
if (startInfo == null) if (startInfo == null)
throw new ArgumentNullException(nameof(startInfo)); throw new ArgumentNullException(nameof(startInfo));
var process = new System.Diagnostics.Process(); var process = new System.Diagnostics.Process();
process.StartInfo = startInfo; process.StartInfo = startInfo;
Start(process, startupFlags, creationFlags);
return process;
}
public unsafe static void Start(System.Diagnostics.Process process, StartupFlags? startupFlags = null, CreationFlags? creationFlags = null)
{
var startInfo = process.StartInfo;
if (startInfo == null)
throw new InvalidOperationException("Process.StartInfo cannot be null.");
if (string.IsNullOrEmpty(startInfo.FileName))
throw new InvalidOperationException("FileName cannot be null or empty.");
var processType = process.GetType(); var processType = process.GetType();
var commandLine = new StringBuilder(); var commandLine = new StringBuilder();
@@ -525,10 +541,11 @@ namespace MontoyaTech.Process.Net
childErrorHandle?.Dispose(); childErrorHandle?.Dispose();
} }
//Unsafely setup the process info. //Invoke SetProcessHandle
processType.GetField("_processId", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(process, (int)processInfo.dwProcessId); processType.GetMethod("SetProcessHandle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(process, new object[] { processHandle });
processType.GetField("_haveProcessId", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(process, true);
processType.GetField("_processHandle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(process, processHandle); //Invoke SetProcessId
processType.GetMethod("SetProcessId", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(process, new object[] { (int)processInfo.dwProcessId });
//Set the standard input writer if needed //Set the standard input writer if needed
if (startInfo.RedirectStandardInput) if (startInfo.RedirectStandardInput)
@@ -551,11 +568,9 @@ namespace MontoyaTech.Process.Net
if (startInfo.RedirectStandardError) if (startInfo.RedirectStandardError)
{ {
var reader = new StreamReader(new FileStream(parentErrorHandle, FileAccess.Read, 4096, false), startInfo.StandardErrorEncoding ?? GetEncoding(GetConsoleOutputCP()), true, 4096); var reader = new StreamReader(new FileStream(parentErrorHandle, FileAccess.Read, 4096, false), startInfo.StandardErrorEncoding ?? GetEncoding(GetConsoleOutputCP()), true, 4096);
processType.GetField("_standardError", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(process, reader); processType.GetField("_standardError", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(process, reader);
} }
return process;
} }
} }
} }