Results 1 to 9 of 9

Thread: Function to get UserName from TaskManager

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Function to get UserName from TaskManager

    Does anybody have a Function to get the username from task manager? I have this code that I gets me everything I need but the username. From what I have read it appears that I need to query Windows WMI but I am not real sure what that means.

    VB Code:
    1. Imports System.Data
    2. Imports System.Timers
    3. Imports System.Data.SqlClient
    4. Imports System.Diagnostics
    5. Public Class Form1
    6.     Private Sub GetProcessIDstats(ByVal strIP As String)
    7.         Dim procList() As Process = Process.GetProcesses(strIP)
    8.         Dim i As Integer
    9.         For Each procList(i) In procList
    10.             Dim strProcID As Double = procList(i).Id
    11.             Dim strProcName As String = procList(i).ProcessName
    12.             Dim dblVMem As Double = (procList(i).PagedMemorySize64) / 1024
    13.             If dblVMem > 100000 Then
    14.                 lbProcessList.Items.Add( _
    15.                     strProcName & " - " & _
    16.                     strProcID.ToString & " - " & _
    17.                     dblVMem.ToString)
    18.             End If
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub btShowProcess_Click( _
    23.         ByVal sender As System.Object, _
    24.         ByVal e As System.EventArgs) _
    25.         Handles btShowProcess.Click
    26.  
    27.         GetProcessIDstats("192.168.1.10")
    28.  
    29.     End Sub
    30. End Class

  2. #2
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Function to get UserName from TaskManager

    I've tried getting the username of a process before, with no success,
    I am not sure if it can be done, I am probably wrong though, considering the Task Manager can do it:P

  3. #3
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Function to get UserName from TaskManager

    You can get the user of any process using the unmanaged WTSQuerySessionInformation API.
    http://msdn2.microsoft.com/en-us/library/aa383838.aspx

    You can use this API to essentially get all the same information like the TaskManager.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Function to get UserName from TaskManager

    OK now I am getting all the processes with the code below.
    VB Code:
    1. Dim moReturn As Management.ManagementObjectCollection
    2.         Dim moSearch As Management.ManagementObjectSearcher
    3.         Dim mo As Management.ManagementObject
    4.         moSearch = New Management.ManagementObjectSearcher _
    5.             ("Select * from Win32_Process")
    6.         moReturn = moSearch.Get
    7.         For Each mo In moReturn
    8.             Dim arOwner(2)
    9.             mo.InvokeMethod("GetOwner", arOwner)
    10.             Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
    11.                 mo("Name"), arOwner(0), arOwner(1)))
    12.         Next

    But I want to limit by one program. I thought I could modify the Select Statement below to something like this to make it work but it doesn't.
    VB Code:
    1. Try
    2.             Dim moReturn As Management.ManagementObjectCollection
    3.             Dim moSearch As Management.ManagementObjectSearcher
    4.             Dim mo As Management.ManagementObject
    5.             Dim strExe As String = "myProgram.exe"
    6.             moSearch = New Management.ManagementObjectSearcher _
    7.                 ("Select * from Win32_Process Where OWNER = '" & strExe & "'")
    8.             moReturn = moSearch.Get
    9.             For Each mo In moReturn
    10.                 Dim arOwner(2)
    11.                 mo.InvokeMethod("GetOwner", arOwner)
    12.                 Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
    13.                     mo("Name"), arOwner(0), arOwner(1)))
    14.             Next
    15.         Catch ex As Exception
    16.             MessageBox.Show( _
    17.            "Message: " & ex.Message & vbNewLine & _
    18.            "Source: " & ex.Source & vbNewLine & _
    19.            "Trace: : " & ex.StackTrace, _
    20.            "Proc Lookup Error", MessageBoxButtons.OK)
    21.         End Try

    This is the error

    ---------------------------
    Proc Lookup Error
    ---------------------------
    Message: Invalid query
    Source: System.Management
    Trace: : at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext() at GetProcUser.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\xxxxxx\AppData\Local\Temporary Projects\GetProcUser\Form1.vb:line 18

    By the way line 18 is the Next.

  5. #5
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: Function to get UserName from TaskManager

    try not using the same variable names in your For Each statement, as the ones you have declared..

    vb.net Code:
    1. For Each moE In moReturnE
    2.             Dim arOwner(2)
    3.             mo.InvokeMethod("GetOwner", arOwner)
    4.             Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
    5.                 mo("Name"), arOwner(0), arOwner(1)))
    6.         Next

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Function to get UserName from TaskManager

    I don't see/understand what you are saying.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Function to get UserName from TaskManager

    you just want the 1st process?

    vb Code:
    1. For Each mo In moReturn            
    2.      Dim arOwner(2)            
    3.      mo.InvokeMethod("GetOwner", arOwner)            
    4.      Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
    5.      mo("Name"), arOwner(0), arOwner(1)))        
    6.      exit for
    7. Next

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Function to get UserName from TaskManager

    I am not looking for the first process. I am looking for a particular process. Actualy I want the owner of the user using my vb.net program. So if my program was named myprogram.exe this is what I am looking for.

    myprogram.exe Owner FastEddie Domain MyDomain

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Function to get UserName from TaskManager

    I just had to fix my select statement and make it this

    "Select * from Win32_Process Where Name= 'myprogram.exe'"

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