#include <Array.au3>
Const $tagWTS_SESSION_INFO = "long SessionID;ptr SessionName;long State"
Func _WTSEnumSessions($hServer)
Local $iResult
$iResult = DllCall("Wtsapi32.dll", "bool", "WTSEnumerateSessionsW", "handle", $hServer, "long", 0, "long", 1, "ptr*", 0, "long*", 0)
Local $aResult[$iResult[5] + 1][7] = [[$iResult[5]]]
TCPStartup()
For $i = 1 To $aResult[0][0]
$tSessionInfo = DllStructCreate($tagWTS_SESSION_INFO, $iResult[4] + ($i - 1) * (@AutoItX64 + 1) * 12)
$aResult[$i][0] = DllStructGetData($tSessionInfo, "SessionID")
$aResult[$i][1] = DllStructGetData($tSessionInfo, "State")
$aResult[$i][2] = _WTSReadStringW( DllStructGetData($tSessionInfo, "SessionName"), 0)
$aResult[$i][3] = _WTSReadStringW(_WTSQuerySessionData($hServer, $aResult[$i][0], 7)) ; Domain name
$aResult[$i][4] = _WTSReadStringW(_WTSQuerySessionData($hServer, $aResult[$i][0], 5)) ; User name
$aResult[$i][5] = _WTSReadStringW(_WTSQuerySessionData($hServer, $aResult[$i][0], 10)) ; Client name
$aResult[$i][6] = _GetHostIPAddresses($aResult[$i][3])
Next
DllCall("Wtsapi32.dll", "none", "WTSFreeMemory", "ptr", $iResult[4])
Return $aResult
EndFunc ;==>_WTSEnumSessions
Func _WTSReadStringW($pStringW, $fFree = 1)
If $pStringW = 0 Then Return ""
Local $iResult
$iResult = DllCall("Kernel32.dll", "long", "lstrlenW", "ptr", $pStringW)
$iResult = DllCall("Kernel32.dll", "none", "RtlMoveMemory", "wstr", "", "ptr", $pStringW, "long", $iResult[0] * 2 + 2)
If $fFree Then DllCall("Wtsapi32.dll", "none", "WTSFreeMemory", "ptr", $pStringW)
Return $iResult[1]
EndFunc ;==>_WTSReadStringW
Func _WTSQuerySessionData($hServer, $iSessionID, $iInfoClass)
Local $iResult
$iResult = DllCall("Wtsapi32.dll", "bool", "WTSQuerySessionInformationW", "ptr", $hServer, "long", $iSessionID, "long", $iInfoClass, "ptr*", 0, "long*", 0)
Return SetError(0, $iResult[5], $iResult[4])
EndFunc ;==>_WTSQuerySessionData
Func _GetHostIPAddresses($sHostName)
Local $iResult
$iResult = DllCall("Ws2_32.dll", "ptr", "gethostbyname", "str", $sHostName)
If $iResult[0] = 0 Then Return ""
Local $tHostent = DllStructCreate("ptr name;ptr aliases;word addrtype;word length;ptr addrlist", $iResult[0])
Local $pAddrList = DllStructGetData($tHostent, "addrlist")
Local $tAddress, $pAddress, $tLONG, $iResult, $sResult
For $i = 0 To 1023
$tAddress = DllStructCreate("ptr address", $pAddrList + $i * ((@AutoItX64 + 1) * 4))
$pAddress = DllStructGetData($tAddress, "address")
If $pAddress = 0 Then ExitLoop
$tLONG = DllStructCreate("long LONG", $pAddress)
$iResult = DllCall("Ws2_32.dll", "str", "inet_ntoa", "long", DllStructGetData($tLONG, "LONG"))
$sResult &= $iResult[0] & ", "
Next
Return StringTrimRight($sResult, 2)
EndFunc ;==>_GetHostIPAddresses
$aSession = _WTSEnumSessions(0)
_ArrayDisplay($aSession)
|