Results 1 to 9 of 9

Thread: [RESOLVED] Check if a process is running on a remote computer.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Resolved [RESOLVED] Check if a process is running on a remote computer.

    Hi.
    I need to check if the explorer.exe process is running on a remote computer to verify that there is a user logged on the computer. I Also want to get the Process Owner of the explorer.exe process to check which user is logged on. I have found some VBscript examples online, but i can't seem to get them to work. If anyone knows of a better way of checking if and what user is logged on to a computer, please give me a reply also. I have tried System.Diagnostics.Process.GetProcesses, but i can't seem to get the owner of the process, maybe WMI will do the trick ? Anyone have som VB code for this task....

    Last edited by StianR; Dec 4th, 2009 at 06:53 AM.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Check if a process is running on a remote computer.

    Checking to see if explorer.exe is running is not the best way to check if someone is logged on really... I'm not a fan of WMI but its probably the easiest way to do it. I found this code on google (did you not try searching there first?) http://snippets.dzone.com/posts/show/5478 and tidied it up a bit to come up with this, which seems to work fine on the 3 servers I just tested it on:

    vb Code:
    1. Dim Strcomputer As String = "ENTER COMPUTER NAME HERE"
    2.  
    3.         Dim WMIConnectionOptions As New ConnectionOptions
    4.         WMIConnectionOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate
    5.         WMIConnectionOptions.Authentication = System.Management.AuthenticationLevel.Packet
    6.  
    7.         Dim WMIScope As New Management.ManagementScope("\\" & _
    8.         Strcomputer & "\root\cimv2", WMIConnectionOptions)
    9.  
    10.         Dim oq As New System.Management.ObjectQuery("SELECT UserName from Win32_ComputerSystem")
    11.         Dim query As New ManagementObjectSearcher(WMIScope, oq)
    12.         Dim queryCollection As ManagementObjectCollection = query.Get()
    13.  
    14.         For Each oReturn As ManagementObject In queryCollection
    15.             If Not oReturn("UserName") Is Nothing Then
    16.                 MessageBox.Show(oReturn("UserName").ToString)
    17.             Else
    18.                 MessageBox.Show("Not logged in")
    19.             End If
    20.         Next
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Re: Check if a process is running on a remote computer.

    Hi Chris, and thank you for your reply. I found some examples online, but i guess i do not have the required knowledge to get it to work. Thank again for your effort, i will try it first thing on monday when i'm back at work

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Re: Check if a process is running on a remote computer.

    Hi Again.
    This code did not work i'm affraid . It reports back my username as logged in even if i log out the RDP session i'm using to test the code. I am creating an application that checks which of our HP ProLiant xw460c Blade Workstation is available for a HP RGS connection. When the user connects to a available Workstation, the RGS receiver software will make a connection in the background. Everything seems to work using different WMi queries, except the "check if a user is logged in" part.
    Attached Images Attached Images  

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Check if a process is running on a remote computer.

    Are you sure you are actually logging the RDP connection off and not just closing the RDP window (which leaves your session running) ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Re: Check if a process is running on a remote computer.

    Hi.
    Yes i log off the RDP session. I have managed to get together some code which invokes the "GetOwner" method of the Win32_Process. but i now get the following exception :

    System.InvalidOperationException: Operation is not valid due to the current state of the object.
    at System.Management.ManagementObject.InvokeMethod(String methodName, Object[] args)
    at GeoConnect.MainWindow.CheckLoginState(String strHostName) in D:\GeoConnect\GeoConnect\MainWindow.xaml.vb:line 123

    It breaks on the following line: ExProcess.InvokeMethod("GetOwner",CType(ProcReturn, Object()))

    I guess i'm doing something wrong with the ProcReturn variable.

    Here is my current code:

    GetUserName Code:
    1. ' Check if a user is logged on to a remote host. Return the user name if true.
    2.     Private Function CheckLoginState(ByVal strHostName As String) As String
    3.         Try
    4.             ' WMI Connection Options.
    5.             Dim WMIConnectionOptions As New ConnectionOptions
    6.             WMIConnectionOptions.Impersonation = System.Management.ImpersonationLevel.Impersonate
    7.             WMIConnectionOptions.Authentication = System.Management.AuthenticationLevel.Packet
    8.  
    9.             ' WMI Query.
    10.             Dim WMIScope As New Management.ManagementScope("\\" & strHostName & "\root\cimv2", WMIConnectionOptions)
    11.             Dim MyQuery As New System.Management.ObjectQuery("Select Name from Win32_Process Where Name='explorer.exe'")
    12.             Dim MySearcher As New ManagementObjectSearcher(WMIScope, MyQuery)
    13.             Dim MyResults As ManagementObjectCollection = MySearcher.Get()
    14.  
    15.             If Not MyResults.Count = 0 Then
    16.                 For Each ExProcess As ManagementObject In MyResults
    17.                     Dim ProcReturn(1) As String
    18.                     ExProcess.InvokeMethod("GetOwner", CType(ProcReturn, Object()))
    19.                     Return (ProcReturn(1)).ToString
    20.                 Next
    21.             End If
    22.             Return "None"
    23.         Catch ex As Exception
    24.         MessageBox.Show(ex.ToString)
    25.         Return "Error"
    26.         End Try
    27.     End Function
    Last edited by StianR; Dec 7th, 2009 at 08:05 AM.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Check if a process is running on a remote computer.

    Change this line:

    Code:
    Dim MyQuery As New System.Management.ObjectQuery("Select Name from Win32_Process Where Name='explorer.exe'")
    to this:
    Code:
    Dim MyQuery As New System.Management.ObjectQuery("Select * from Win32_Process Where Name='explorer.exe'")
    and change this line:
    Code:
    ExProcess.InvokeMethod("GetOwner", CType(ProcReturn, Object()))
    to this:
    Code:
    ExProcess.InvokeMethod("GetOwner", ProcReturn)
    annnd this line:
    Code:
    Return (ProcReturn(1)).ToString
    to this:
    Code:
    Return (ProcReturn(0)).ToString
    because ProcReturn(1) will be the user's domain - ProcReturn(0) will be the username.

    worked most of that out by looking at the MSDN page for the method you are trying to invoke: http://msdn.microsoft.com/en-us/libr...60(VS.85).aspx
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Re: Check if a process is running on a remote computer.

    Thank you Chris. That worked perfectly. I can see the changes you made makes sense. I guess i was getting blind while tring to get it to work. Once again Big thanks

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Check if a process is running on a remote computer.

    no worries
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width