Sign in to follow this  
Followers 0
psikoh

Retrieve Terminal Server Session IP

3 posts in this topic

Hey,

I'm trying to retrieve a connecting sessions ip address in terminal services for use in other scripts.

I found two threads on this but neither of them have autoit code that works, only vb code (not vbs), and I'm having trouble converting this. This is also my first attempt at using DllCall..

Here is the original vb code

CODE
4. Clay Keller

Aug 11 2004, 8:53 pm show options

Newsgroups: microsoft.public.windows.terminal_services

From: "Clay Keller" <NO_claySPAM_kel...@hotmail.com> - Find messages by this author

Date: Wed, 11 Aug 2004 20:53:03 -0500

Local: Wed, Aug 11 2004 8:53 pm

Subject: Re: Need to capture client IP address

Reply to Author | Forward | Print | Individual Message | Show original | Report Abuse

Here is a little VB module that is a subset of functionality from a larger

Terminal services module I wrote.

This will do what you want.

Attribute VB_Name = "RDPGetMachine"

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'This Code Snippet taken from mdlTSAPI.bas written by Clay Keller

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Option Explicit

'Terminal Services API Declarations

Private Declare Function WTSQuerySessionInformation Lib "wtsapi32.dll" _

Alias "WTSQuerySessionInformationA" _

(ByVal hServer As Long, _

ByVal SessionID As Long, _

ByVal WTSInfoClass As Long, _

ByRef ppBuffer As Long, _

ByRef pBytesReturned As Long _

) As Long

Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" _

(ByVal pMemory As Any)

'Win32 Declarations

Private Declare Sub CopyMemory Lib "kernel32.dll" _

Alias "RtlMoveMemory" _

(ByRef aDestination As Any, _

ByVal lSource As Any, _

ByVal lBytesToCopy As Long _

)

'Terminal Services Constants and Definitions

Public Enum WTS_INFO_CLASS

WTSInitialProgram

WTSApplicationName

WTSWorkingDirectory

WTSOEMId

WTSSessionId

WTSUserName

WTSWinStationName

WTSDomainName

WTSConnectState

WTSClientBuildNumber

WTSClientName

WTSClientDirectory

WTSClientProductId

WTSClientHardwareId

WtsClientAddress

WTSClientDisplay

WTSClientProtocolType

End Enum

Public Type WTS_CLIENT_ADDRESS

AddressFamily As Long 'DWORD This member can be AF_INET, AF_IPX,

AF_NETBIOS, or AF_UNSPEC

Address(20) As Byte ' BYTE Address[20]

End Type

Private Const WTS_CURRENT_SERVER_HANDLE = 0&

Private Const WTS_CURRENT_SESSION = (-1)

Private Function GetStringFromLP(ByVal StrPtr As Long) As String

Dim b As Byte

Dim tempStr As String

Dim bufferStr As String

Dim Done As Boolean

Done = False

Do

' Get the byte/character that StrPtr is pointing to.

CopyMemory b, ByVal StrPtr, 1

If b = 0 Then ' If you've found a null character,

then you're done.

Done = True

Else

tempStr = Chr$(:P ' Get the character for the byte's

value

bufferStr = bufferStr & tempStr 'Add it to the string

StrPtr = StrPtr + 1 ' Increment the pointer to next

byte/char

End If

Loop Until Done

GetStringFromLP = bufferStr

End Function

Public Function GetMachinenameofCurrentSession() As String

Dim RetVal As Long 'Return Value of API Call

Dim lpBuffer As Long 'Buffer to Hold Info Returned

Dim Count As Long 'Length of Buffer info

Dim MachineName As String

'If the function succeeds, the return value is a nonzero value.

'If the function fails, the return value is zero.

'To get extended error information, call GetLastError API.

RetVal = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _

WTS_CURRENT_SESSION, _

WTSClientName, _

lpBuffer, _

Count)

MachineName = GetStringFromLP(lpBuffer)

WTSFreeMemory lpBuffer 'Free the memory used by the buffer.

GetMachinenameofCurrentSession = MachineName

End Function

Public Function GetIPofClientSession() As String

Dim lRet As Long

Dim Ret As Long

Dim wca_Tmp As WTS_CLIENT_ADDRESS

Dim pBytesReturned As Long

Dim ppBuffer As Long

Dim m_IpAddress As String

' IP address information for a session is bogus unless the session

' is a connected remote session.

lRet = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, _

WTS_CURRENT_SESSION, _

WtsClientAddress, _

ppBuffer, _

pBytesReturned)

If lRet Then

CopyMemory wca_Tmp, ppBuffer, pBytesReturned

WTSFreeMemory ppBuffer

m_IpAddress = Join(Array(wca_Tmp.Address(2), _

wca_Tmp.Address(3), _

wca_Tmp.Address(4), _

wca_Tmp.Address(5)), ".")

Else

m_IpAddress = vbNullString

End If

GetIPofClientSession = m_IpAddress

End Function

'End Module

"Mike Pratt" <MikePr...@discussions.microsoft.com> wrote in message

news:24F7ACE4-EAC7-426A-86B2-CF7271A97910@microsoft.com...

And here is my attempt at converting the part i need to autoit:

CODE
local $returned, $addr

$TSCall = DllCall("Wtsapi32.dll", "Int", "WTSQuerySessionInformationW", _

"ptr", "WTS_CURRENT_SERVER_HANDLE", _

"int*", "WTS_CURRENT_SESSION", _

"Int*", "WTSClientAddress", _

"str", $addr, _

"Int*", $returned)

MsgBox(0, '', $TSCall[0])

But as this doesn't return anything I can't do anything with it. Can somebody help me out with this?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

dim $decip[5]

$sid = InputBox("Pleaes enter SID!","-1 = Current Session")
$ip = Call("_GetWTSClientIP",$sid)
if Not @error then
    MsgBox(0,"IP",$ip)
EndIf

Func _GetWTSClientIP($sid)
;       http://msdn.microsoft.com/en-us/library/aa383838(v=VS.85).aspx
;       http://msdn.microsoft.com/en-us/library/aa383857(v=VS.85).aspx

;       BOOL WTSQuerySessionInformation(
;       __in   HANDLE hServer,                  ( 0 = Current)
;       __in   DWORD SessionId,                 (-1 = Current)
;       __in   WTS_INFO_CLASS WTSInfoClass,
;       __out  LPTSTR *ppBuffer,
;       __out  DWORD *pBytesReturned
;       );

    Local $result = DllCall("Wtsapi32.dll","int", "WTSQuerySessionInformationW", "Ptr", 0, "int", $sid, "int", 14, "ptr*", 0, "dword*", 0)
    If @error Or $result[0] = 0 Then Return SetError(1,0,"")
    Local $ip = DllStructGetData(DllStructCreate("byte[" & $result[5] & "]" , $result[4]),1)
    DllCall("Wtsapi32.dll", "int", "WTSFreeMemory", "ptr", $result[4])

; Byte 2 IP
    $ip = StringTrimLeft($ip,14)
    $decip[1] = Dec(StringLeft($ip,2))
    $ip = StringTrimLeft($ip,2)
    $decip[2] = Dec(StringLeft($ip,2))
    $ip = StringTrimLeft($ip,2)
    $decip[3] = Dec(StringLeft($ip,2))
    $ip = StringTrimLeft($ip,2)
    $decip[4] = Dec(StringLeft($ip,2))
    $ip = $decip[1] & "." & $decip[2] & "." & $decip[3] & "." & $decip[4]
    Return $ip
EndFunc

Edited by panic

Share this post


Link to post
Share on other sites

dim $decip[5]

$sid = InputBox("Pleaes enter SID!","-1 = Current Session")
$ip = Call("_GetWTSClientIP",$sid)
if Not @error then
    MsgBox(0,"IP",$ip)
EndIf

Func _GetWTSClientIP($sid)
;       http://msdn.microsoft.com/en-us/library/aa383838(v=VS.85).aspx
;       http://msdn.microsoft.com/en-us/library/aa383857(v=VS.85).aspx

;       BOOL WTSQuerySessionInformation(
;       __in   HANDLE hServer,                  ( 0 = Current)
;       __in   DWORD SessionId,                 (-1 = Current)
;       __in   WTS_INFO_CLASS WTSInfoClass,
;       __out  LPTSTR *ppBuffer,
;       __out  DWORD *pBytesReturned
;       );

    Local $result = DllCall("Wtsapi32.dll","int", "WTSQuerySessionInformationW", "Ptr", 0, "int", $sid, "int", 14, "ptr*", 0, "dword*", 0)
    If @error Or $result[0] = 0 Then Return SetError(1,0,"")
    Local $ip = DllStructGetData(DllStructCreate("byte[" & $result[5] & "]" , $result[4]),1)
    DllCall("Wtsapi32.dll", "int", "WTSFreeMemory", "ptr", $result[4])

; Byte 2 IP
    $ip = StringTrimLeft($ip,14)
    $decip[1] = Dec(StringLeft($ip,2))
    $ip = StringTrimLeft($ip,2)
    $decip[2] = Dec(StringLeft($ip,2))
    $ip = StringTrimLeft($ip,2)
    $decip[3] = Dec(StringLeft($ip,2))
    $ip = StringTrimLeft($ip,2)
    $decip[4] = Dec(StringLeft($ip,2))
    $ip = $decip[1] & "." & $decip[2] & "." & $decip[3] & "." & $decip[4]
    Return $ip
EndFunc

this doesn't work , returned a failure IP

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0