arun ragam

arun ragam

  • NA
  • 64
  • 2.4k

Read process Output Message from process within the process?

Jun 7 2016 2:45 AM

I am running an exe through commandline and getting following output.

C:\Users\sysadmin>C:\Users\sysadmin\Desktop\New_folder\Setup\PatchInstaller.exe --mode=silent

C:\Users\sysadmin Begin Setup UI mode: Silent Error : Another instance running, Only a single instance can be run at a time. Exit Code: 11

i am running this through System.daignostics.process.

My issue is PatchInstaller.exe calling another process and the output of that nested process is what is visible with cmd. but the same result and exit code i am not able to get through Process object ofPatchInstaller.exe. Is there any way of getting output of process running within process?

Following is the code i have tired...

 
string command = @"C:\Users\sysadmin\Desktop\Setup\PatchInstaller.exe";
string result = string.Empty;
System.Diagnostics.ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command + " --mode=silent);
System.Diagnostics.Process proc = new Process();
procStartInfo.ErrorDialog = false;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
if (!string.IsNullOrEmpty(domain) && !string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pwd))
{
procStartInfo.Domain = domain;
procStartInfo.UserName = user;
System.Security.SecureString ss = new System.Security.SecureString();
foreach (char c in pwd) { ss.AppendChar(c); }
procStartInfo.Password = ss;
}
proc = System.Diagnostics.Process.Start(procStartInfo);
proc.ErrorDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs errorLine)
{
if (errorLine.Data != null) result += "error:" + errorLine.Data +;
};
proc.OutputDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs outputLine)
{
if (outputLine.Data != null) result += outputLine.Data +;
};
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
 
More Explanation 

Here my exe is running and it will execute the below line first,

C:\Users\sysadmin\Desktop\New_folder\Setup\PatchInstaller.exe --mode=silent

Now PatchInstaller.exe is internally executing other process, I am expecting the output from this internal process,

(Example A:

C:\Users\sysadmin\Desktop\New_folder\Setup\PatchInstaller.exe --mode=silent

C:\Windows\system32>Begin Setup
UI mode: Silent
Error : Conflicting Processes running.
Exit Code: 19
)

(Note: PatchInstaller.exe is thirdparty exe. When we are excuting from command-line we are getting (Example A) output. )


 
 
 

Answers (2)