VB and VBA Users Source Code: Returning a Terminal Server Session ID
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning a Terminal Server Session ID
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, June 06, 2001
Hits:
1579
Category:
Networks
Article:
The following code can be used to return a terminal server session ID and can also be used detect if Terminal Server is running on the machine. Option Explicit Private Declare Function WTSEnumerateProcesses Lib "wtsapi32.dll" Alias "WTSEnumerateProcessesA" (ByVal hServer As Long, ByVal Reserved As Long, ByVal Version As Long, ByRef ppProcessInfo As Long, ByRef pCount As Long) As Long Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" (ByVal pMemory As Long) Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Private Declare Function GetCurrentProcessId Lib "Kernel32" () As Long Private Type WTS_PROCESS_INFO SessionID As Long ProcessId As Long pProcessName As Long pUserSid As Long End Type 'Purpose : Returns a terminal server session ID 'Inputs : N/A 'Outputs : Returns "0" if not a terminal server, else returns ' the terminal server session ID. 'Author : Andrew Baker 'Date : 25/05/2001 'Notes : Function TerminalServerSessionId() As String Dim lRetVal As Long, lCount As Long, lThisProcess As Long, lThisProcessId As Long Dim lpBuffer As Long, lp As Long, udtProcessInfo As WTS_PROCESS_INFO Const WTS_CURRENT_SERVER_HANDLE = 0& On Error GoTo ErrNotTerminalServer 'Set Default Value TerminalServerSessionId = "0" lThisProcessId = GetCurrentProcessId lRetVal = WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0&, 1, lpBuffer, lCount) If lRetVal Then 'Successful lp = lpBuffer For lThisProcess = 1 To lCount CopyMemory udtProcessInfo, ByVal lp, LenB(udtProcessInfo) If lThisProcessId = udtProcessInfo.ProcessId Then TerminalServerSessionId = CStr(udtProcessInfo.SessionID) Exit For End If lp = lp + LenB(udtProcessInfo) Next 'Free memory buffer WTSFreeMemory lpBuffer End If Exit Function ErrNotTerminalServer: 'The machine is not a Terminal Server On Error GoTo 0 End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder