[RESOLVED] Count processes per user in terminal server session?
We can enumerate running executables using EnumProcesses, OpenProcess, EnumProcessModules and GetModuleBaseName APIs, but how to distinquish process count between users from server memory, when two or more users are running one or more copies of same executable in Terminal Server session?
Need to compute count of processes running in my own account only - fex. count of notepad processes run by me.
Code:
Private Const PROCESS_VM_READ As Long = &H10 'Enables using the process handle in the ReadProcessMemory function to read from the virtual memory of the process.
Private Const PROCESS_QUERY_INFORMATION As Long = &H400 'Enables using the process handle in the GetExitCodeProcess and GetPriorityClass functions to read information from the process object.
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function EnumProcesses Lib "PSAPI.DLL" (lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
Private Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
Private Declare Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Sub Form_Load()
Debug.Print "Count of Notepad processes =" & GetProcessCount("notepad")
End Sub
Function GetProcessCount(ByVal AppEXE_Filename As String) As Long
Const MAX_PATH As Long = 260
Dim lProcesses() As Long
Dim lModules() As Long
Dim n As Long
Dim lRet As Long
Dim hProcess As Long
Dim sName As String
'Call by: application executable name, without .exe part -> fex. notepad
AppEXE_Filename = UCase$(AppEXE_Filename)
ReDim lProcesses(1023) As Long
If EnumProcesses(lProcesses(0), 1024 * 4, lRet) Then
For n = 0 To (lRet \ 4) - 1
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcesses(n))
If hProcess Then
ReDim lModules(1023)
If EnumProcessModules(hProcess, lModules(0), 1024 * 4, lRet) Then
sName = String$(MAX_PATH, vbNullChar)
'Debug.Print sName
GetModuleBaseName hProcess, lModules(0), sName, MAX_PATH
sName = Left$(sName, InStr(sName, vbNullChar) - 1) 'Strip nulls.
sName = UCase$(Left$(sName, InStrRev(sName, ".") - 1)) 'strip .exe from end
'Add count of specific process
Select Case sName
Case AppEXE_Filename
GetProcessCount = GetProcessCount + 1
End Select
End If 'If EnumProcess
End If 'If hProcess Then
CloseHandle hProcess
Next n
End If
End Function
Last edited by Tech99; Mar 20th, 2017 at 09:56 PM.
Reason: clarified question a bit.
Re: Count processes per user in terminal server session?
Thanks Peter token api helped too.
Dilettante, thank you very much. WTSEnumerateProcesses is the right solution. Now actually implementing it.
Processes can be listed using WTSEnumerateProcesses api and username for the invidual processes can be listed using LookupAccountSid api function. LookupAccountSid returns username for the logged in user processes only.
Above listed GetProcessCount actually works also, it computes only user own processes.