Return Clients Hostname on a Terminal Server Using Visual Basic 2005

We are designing an application that requires us to return the hostname of the Client computer that is connecting to our Terminal Server.   Using Visual Basic 2005, what is the best approach for returning this information.


ClientA  ------------------à TSServerB

We are trying to return ClientA using a function that is running on a TSSERVERB Session.
LVL 1
dflynn21Director of ITAsked:
Who is Participating?
 
dflynn21Connect With a Mentor Director of ITAuthor Commented:
  We ended up solving the issue.   Here is the code that we used.
 
  Private Enum WTS_INFO_CLASS
        WTSInitialProgram
        WTSApplicationName
        WTSWorkingDirectory
        WTSOEMId
        WTSSessionId
        WTSUserName
        WTSWinStationName
        WTSDomainName
        WTSConnectState
        WTSClientBuildNumber
        WTSClientName
        WTSClientDirectory
        WTSClientProductId
        WTSClientHardwareId
        WTSClientAddress
        WTSClientDisplay
        WTSClientProtocolType
    End Enum
 
    Private Declare Auto Function WTSQuerySessionInformation Lib "wtsapi32.dll" ( _
         ByVal hServer As Int32, _
         ByVal SessionId As Int32, _
         ByVal InfoClass As WTS_INFO_CLASS, _
         ByRef ppBuffer As IntPtr, _
         ByRef pCount As Int32) As Int32
 
    Declare Function ProcessIdToSessionId Lib "kernel32.dll" (ByVal dwProcessId As Int32, ByRef pSessionId As Int32) As Int32
 
    Declare Function GetCurrentProcessId Lib "kernel32" () As Integer
 
 
 
       ' return computer name on the Terminal Servers
        Dim dwSessionID As Integer = -1
        Dim szClientName As String
        Dim Buffer As System.IntPtr = IntPtr.Zero
        Dim dwClintNameLen As Integer = 0
 
        ProcessIdToSessionId(GetCurrentProcessId(), dwSessionID)
 
        WTSQuerySessionInformation(0, dwSessionID, WTS_INFO_CLASS.WTSClientName, Buffer, dwClintNameLen)
 
        szClientName = Marshal.PtrToStringUni(Buffer, dwClintNameLen / 2 + 1)
 
        'MsgBox(szClientName)

Open in new window

0
 
rheitzmanCommented:
One of the Environment variables is CLIENTNAME.

To verify open a command window (RUN cmd) and type SET.

Not sure what tool you are using but most have a way to get to the environment variables. If you version doesn't provide access the Windows Scripting Host can be referencee and the WSHShell.ExpandEnvironmentStrings used:

    Dim wsh As New WshShell
    Debug.Print wsh.ExpandEnvironmentStrings("%TEMP%")

replace %TEMP% with variable you need.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.