Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process Enmuration

Status
Not open for further replies.

lothos12345

Programmer
Mar 8, 2005
40
US
Can anyone explain to me how to accomplish the following?

I need to find a method in VB .net to enumerate a process user name or sessionid. User name is preferred. Native is preferred, using system.diagnostics, if not available then API and WMI as last option.

Any help offered is greatly appreciated.
 
This should give you a starting point - this iterates through all processes running on the local machine. I'm pretty sure you can do this for a named machine also.
Code:
		For Each proc As Process In Diagnostics.Process.GetProcesses
			Dim sessid As Integer
			ProcessIdToSessionId(proc.Id, sessid)
		Next
Note, this uses:
Code:
Declare Function ProcessIdToSessionId Lib "kernel32.dll" (ByVal dwProcessId As Int32, ByRef pSessionId As Int32) As Int32

I found that many of our users did not have rights to execute the ProcessIdToSessionId function, so used impersonation to get around this.
 
This is great is there away I can get the username as well. I want the user to be able to enter in a process name and get the user name for the process. The same goes for the sessionid your code worked for it I just modified it a little making a minor change to the Declare Function it now looks like:

Declare Function ProcessIdToSessionId Lib "kernel32.dll" (ByVal dwProcessId As String, ByRef pSessionId As Int32) As Int32


Now I just need to do this to get the UserName as well.
 
Not sure what it is you're after.

ProcessName
Code:
Diagnostics.Process.ProcessName
UserName
Code:
Environment.UserName

Does this help?
 
I want to be able to get the username of a running process. Like the user name for the process NicConfigSvc on my machine is SYSTEM. So I want the user to enter 'NicConfigSvc' because that is the name of the process and get 'SYSTEM' in return because that is the User Name for the 'NicConfigSvc'.
 
Can't help you with that one I'm afraid.

You could try searching the web for Win32_Process GetOwner.
 
This link for an example of working with the WMI Wrapper in .Net was posted in another thread.


The example for download pg6 of the article works and gives you a way in to reading the properties of a Process e.g. SessionID.

You should be able to use System.Management.ManagementObject.Invokemethod() to execute GetOwner Method of the Win32_Process Class


Hope this gets you what you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top