Finde IP adressen på den lokale maskine en session køres fra.
Hej alle,Jeg har fundet nedenstående vb-scribt som løser lige precis det problem jeg sidder fast i. Jeg skal forsøge at finde den lokale IP adresse som en computer har, når den starter en citrix eller en terminal server session op. Det skal køres som et startup/login script og bruges f.eks til at når en bruger logger på en citrix, at finde den IP adresse hans egen computer har, for at se i hvilket IP scope han er og dermed mappe den korrekte printer til hans maskine og andre lokationsbestemte ting.
Men jeg kan ikke få dette script til at virke, jeg indrømmer gerne at jeg er en klovn til at kode og løsningen måske er simpel? Men jeg har nedenstående stående i et en RDPGetMachine.vbs og hvis jeg dobbeltklikker, giver den en fejl og hvis jeg afvikler den med cscript.exe eller vscript.exe giver den også fejl, er der nogle kloge hoveder der kan hjælpe en fortabt vb sjæl :)
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$(b) ' 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
