Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than wtsapi32, prefix the name with the module name and a period.
WTSEnumerateSessions (wtsapi32)
.
C# Signature:
[DllImport("wtsapi32.dll", SetLastError=true)]
static extern TODO WTSEnumerateSessions(TODO);
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
<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
<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
'Private WTS_CURRENT_SERVER_HANDLE As Integer = 0
#End Region
#Region " Public "
Friend Structure strSessionsInfo
Dim SessionID As Integer
Dim StationName As String
Dim ConnectionState As String
End Structure
<DllImport("wtsapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function WTSOpenServer(ByVal pServerName As String) As IntPtr
End Function
Friend Function GetSessions(ByVal ServerName As String) As strSessionsInfo()
Dim ptrOpenedServer As IntPtr
Dim RetVal As strSessionsInfo()
Try
ptrOpenedServer = WTSOpenServer(ServerName)
Dim FRetVal As Int32
Dim ppSessionInfo As IntPtr = IntPtr.Zero
Dim Count As Int32 = 0
Try
FRetVal = WTSEnumerateSessions(ptrOpenedServer, 0, 1, ppSessionInfo, Count)
If FRetVal <> 0 Then
Dim sessionInfo() As WTS_SESSION_INFO = New WTS_SESSION_INFO(Count) {}
Dim i As Integer
For i = 0 To Count - 1 ' Step i + 1
sessionInfo(i) = CType(Marshal.PtrToStructure(ppSessionInfo, GetType(WTS_SESSION_INFO)), WTS_SESSION_INFO)
Next
WTSFreeMemory(ppSessionInfo)
Dim tmpArr(sessionInfo.GetUpperBound(0)) As strSessionsInfo
For i = 0 To tmpArr.GetUpperBound(0)
tmpArr(i).SessionID = sessionInfo(i).SessionID
tmpArr(i).StationName = sessionInfo(i).pWinStationName
tmpArr(i).ConnectionState = GetConnectionState(sessionInfo(i).State)
Next
ReDim sessionInfo(-1)
RetVal = tmpArr
Else
Throw New ApplicationException("No data retruned")
End If
Catch ex As Exception
Throw New Exception(ex.Message & vbCrLf & System.Runtime.InteropServices.Marshal.GetLastWin32Error)
End Try
Catch ex As Exception
Throw New Exception(ex.Message)
Exit Function
Finally
Try
WTSCloseServer(ptrOpenedServer)
Catch ex As Exception
Private Function GetConnectionState(ByVal State As WTS_CONNECTSTATE_CLASS) As String
Dim RetVal As String
Select Case State
Case WTS_CONNECTSTATE_CLASS.WTSActive
RetVal = "Active"
Case WTS_CONNECTSTATE_CLASS.WTSConnected
RetVal = "Connected"
Case WTS_CONNECTSTATE_CLASS.WTSConnectQuery
RetVal = "Query"
Case WTS_CONNECTSTATE_CLASS.WTSDisconnected
RetVal = "Disconnected"
Case WTS_CONNECTSTATE_CLASS.WTSDown
RetVal = "Down"
Case WTS_CONNECTSTATE_CLASS.WTSIdle
RetVal = "Idle"
Case WTS_CONNECTSTATE_CLASS.WTSInit
RetVal = "Initializing."
Case WTS_CONNECTSTATE_CLASS.WTSListen
RetVal = "Listen"
Case WTS_CONNECTSTATE_CLASS.WTSReset
RetVal = "reset"
Case WTS_CONNECTSTATE_CLASS.WTSShadow
RetVal = "Shadowing"
Case Else
RetVal = "Unknown connect state"
End Select
Return RetVal
End Function
#End Region
#End Region
End Class
Alternative Managed API:
Do you know one? Please contribute it!
This Terminal Services API call lists all local and remote sessions for a given server, including their state (e.g. connected, disconnected) and type (local, RDP). It is the basis for the output of qwinsta.exe.
3/25/2015 12:50:12 PM - -62.209.223.226
The WTSFreeMemory function frees memory allocated by a Terminal Services function.
7/12/2011 7:01:29 AM - -87.178.3.148
The WTSOpenServer function opens a handle to the specified terminal server.
4/8/2021 2:12:43 AM - -165.86.81.72
Closes an open handle to a terminal server opened by a call to the WTSOpenServer function.
3/26/2007 2:56:06 AM - -212.238.185.167
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).