Get RDP Client Workstation

' VBA Code to get the name of the workstation of the current Remote Desktop user

'Link to MSDN documentation for this windows api

'#WTSQuerySessionInformation (Windows) http://msdn2.microsoft.com/en-us/library/aa383838.aspx

Option Compare Database

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal Source As Long, ByVal Length As Long)

Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" (ByVal pMemory As Long)

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32.dll" Alias "WTSQuerySessionInformationW" _

(ByVal hServer As Long, ByVal sessionId As Long, ByVal wtsInfoClass As Long, ByRef pBuffer As Long, ByRef dwSize As Long) As Long

Public Function GetClientName() As String

Dim pBuffer As Long

Dim dwSize As Long

If WTSQuerySessionInformation(0, -1, 10, pBuffer, dwSize) Then

Dim clientName As String

clientName = String(dwSize, 0)

CopyMemory ByVal StrPtr(clientName), ByVal pBuffer, dwSize

WTSFreeMemory pBuffer

GetClientName = clientName

End If

End Function

Sub Main()

MsgBox GetClientName()

End Sub