How would I go about starting a new process without it being the child of the calling process.
Example:
Main Program (Caller.exe)
process.start("file.exe")
Image:
If the spawning process (parent) ends before the spawned process (child) does, then the parent-child chain is broken. To make use of this, you'd have to use an intermediate stub-process like so:
Caller.exe → Stub.exe → File.exe.
Here Stub.exe is simple launcher program that ends just after starting File.exe.
If you start a process, then you'll be its parent.
Maybe you could try to start your process from cmd.exe instead, so cmd.exe will be the parent.
Process proc = Process.Start(new ProcessStartInfo { Arguments = "/C explorer", FileName = "cmd", WindowStyle = ProcessWindowStyle.Hidden });
I have been trying to start a updater process which deletes the files of the calling process and replaces them with new ones. By setting UseShellExecute = true, I was able to circumvent the spawned process from exiting when the calling process exited.
This is inside a .Net Core 3.0 application using WPF.
var startInfo = new ProcessStartInfo("Updater.exe");
startInfo.UseShellExecute = true;
Process.Start(startInfo);
Environment.Exit(0);
This runs new process without parent:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = #"cmd";
psi.Arguments = "/C start notepad.exe";
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(psi);
The documentation of Process.Start(string fileName) says
a new process that’s started alongside already running instances
of the same process will be independent
and it says
Starting a process by specifying its file name is similar to
typing the information in the Run dialog box of the Windows Start menu
which to me seems consistent with independent processes.
So according to the documentation, Process.Start should do what you desire.
System.Diagnostics.Process.Start() method calls kernel32!CreateProcess() under the hood. When creating a process with kernel32!CreateProcess() you can specify a different parent by using a process attribute. Here is a function written in C++ that does just that - although I'm not sure how .Net supports such features.
bool CreateProcessWithParent(DWORD parentId, PWSTR commandline) {
auto hProcess = ::OpenProcess(PROCESS_CREATE_PROCESS, FALSE, parentId);
if (!hProcess)
return false;
SIZE_T size;
//
// call InitializeProcThreadAttributeList twice
// first, get required size
//
::InitializeProcThreadAttributeList(nullptr, 1, 0, &size);
//
// now allocate a buffer with the required size and call again
//
auto buffer = std::make_unique<BYTE[]>(size);
auto attributes = reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(buffer.get());
::InitializeProcThreadAttributeList(attributes, 1, 0, &size);
//
// add the parent attribute
//
::UpdateProcThreadAttribute(attributes, 0,
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
&hProcess, sizeof(hProcess), nullptr, nullptr);
STARTUPINFOEX si = { sizeof(si) };
//
// set the attribute list
//
si.lpAttributeList = attributes;
PROCESS_INFORMATION pi;
//
// create the process
//
BOOL created = ::CreateProcess(nullptr, commandline, nullptr, nullptr,
FALSE, EXTENDED_STARTUPINFO_PRESENT, nullptr, nullptr,
(STARTUPINFO*)&si, &pi);
//
// cleanup
//
::CloseHandle(hProcess);
::DeleteProcThreadAttributeList(attributes);
return created;
}
Source code taken from https://scorpiosoftware.net/2021/01/10/parent-process-vs-creator-process/
Here is the code that I'm now using. I thought that it may be useful to someone. It accepts one argument. The argument is a base64 encoded string that decodes to the path of the file that you would like to run.
Module Module1
Sub Main()
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
If CommandLineArgs.Count = 1 Then
Try
Dim path As String = FromBase64(CommandLineArgs(0))
Diagnostics.Process.Start(path)
Catch
End Try
End
End If
End Sub
Function FromBase64(ByVal base64 As String) As String
Dim b As Byte() = Convert.FromBase64String(base64)
Return System.Text.Encoding.UTF8.GetString(b)
End Function
End Module
Related
See as an example I open CMD and navigate to the folder I want to get the data from then I use it to open the app with arugents with standard input(all synchronously) the code so far
public static Process Start(bool DoNotShowWindow = false)
{
ProcessStartInfo cmdStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = DoNotShowWindow
};
Process cmdProcess = new Process
{
StartInfo = cmdStartInfo,
EnableRaisingEvents = true
};
return cmdProcess;
}
//in other method
Process cli = InteractWithConsoleApp.Start();
cli.Start();
cli.StandardInput.WriteLine("cd /");
cli.StandardInput.WriteLine("cd " + path);
cli.StandardInput.WriteLine("fantasygold-cli getbalance abc");
Thread.Sleep(5000);
Problem
Now when I use StandardOutput.Readline, it starts from the beginning and returns me everything like first two lines of copyright,empty lines and even the input which in my case, after waiting 5 secs for the result I want to read line or to the end depending the input from where I had inputted.
possible solution
One solution I found was to change the position but it turned out it doesn't support it and even copying to another stream reader doesn't works(the position is not by line).
Well I can use filters like check a double or for an address starts with F and has a length of 36. The problem comes when I want to get the whole JSON say for like the past transactions, for which I think using filters like '{' and then check for '}'Caveat in this would be bad code, which I don't want.
TLDR
So, what could be the solution to my problem here :)
I found the answer, to open the file in subdirectory just use this cli.StartInfo.FileName = Directory.GetCurrentDirectory() + #"\SubfolderName\fantasygold-cli";
and the arguements like getbalance as cli.StartInfo.Arguments = "getbalance amunim"
I'm trying to hide command window when process starts. I launch McAfee virus scan using this ProcessStartInfo config:
ProcessStartInfo procInfo = new ProcessStartInfo();
procInfo.FileName = args[0];
...
procInfo.RedirectStandardOutput = true;
procInfo.WindowStyle = ProcessWindowStyle.Hidden;
procInfo.CreateNoWindow = false;
procInfo.UseShellExecute = false;
process.StartInfo = procInfo;
Then, I try:
process.Start();
process.CloseMainWindow();
And also:
process.Start();
while (process.MainWindowHandle == IntPtr.Zero)
{
process.Refresh();
}
var handle = process.MainWindowHandle;
ShowWindow(handle, 0) //ShowWindow from user32.dll
But in both cases command window appears and before it is displayed completely, it desappears (just like maximize window and immediately minimize it). I have also played with all possible ProcessStartInfo properties combinations. I need no window is displayed in any moment. How could I achieve that? Thanks in advance!
For the Code Blocks as shown in your question, the behavior is normal.
Let's have a look at them...
//[...]
procInfo.CreateNoWindow = false; //That's not what you meant
procInfo.UseShellExecute = false;
process.Start(); //Will show up the console window
process.CloseMainWindow(); //Will close the main window (probably also the whole application)
Between process.Start(); and process.CloseMainWindow(); is a short time frame, where the application is visible.
Like described by #BugFinder, it should be enough to edit the CreateNoWindow Value from false to true.
Check if that works.
Your second try is getting you further away:
process.Start();
while (process.MainWindowHandle == IntPtr.Zero)
{
process.Refresh(); //Let's do this as long as there is no window...
}
var handle = process.MainWindowHandle; //This occurs, when there is *already* a window.
ShowWindow(handle, 0) //ShowWindow from user32.dll
The idea of using Shell Functions is basically not bad, but this code is doing the same like the code above (and is also unmanaged).
Let's try a different way and take the C# Sample from PInvoke.Net: (not more managed than ShowWindow()...)
public static void StartupNotepad()
{
const uint NORMAL_PRIORITY_CLASS = 0x0020;
const uint STARTF_USESHOWWINDOW = 0x1; //IMPORTANT for you
bool retValue;
string Application = Environment.GetEnvironmentVariable("windir") + #"\Notepad.exe";
string CommandLine = #" c:\boot.ini"; //Funny
PROCESS_INFORMATION pInfo = new PROCESS_INFORMATION();
STARTUPINFO sInfo = new STARTUPINFO();
sInfo.wShowWindow = 0; //IMPORTANT for you - See http://pinvoke.net/default.aspx/Enums/ShowWindowCommand.html
sInfo.dwFlags = STARTF_USESHOWWINDOW; //IMPORTANT for you
SECURITY_ATTRIBUTES pSec = new SECURITY_ATTRIBUTES();
SECURITY_ATTRIBUTES tSec = new SECURITY_ATTRIBUTES();
pSec.nLength = Marshal.SizeOf(pSec);
tSec.nLength = Marshal.SizeOf(tSec);
retValue = CreateProcess(Application,CommandLine,
ref pSec,ref tSec,false,NORMAL_PRIORITY_CLASS,
IntPtr.Zero,null,ref sInfo,out pInfo);
Console.WriteLine("Process ID (PID): " + pInfo.dwProcessId);
Console.WriteLine("Process Handle : " + pInfo.hProcess);
}
This example demonstrates the use of CreateProcess (MSDN Link at the bottom) in a very static manner.
I suggest you to write a more dynamic function and play around with it. The PROCESS_INFORMATION structure allows you to manipulate the newly created process later in your application. If your problem still persists using the CreateProcess() Call, it's to check whether the started application does some tricky stuff on itself. And last but not least whether the problem occurs not only on your computer...
Here are some more references:
SO: WindowStyle.Hidden vs. CreateNoWindow = True
SO: How to call CreateProcess() with STARTUPINFOEX from C# and re-parent the child
SO: winapi: CreateProcess but hide the process' window?
MSDN: ProcessStartInfo.CreateNoWindow Property
MSDN: CreateProcess() Function
MSDN: STARTUPINFO Structure
EDIT:
Probably a workaround, but if your 3rd party App is designed to show a Window and tries to ignore your requests, STARTUPINFO also has dwX and dwY, which sets the Window's position if dwFlags specifies STARTF_USEPOSITION. Same for dwXSize and dwYSize.
If all else fails, you could give it a try and pass positions and sizes, which make the proces appear invisible. Cheers.
Im trying to make C# application that uses hunpos tagger.
Runing hunpos-tag.exe requires three input arguments: model, inputFile, outputFile
In cmd it would look something like this:
hunpos-tag.exe model <inputFile >outputFile
If I just run it with model it writes something and waits for end command. When i tried using standard redirect I either get an exception (i solved this the code was off by a braceket i just get the or scenario now) or I get the results of running the tagger with just model argument. Here's the code:
string inputFilePath = path + "\\CopyFolder\\rr";
string pathToExe = path + "\\CopyFolder\\hunpos-tag.exe";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
RedirectStandardInput = true,
WorkingDirectory = Directory.GetDirectoryRoot(pathToExe),
Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout",
};
try
{
Process _proc = new Process();
_proc.StartInfo.FileName = pathToExe;
_proc.StartInfo.UseShellExecute = false;
_proc.StartInfo.RedirectStandardInput = true;
_proc.StartInfo.Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout";
//Magic goes here
_proc.Start();
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Any ideas how can I redirect input before starting my process?
It is not only required to set RedirectStandardInput to true but also you need to use the input stream to write the text you want:
_proc.StandardInput.WriteLine("The text you want to write");
There's no need for that ProcessStartInfo if you're setting the info later on. Just get rid of that. And it seems you are already doing what you want. Just creating the process object doesn't start the process, Process.Start does. Just make a new StreamWriter and pass it Process.StandardInput (I think that's right, it may be something else)
How would I go about starting a new process without it being the child of the calling process.
Example:
Main Program (Caller.exe)
process.start("file.exe")
Image:
If the spawning process (parent) ends before the spawned process (child) does, then the parent-child chain is broken. To make use of this, you'd have to use an intermediate stub-process like so:
Caller.exe → Stub.exe → File.exe.
Here Stub.exe is simple launcher program that ends just after starting File.exe.
If you start a process, then you'll be its parent.
Maybe you could try to start your process from cmd.exe instead, so cmd.exe will be the parent.
Process proc = Process.Start(new ProcessStartInfo { Arguments = "/C explorer", FileName = "cmd", WindowStyle = ProcessWindowStyle.Hidden });
I have been trying to start a updater process which deletes the files of the calling process and replaces them with new ones. By setting UseShellExecute = true, I was able to circumvent the spawned process from exiting when the calling process exited.
This is inside a .Net Core 3.0 application using WPF.
var startInfo = new ProcessStartInfo("Updater.exe");
startInfo.UseShellExecute = true;
Process.Start(startInfo);
Environment.Exit(0);
This runs new process without parent:
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = #"cmd";
psi.Arguments = "/C start notepad.exe";
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(psi);
The documentation of Process.Start(string fileName) says
a new process that’s started alongside already running instances
of the same process will be independent
and it says
Starting a process by specifying its file name is similar to
typing the information in the Run dialog box of the Windows Start menu
which to me seems consistent with independent processes.
So according to the documentation, Process.Start should do what you desire.
System.Diagnostics.Process.Start() method calls kernel32!CreateProcess() under the hood. When creating a process with kernel32!CreateProcess() you can specify a different parent by using a process attribute. Here is a function written in C++ that does just that - although I'm not sure how .Net supports such features.
bool CreateProcessWithParent(DWORD parentId, PWSTR commandline) {
auto hProcess = ::OpenProcess(PROCESS_CREATE_PROCESS, FALSE, parentId);
if (!hProcess)
return false;
SIZE_T size;
//
// call InitializeProcThreadAttributeList twice
// first, get required size
//
::InitializeProcThreadAttributeList(nullptr, 1, 0, &size);
//
// now allocate a buffer with the required size and call again
//
auto buffer = std::make_unique<BYTE[]>(size);
auto attributes = reinterpret_cast<PPROC_THREAD_ATTRIBUTE_LIST>(buffer.get());
::InitializeProcThreadAttributeList(attributes, 1, 0, &size);
//
// add the parent attribute
//
::UpdateProcThreadAttribute(attributes, 0,
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
&hProcess, sizeof(hProcess), nullptr, nullptr);
STARTUPINFOEX si = { sizeof(si) };
//
// set the attribute list
//
si.lpAttributeList = attributes;
PROCESS_INFORMATION pi;
//
// create the process
//
BOOL created = ::CreateProcess(nullptr, commandline, nullptr, nullptr,
FALSE, EXTENDED_STARTUPINFO_PRESENT, nullptr, nullptr,
(STARTUPINFO*)&si, &pi);
//
// cleanup
//
::CloseHandle(hProcess);
::DeleteProcThreadAttributeList(attributes);
return created;
}
Source code taken from https://scorpiosoftware.net/2021/01/10/parent-process-vs-creator-process/
Here is the code that I'm now using. I thought that it may be useful to someone. It accepts one argument. The argument is a base64 encoded string that decodes to the path of the file that you would like to run.
Module Module1
Sub Main()
Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
If CommandLineArgs.Count = 1 Then
Try
Dim path As String = FromBase64(CommandLineArgs(0))
Diagnostics.Process.Start(path)
Catch
End Try
End
End If
End Sub
Function FromBase64(ByVal base64 As String) As String
Dim b As Byte() = Convert.FromBase64String(base64)
Return System.Text.Encoding.UTF8.GetString(b)
End Function
End Module
When working with a command line program, via a c# class method.
How do you determine if the commandline program was successfully executed and the operation it has performed is ok or has failed?
Also how do you get the screen commandline output into the c# class method?
You can use the Process class to execute a command line command.
The following code captures the standard output to output, and assigns the processes exit code to exitCode.
using (Process p = new Process())
{
p.StartInfo.FileName = exeName;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
int exitCode = p.ExitCode;
}
Something like:
Process mycommand = new Process();
mycommand.StartInfo.FileName = "myexe.exe";
mycommand.StartInfo.Arguments = "param1";
mycommand.StartInfo.UseShellExecute = false;
mycommand.StartInfo.RedirectStandardOutput = true;
mycommand.Start();
Console.WriteLine(mycommand.StandardOutput.ReadToEnd());
mycommand.WaitForExit();
You usually determine an exe's state wether the exit code is 0, but that is arguably down to the writer of the exe
I assume you're using the Process class to call the command line app.
You can find the exit code of the process using Process.ExitCode. You can redirect its standard output by setting ProcessStartInfo.RedirectStandardOutput before starting it, and then either using Process.StandardOutput or the Process.OutputDataReceived event.
Take a look at this questionenter link description here.
The additional information you might need is process.ExitCode to see if it was sucessful. Of course, the Main method of the console app must return an exit code when it is unsuccessful, which many do not.
For this, you use the Process.Start method. You can control how the process runs with the passed in ProcessStartInfo:
var myProcess = Process.Start(new ProcessStartInfo {
FileName = "process.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
});
if (!myProcess.WaitForExit(5000)) { // give it 5 seconds to exit
myProcess.Kill();
}
if (myProcess.ExitCode != 0) {
// error!
}
var output = myProcess.StandardOutput.ReadToEnd(); // access output