Start Remote Process and track the exit code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I am trying to execute an application on a remote system. After checking
several of the website I was managed to write a C# application do the same.

But I am not able to track the Exit Code of the application. The return
value only tells me if the application started successfully.

Is their a way to capture the final exit code of the application that is
started remotely?

using Invoke Method
InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null,
System.TimeSpan.MaxValue);

And

ManagementEventWatcher for completion of the applicaiton.




Thanks,
Suresh
 
Suresh,

Can you show some code how you are starting the process in the first
place?
 
Sure. Thanks for your help.

private void Form1_Load(object sender, EventArgs e)
{

ConnectionOptions co = new ConnectionOptions();

co.Impersonation = ImpersonationLevel.Impersonate;
co.EnablePrivileges = true;

co.Username = "User";
co.Password = "password";

ManagementScope myscope = new
ManagementScope(@"\\Computername\ROOT\CIMV2", co);
myscope.Connect();

ManagementClass myclass = new ManagementClass(myscope, new
ManagementPath("Win32_Process"), new ObjectGetOptions(null,
TimeSpan.MaxValue, true));
ManagementBaseObject inparams =
myclass.GetMethodParameters("Create");
inparams["CommandLine"] = @"calc.exe";

InvokeMethodOptions MethodOptions = new
InvokeMethodOptions(null, System.TimeSpan.MaxValue);
ManagementBaseObject outparams = myclass.InvokeMethod("Create",
inparams, MethodOptions);

string ProcID = outparams["ProcessID"].ToString();
string retval = outparams["ReturnValue"].ToString();

WqlEventQuery wQuery = new WqlEventQuery("Select * From
__InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'");

ManagementEventWatcher wWatcher = new
ManagementEventWatcher(myscope, wQuery);

int i = 1;

while (i == 1)
{
ManagementBaseObject MBOobj = wWatcher.WaitForNextEvent();

if
(((ManagementBaseObject)MBOobj["TargetInstance"])["ProcessID"].ToString() ==
ProcID)
{

//MessageBox.Show(retval+ " - " +
((ManagementBaseObject)MBOobj["TargetInstance"])["Name"].ToString() + " - " +
((ManagementBaseObject)MBOobj["TargetInstance"])["ExecutablePath"].ToString()); ;
i = 5;

}

}

wWatcher.Stop();

}

Nicholas Paldino said:
Suresh,

Can you show some code how you are starting the process in the first
place?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Suresh Nagarajan said:
Hello,
I am trying to execute an application on a remote system. After checking
several of the website I was managed to write a C# application do the
same.

But I am not able to track the Exit Code of the application. The return
value only tells me if the application started successfully.

Is their a way to capture the final exit code of the application that is
started remotely?

using Invoke Method
InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null,
System.TimeSpan.MaxValue);

And

ManagementEventWatcher for completion of the applicaiton.




Thanks,
Suresh
 
Suresh Nagarajan said:
Sure. Thanks for your help.

private void Form1_Load(object sender, EventArgs e)
{

ConnectionOptions co = new ConnectionOptions();

co.Impersonation = ImpersonationLevel.Impersonate;
co.EnablePrivileges = true;

co.Username = "User";
co.Password = "password";

ManagementScope myscope = new
ManagementScope(@"\\Computername\ROOT\CIMV2", co);
myscope.Connect();

ManagementClass myclass = new ManagementClass(myscope, new
ManagementPath("Win32_Process"), new ObjectGetOptions(null,
TimeSpan.MaxValue, true));
ManagementBaseObject inparams =
myclass.GetMethodParameters("Create");
inparams["CommandLine"] = @"calc.exe";

InvokeMethodOptions MethodOptions = new
InvokeMethodOptions(null, System.TimeSpan.MaxValue);
ManagementBaseObject outparams = myclass.InvokeMethod("Create",
inparams, MethodOptions);

string ProcID = outparams["ProcessID"].ToString();
string retval = outparams["ReturnValue"].ToString();

WqlEventQuery wQuery = new WqlEventQuery("Select * From
__InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'");

ManagementEventWatcher wWatcher = new
ManagementEventWatcher(myscope, wQuery);

int i = 1;

while (i == 1)
{
ManagementBaseObject MBOobj = wWatcher.WaitForNextEvent();

if
(((ManagementBaseObject)MBOobj["TargetInstance"])["ProcessID"].ToString() ==
ProcID)
{

//MessageBox.Show(retval+ " - " +
((ManagementBaseObject)MBOobj["TargetInstance"])["Name"].ToString() + " - " +
((ManagementBaseObject)MBOobj["TargetInstance"])["ExecutablePath"].ToString()); ;
i = 5;

}

}

wWatcher.Stop();

}

Nicholas Paldino said:
Suresh,

Can you show some code how you are starting the process in the first
place?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Suresh Nagarajan said:
Hello,
I am trying to execute an application on a remote system. After checking
several of the website I was managed to write a C# application do the
same.

But I am not able to track the Exit Code of the application. The return
value only tells me if the application started successfully.

Is their a way to capture the final exit code of the application that is
started remotely?

using Invoke Method
InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null,
System.TimeSpan.MaxValue);

And

ManagementEventWatcher for completion of the applicaiton.




Thanks,
Suresh


You can't get the "exit code" on anything less than Vista and Longhorn,
So the following only works on Vista, on XP you get the exit event but without the
ExitStatus property.

......
WqlEventQuery q = new WqlEventQuery( "Win32_ProcessStopTrace");
using(ManagementEventWatcher w = new ManagementEventWatcher(q)){
w.EventArrived += new EventArrivedEventHandler(ProcessStoptEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
w.Stop();
}
}
static void ProcessStoptEventArrived(object sender, EventArrivedEventArgs e) {

Console.WriteLine("Process : {0}, stopped with ExitStatus :{1},
Console.WriteLine("Process: {0}, Stopped with Code: {1}",
(int)(uint)e.NewEvent.Properties["ProcessId"].Value,
(int)(uint)e.NewEvent.Properties["ExitStatus"].Value);

}
....

Willy.
 
Hi Willy,
Thanks for your response. I came across .NET Remoting. without looking
deep into it, wanted to know if this is something that I could use to get the
same results.

Please forgive my ignorance. I am a newbie to C#.

Thanks,
Suresh

Willy Denoyette said:
Suresh Nagarajan said:
Sure. Thanks for your help.

private void Form1_Load(object sender, EventArgs e)
{

ConnectionOptions co = new ConnectionOptions();

co.Impersonation = ImpersonationLevel.Impersonate;
co.EnablePrivileges = true;

co.Username = "User";
co.Password = "password";

ManagementScope myscope = new
ManagementScope(@"\\Computername\ROOT\CIMV2", co);
myscope.Connect();

ManagementClass myclass = new ManagementClass(myscope, new
ManagementPath("Win32_Process"), new ObjectGetOptions(null,
TimeSpan.MaxValue, true));
ManagementBaseObject inparams =
myclass.GetMethodParameters("Create");
inparams["CommandLine"] = @"calc.exe";

InvokeMethodOptions MethodOptions = new
InvokeMethodOptions(null, System.TimeSpan.MaxValue);
ManagementBaseObject outparams = myclass.InvokeMethod("Create",
inparams, MethodOptions);

string ProcID = outparams["ProcessID"].ToString();
string retval = outparams["ReturnValue"].ToString();

WqlEventQuery wQuery = new WqlEventQuery("Select * From
__InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'");

ManagementEventWatcher wWatcher = new
ManagementEventWatcher(myscope, wQuery);

int i = 1;

while (i == 1)
{
ManagementBaseObject MBOobj = wWatcher.WaitForNextEvent();

if
(((ManagementBaseObject)MBOobj["TargetInstance"])["ProcessID"].ToString() ==
ProcID)
{

//MessageBox.Show(retval+ " - " +
((ManagementBaseObject)MBOobj["TargetInstance"])["Name"].ToString() + " - " +
((ManagementBaseObject)MBOobj["TargetInstance"])["ExecutablePath"].ToString()); ;
i = 5;

}

}

wWatcher.Stop();

}

Nicholas Paldino said:
Suresh,

Can you show some code how you are starting the process in the first
place?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

message Hello,
I am trying to execute an application on a remote system. After checking
several of the website I was managed to write a C# application do the
same.

But I am not able to track the Exit Code of the application. The return
value only tells me if the application started successfully.

Is their a way to capture the final exit code of the application that is
started remotely?

using Invoke Method
InvokeMethodOptions MethodOptions = new InvokeMethodOptions(null,
System.TimeSpan.MaxValue);

And

ManagementEventWatcher for completion of the applicaiton.




Thanks,
Suresh


You can't get the "exit code" on anything less than Vista and Longhorn,
So the following only works on Vista, on XP you get the exit event but without the
ExitStatus property.

......
WqlEventQuery q = new WqlEventQuery( "Win32_ProcessStopTrace");
using(ManagementEventWatcher w = new ManagementEventWatcher(q)){
w.EventArrived += new EventArrivedEventHandler(ProcessStoptEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
w.Stop();
}
}
static void ProcessStoptEventArrived(object sender, EventArrivedEventArgs e) {

Console.WriteLine("Process : {0}, stopped with ExitStatus :{1},
Console.WriteLine("Process: {0}, Stopped with Code: {1}",
(int)(uint)e.NewEvent.Properties["ProcessId"].Value,
(int)(uint)e.NewEvent.Properties["ExitStatus"].Value);

}
....

Willy.
 
Back
Top