473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Start Remote Process and track the exit code

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

Apr 4 '07 #1
4 5473
Suresh,

Can you show some code how you are starting the process in the first
place?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Suresh Nagarajan" <Su*************@discussions.microsoft.comwrote in
message news:C2**********************************@microsof t.com...
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

Apr 4 '07 #2
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 [.NET/C# MVP]" wrote:
Suresh,

Can you show some code how you are starting the process in the first
place?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Suresh Nagarajan" <Su*************@discussions.microsoft.comwrote in
message news:C2**********************************@microsof t.com...
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


Apr 5 '07 #3
"Suresh Nagarajan" <Su*************@discussions.microsoft.comwrote in message
news:9A**********************************@microsof t.com...
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 [.NET/C# MVP]" wrote:
>Suresh,

Can you show some code how you are starting the process in the first
place?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Suresh Nagarajan" <Su*************@discussions.microsoft.comwrote in
message news:C2**********************************@microsof t.com...
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.

Apr 6 '07 #4
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 [MVP]" wrote:
"Suresh Nagarajan" <Su*************@discussions.microsoft.comwrote in message
news:9A**********************************@microsof t.com...
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 [.NET/C# MVP]" wrote:
Suresh,

Can you show some code how you are starting the process in the first
place?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Suresh Nagarajan" <Su*************@discussions.microsoft.comwrote in
message news:C2**********************************@microsof t.com...
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.

Apr 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Åstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
2
by: Tobias Johansson | last post by:
Hello, I'm having what I believe a security problem to execute an executable file from a windows service in windows server 2003. It works fine in WIN XP SP2 The program(the service) itself...
10
by: Tony | last post by:
I am running an application called AcroComm.exe to poll time clocks here at our company. I have written a small C# app that will poll the clocks based on information found in a DB. My problem is...
14
by: Jasonkimberson | last post by:
Currently I am having a problem creating a windows service that monitors a mapped drive for events (using the FileSystemEventHandler). The service complies but fails to start: "The XMLWatcher...
16
by: Serdar Kalaycý | last post by:
Hi everybody, My problem seems a bit clichè but I could not work around. Well I read lots of MSDN papers and discussions, but my problem is a bit different from them. When I tried to run the...
2
by: Greg Allen | last post by:
I know this has been discussed before, and have found some documentation about it on the web. But nothing has fixed my problem. I am running the 1.1 .NET framework, SP1. I have a web...
10
by: Doug Robertson | last post by:
First off, I'm a hardware/OS guy. I just write code on the side and I'm completely self taught - so bear in mind my total lack of expertise. I have a program originally written in VB2003 using...
3
by: Billy Bob | last post by:
Hello In C# how can i start a remote process such as someapp.exe on a remote PC? I know the remote PC's name, but how do I start the app on that PC? Thanks ..
5
by: sjoshi23 | last post by:
Hello I'm trying to start a remote desktop session using Process.Start but it keeps saying "File not found". The same cmd thru DOS window works fine. I used: C:\\WINDOWS\\system32\\mstsc.exe...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.