|
-
Dec 11th, 2007, 04:53 PM
#1
Thread Starter
Hyperactive Member
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:
Imports System.Data
Imports System.Timers
Imports System.Data.SqlClient
Imports System.Diagnostics
Public Class Form1
Private Sub GetProcessIDstats(ByVal strIP As String)
Dim procList() As Process = Process.GetProcesses(strIP)
Dim i As Integer
For Each procList(i) In procList
Dim strProcID As Double = procList(i).Id
Dim strProcName As String = procList(i).ProcessName
Dim dblVMem As Double = (procList(i).PagedMemorySize64) / 1024
If dblVMem > 100000 Then
lbProcessList.Items.Add( _
strProcName & " - " & _
strProcID.ToString & " - " & _
dblVMem.ToString)
End If
Next
End Sub
Private Sub btShowProcess_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btShowProcess.Click
GetProcessIDstats("192.168.1.10")
End Sub
End Class
-
Dec 11th, 2007, 05:01 PM
#2
Frenzied Member
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
-
Dec 11th, 2007, 11:28 PM
#3
Fanatic Member
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.
-
Dec 13th, 2007, 02:45 PM
#4
Thread Starter
Hyperactive Member
Re: Function to get UserName from TaskManager
OK now I am getting all the processes with the code below.
VB Code:
Dim moReturn As Management.ManagementObjectCollection
Dim moSearch As Management.ManagementObjectSearcher
Dim mo As Management.ManagementObject
moSearch = New Management.ManagementObjectSearcher _
("Select * from Win32_Process")
moReturn = moSearch.Get
For Each mo In moReturn
Dim arOwner(2)
mo.InvokeMethod("GetOwner", arOwner)
Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
mo("Name"), arOwner(0), arOwner(1)))
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:
Try
Dim moReturn As Management.ManagementObjectCollection
Dim moSearch As Management.ManagementObjectSearcher
Dim mo As Management.ManagementObject
Dim strExe As String = "myProgram.exe"
moSearch = New Management.ManagementObjectSearcher _
("Select * from Win32_Process Where OWNER = '" & strExe & "'")
moReturn = moSearch.Get
For Each mo In moReturn
Dim arOwner(2)
mo.InvokeMethod("GetOwner", arOwner)
Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
mo("Name"), arOwner(0), arOwner(1)))
Next
Catch ex As Exception
MessageBox.Show( _
"Message: " & ex.Message & vbNewLine & _
"Source: " & ex.Source & vbNewLine & _
"Trace: : " & ex.StackTrace, _
"Proc Lookup Error", MessageBoxButtons.OK)
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.
-
Dec 13th, 2007, 03:40 PM
#5
Frenzied Member
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:
For Each moE In moReturnE
Dim arOwner(2)
mo.InvokeMethod("GetOwner", arOwner)
Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
mo("Name"), arOwner(0), arOwner(1)))
Next
-
Dec 13th, 2007, 05:41 PM
#6
Thread Starter
Hyperactive Member
Re: Function to get UserName from TaskManager
I don't see/understand what you are saying.
-
Dec 13th, 2007, 06:35 PM
#7
Re: Function to get UserName from TaskManager
you just want the 1st process?
vb Code:
For Each mo In moReturn
Dim arOwner(2)
mo.InvokeMethod("GetOwner", arOwner)
Debug.WriteLine(String.Format("{0} Owner {1} Domain {2}", _
mo("Name"), arOwner(0), arOwner(1)))
exit for
Next
-
Dec 14th, 2007, 10:31 AM
#8
Thread Starter
Hyperactive Member
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
-
Dec 14th, 2007, 01:54 PM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|