[RESOLVED] Count processes per user in terminal server session?-VBForums
Results 1 to 4 of 4

Thread: [RESOLVED] Count processes per user in terminal server session?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    536

    Resolved [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.

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    581

    Re: Count processes per user in terminal server session?


  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,340

    Re: Count processes per user in terminal server session?

    Have you looked at WTSEnumerateProcessesEx function?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    536

    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.
    Attached Images Attached Images  

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.