Home All Groups Group Topic Archive Search About

Start Remote Process and track the exit code

Author
4 Apr 2007 8:50 PM
Suresh Nagarajan
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

Author
4 Apr 2007 8:58 PM
Nicholas Paldino [.NET/C# MVP]
Suresh,

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


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

Show quoteHide quote
"Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in
message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.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
>
Are all your drivers up to date? click for free checkup

Author
5 Apr 2007 4:32 AM
Suresh Nagarajan
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();

}

Show quoteHide quote
"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]
>           - mvp@spam.guard.caspershouse.com
>
> "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in
> message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.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
> >
>
>
>
Author
6 Apr 2007 2:27 PM
Willy Denoyette [MVP]
Show quote Hide quote
"Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in message
news:9AC3604A-E094-4556-AC40-84063B541245@microsoft.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]
>>           - mvp@spam.guard.caspershouse.com
>>
>> "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in
>> message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.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.
Author
6 Apr 2007 4:30 PM
Suresh Nagarajan
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

Show quoteHide quote
"Willy Denoyette [MVP]" wrote:

> "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in message
> news:9AC3604A-E094-4556-AC40-84063B541245@microsoft.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]
> >>           - mvp@spam.guard.caspershouse.com
> >>
> >> "Suresh Nagarajan" <SureshNagara***@discussions.microsoft.com> wrote in
> >> message news:C26FB70A-C6C6-4EED-B7D1-B085E00295E5@microsoft.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.
>
>

Bookmark and Share