[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 638
  • Last Modified:

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.
0
dflynn21
Asked:
dflynn21
1 Solution
 
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
 
dflynn21Author 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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now