Return Clients Hostname on a Terminal Server Using Visual Basic 2005

Posted on 2008-11-12
Last Modified: 2013-11-26
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.
Question by:dflynn21
    LVL 7

    Expert Comment

    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.
    LVL 1

    Accepted Solution

      We ended up solving the issue.   Here is the code that we used.
      Private Enum WTS_INFO_CLASS
        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)

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now