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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
dflynn21Director 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.