Возвращает текущий IP-адрес клиента сеанса RDP или пустую строку, если не выполняется в сеансе RDP.
IP-адрес клиента - это один из локальных IP-адресов клиентского компьютера, сообщаемый mstsc.exe
при установлении соединения, и он может отличаться от реальных IP-адресов, наблюдаемых при netstat
или аналогичных.
Option Explicit
'--- for WTSQuerySessionInformation
Private Const WTS_CURRENT_SERVER_HANDLE As Long = 0
Private Const WTS_CURRENT_SESSION As Long = -1
Private Const WTSClientAddress As Long = 14
'--- for AddressFamily
Private Const AF_INET As Long = 2
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Declare Function WTSQuerySessionInformation Lib "wtsapi32" Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, ByVal SessionId As Long, ByVal WtsInfoClass As Long, ppBuffer As Long, pBytesReturned As Long) As Long
Private Declare Sub WTSFreeMemory Lib "wtsapi32" (ByVal pMemory As Long)
Private Type WTS_CLIENT_ADDRESS
AddressFamily As Long
Address(0 To 19) As Byte
End Type
Private Function GetSessionClientIp() As String
Dim uAddress As WTS_CLIENT_ADDRESS
Dim lPtr As Long
Dim lSize As Long
Call WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientAddress, lPtr, lSize)
If lSize >= LenB(uAddress) Then
Call CopyMemory(uAddress, ByVal lPtr, LenB(uAddress))
End If
Call WTSFreeMemory(lPtr)
If uAddress.AddressFamily = AF_INET Then
GetSessionClientIp = uAddress.Address(2) & "." & uAddress.Address(3) & "." & uAddress.Address(4) & "." & uAddress.Address(5)
End If
End Function
Private Sub Form_Load()
MsgBox "GetSessionClientIp=" & GetSessionClientIp(), vbExclamation
End Sub