WTSEnumerateProcesses (wtsapi32)
Last changed: -78.175.55.204

.
Summary
The WTSEnumerateProcesses function retrieves information about the active processes on a specified terminal server.

C# Signature:

[DllImport("wtsapi32.dll", SetLastError=true)]
static extern TODO WTSEnumerateProcesses(TODO);

VB Signature:

    Private Declare Function WTSEnumerateProcesses Lib "wtsapi32.dll" ( _
    ByVal hServer As Int32, _
    ByVal Reserved As Int32, _
    ByVal Version As Int32, _
    ByRef ppProcessInfo As IntPtr, _
    ByRef pCount As Int32) As Int32

User-Defined Types:

WTS_PROCESS_INFO

Alternative Managed API:

Much can be done with the System.Diagnostics.Process class with methods such as GetProcesses, GetProcessByName and GetProcessByID.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    Dim strucProcessInfo As WTS_PROCESS_INFO
    Dim ptrProcessInfo As IntPtr
    Dim lngPtrPos As Long
    Dim intReturn As Integer
    Dim intCount As Integer
    Dim intProcessCount As Integer
    Dim strProcessName As String

    intReturn = WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, ptrProcessInfo, intProcessCount)

    'Get the length in bytes of each structure...
    lngPtrPos = ptrProcessInfo.ToInt32()

    For intCount = 0 To intProcessCount - 1
        strucProcessInfo = Marshal.PtrToStructure(New IntPtr(lngPtrPos), strucProcessInfo.GetType)
        'ProcessName is merely a pointer to the string - convert it to the full string...
        strProcessName = Marshal.PtrToStringAnsi(New IntPtr(strucProcessInfo.ProcessName))

        Console.WriteLine ("Process Name: " & strProcessName)
        Console.WriteLine ("Process ID: " & strucProcessInfo.ProcessID)
        Console.WriteLine ("Session ID: " & strucProcessInfo.SessionID)

        'Now move on the pointer to the next member of the array...
        lngPtrPos = lngPtrPos + Len(strucProcessInfo)
    Next

    Call WTSFreeMemory(ptrProcessInfo)

Documentation