Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Remote Process.GetProcesses and SessionId

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anders Bovin - 26 Oct 2007 15:53 GMT
Hi,

I am trying to get the SessionId of processes on a remote machine using
System.Diagnostics.Process:

foreach (Process p in Process.GetProcesses("remotemachine"))
{
    Console.WriteLine(p.SessionId.ToString());
}

SessionId is always 0 while Task Manager (on the remote machine) shows the
correct Session ID. Any ideas why?

Thanks,
Anders
Willy Denoyette [MVP] - 26 Oct 2007 18:13 GMT
> Hi,
>
[quoted text clipped - 11 lines]
> Thanks,
> Anders

Don't use this method, it's just a bag of bugs, use System.Management (WMI)
instead.

Following snip illustrates how you can get the Processes SessionId from a
remote server:
Check MSDN for more detail on WMI's Win32_Process class.
...
 ConnectionOptions co = new ConnectionOptions();;
 co.Username = "administrator"; //any account with appropriate privileges
 co.Password = "pppppp";
 string remMachine = "machineName";
 ManagementScope scope = new ManagementScope(@"\\" + remMachine +
@"\root\cimv2", co);
       SelectQuery selectQuery = new SelectQuery("Select Name, SessionId
from Win32_Process");
       using(ManagementObjectSearcher searcher =
           new ManagementObjectSearcher(scope, selectQuery))
       {
           foreach (ManagementObject proc in searcher.Get())
               Console.WriteLine("{0}, {1}",proc["Name"].ToString(),
proc["SessionId"].ToString());
       }

Willy.
Anders Bovin - 27 Oct 2007 12:20 GMT
OK! Will do.

Thanks Willy

> > Hi,
> >
[quoted text clipped - 36 lines]
>
> Willy.
Willy Denoyette [MVP] - 27 Oct 2007 17:08 GMT
> OK! Will do.
>
> Thanks Willy

Note that there is nothing wrong with this method when you have to query the
processes running on a local machine, the bugs I've mentioned are related to
the GetProcesses("remotemachine") method. This method uses an internal API
"NtQuerySystemInformation"
http://msdn2.microsoft.com/en-us/library/ms724509.aspx which by itself
should never be used in production code, as as an *added bonus* the
Framework code added a couple of bugs in the part that marshals the
(undocumented part of)  _SYSTEM_PROCESS_INFORMATION  structure.

Willy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.