wtsenumeratesessions (wtsapi32)
Last changed: -62.209.223.226

.
Summary
The WTSEnumerateSessions function retrieves a list of sessions on a specified terminal server.

C# Signature:

[
DllImport( "wtsapi32.dll",
       SetLastError = true,
       CallingConvention = CallingConvention.StdCall,
       CharSet = CharSet.Auto,
       ThrowOnUnmappableChar = true
       )
]
static extern int WTSEnumerateSessions(
    IntPtr hServer,
    UInt32 Reserved, // must be 0
    UInt32 Version,  // must be 1
    ref IntPtr ppSessionInfo,
    ref UInt32 pCount
    );

VB Signature:

    <DllImport("wtsapi32.dll", _
    bestfitmapping:=True, _
    CallingConvention:=CallingConvention.StdCall, _
    CharSet:=CharSet.Auto, _
    EntryPoint:="WTSEnumerateSessions", _
    setlasterror:=True, _
    ThrowOnUnmappableChar:=True)> _
    Private Shared Function WTSEnumerateSessions( _
    ByVal hServer As IntPtr, _
    <MarshalAs(UnmanagedType.U4)> _
    ByVal Reserved As Int32, _
    <MarshalAs(UnmanagedType.U4)> _
    ByVal Vesrion As Int32, _
    ByRef ppSessionInfo As IntPtr, _
    <MarshalAs(UnmanagedType.U4)> _
    ByRef pCount As Int32) As Int32
    End Function

User-Defined Types:

    Private Enum WTS_CONNECTSTATE_CLASS
    WTSActive
    WTSConnected
    WTSConnectQuery
    WTSShadow
    WTSDisconnected
    WTSIdle
    WTSListen
    WTSReset
    WTSDown
    WTSInit
    End Enum

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Private Structure WTS_SESSION_INFO
    Dim SessionID As Int32 'DWORD integer
    Dim pWinStationName As String ' integer LPTSTR - Pointer to a null-terminated string containing the name of the WinStation for this session
    Dim State As WTS_CONNECTSTATE_CLASS
    End Structure

Notes:

To free the returned buffer pointed by ppSessionInfo, call the WTSFreeMemory function.

To be able to enumerate a session, you need to have the Query Information permission

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

Do you know one? Please contribute it!

Documentation