Microsoft KB Archive/291789

= How To Use the WTSEnumerateSessions Terminal Server Function from Visual Basic =

Article ID: 291789

Article Last Modified on 7/15/2004

-

APPLIES TO

 Microsoft Visual Basic 5.0 Enterprise Edition, when used with:  Microsoft Windows 2000 Standard Edition

 Microsoft Windows NT 4.0  Microsoft Visual Basic 6.0 Enterprise Edition, when used with:  Microsoft Windows 2000 Standard Edition

 Microsoft Windows NT 4.0</li></ul> </li> Microsoft Visual Basic 5.0 Professional Edition, when used with:  Microsoft Windows 2000 Standard Edition</li></ul>

 Microsoft Windows NT 4.0</li></ul> </li> Microsoft Visual Basic 6.0 Professional Edition, when used with:  Microsoft Windows 2000 Standard Edition</li></ul>

 Microsoft Windows NT 4.0</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q291789

<div class="summary_section">

SUMMARY
This article includes a code sample that demonstrates how to use the WTSEnumerateSessions Terminal Server function from Visual Basic.

<div class="moreinformation_section">

MORE INFORMATION
The WTSEnumerateSessions function is used to retrieve session information on a Terminal Server. As a result, it requires Windows 2000 Server that is running Terminal Services or Windows NT 4.0 Service Pack 4 (SP4) or later, Terminal Server Edition. This sample enumerates the sessions on a specified Terminal Server and displays the information in the Immediate window of the Visual Basic Integrated Development Environment (IDE).

You can use the WTSEnumerateSessions function to retrieve the session ID, computer name, and session state. The code sample queries the Terminal Server computer that the code runs on, but it can be modified to query sessions that are running on other Terminal Servers. You must have Query Information permission to query a Terminal Server. For more information, see the MSDN documentation in the &quot;References&quot; section.

Step-by-Step Example
<ol> Create a new Standard EXE project in Visual Basic. Form1 is created by default.</li> Add a CommandButton control (Command1) to Form1.</li>  Paste the following code into the code window of Form1: Option Explicit

Private Const WTS_CURRENT_SERVER_HANDLE = 0&

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

Private Type WTS_SESSION_INFO SessionID As Long pWinStationName As Long state As WTS_CONNECTSTATE_CLASS End Type

Private Declare Function WTSEnumerateSessions _ Lib &quot;wtsapi32.dll&quot; Alias &quot;WTSEnumerateSessionsA&quot; ( _   ByVal hServer As Long, ByVal Reserved As Long, _    ByVal Version As Long, ByRef ppSessionInfo As Long, _    ByRef pCount As Long _    ) As Long Private Declare Sub WTSFreeMemory Lib &quot;wtsapi32.dll&quot; ( _   ByVal pMemory As Long)

Private Declare Sub CopyMemory Lib &quot;kernel32&quot; Alias &quot;RtlMoveMemory&quot; ( _   Destination As Any, Source As Any, ByVal length As Long)

Private Declare Function lstrlenA Lib &quot;kernel32&quot; ( _   ByVal lpString As String) As Long

Private Declare Function lstrcpy Lib &quot;kernel32&quot; Alias &quot;lstrcpyA&quot; ( _   ByVal lpString1 As String, ByVal lpString2 As Long) As Long

Private arrWTSSessions As WTS_SESSION_INFO

Private Function GetWTSSessions As WTS_SESSION_INFO Dim RetVal As Long Dim lpBuffer As Long Dim Count As Long Dim p As Long Dim arrSessionInfo As WTS_SESSION_INFO RetVal = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, _                                  0&, _                                   1, _                                   lpBuffer, _                                   Count) If RetVal Then ' WTSEnumerateProcesses was successful. p = lpBuffer ReDim arrSessionInfo(Count - 1) CopyMemory arrSessionInfo(0), ByVal p, _ Count * LenB(arrSessionInfo(0)) ' Free the memory buffer. WTSFreeMemory lpBuffer Else ' Error occurred calling WTSEnumerateProcesses. ' Check Err.LastDllError for error code. MsgBox &quot;An error occurred calling WTSEnumerateProcesses. &quot; & _ &quot;Check the Platform SDK error codes in the MSDN Documentation &quot; & _ &quot;for more information.&quot;, vbCritical, &quot;ERROR &quot; & Err.LastDllError End If   GetWTSSessions = arrSessionInfo End Function

Private Sub Command1_Click Dim i As Integer arrWTSSessions = GetWTSSessions For i = LBound(arrWTSSessions) To UBound(arrWTSSessions) Debug.Print &quot;Session ID: &quot; & arrWTSSessions(i).SessionID Debug.Print &quot;Machine Name: &quot; & _ PointerToStringA(arrWTSSessions(i).pWinStationName) Debug.Print &quot;Connect State: &quot; & arrWTSSessions(i).state Debug.Print &quot;***********&quot; Next i End Sub

Public Function PointerToStringA(ByVal lpStringA As Long) As String Dim nLen As Long Dim sTemp As String

If lpStringA Then nLen = lstrlenA(ByVal lpStringA) If nLen Then sTemp = String(nLen, vbNullChar) lstrcpy sTemp, ByVal lpStringA PointerToStringA = sTemp End If  End If End Function </li> Press the F5 key to run the project.</li> <li>Make sure that the Immediate window is open. If not, press CTRL+G to open it. Click Command1. The session information is displayed in the Immediate window.</li></ol>

<div class="references_section">