1

I have a legacy application in Visual Basic 6 that uses the Environ() function to get the environment variable CLIENTNAME.

The function returns no value. However, if I use the command "SET CLIENTNAME" from the shell, I get the correct value.

If the user is given admin privileges, Environ() works ok, returning the correct value, suggesting is a security problem.

I would appreciate any suggestion.

JGarate
  • 13
  • 2
  • 2
    Where should CLIENTNAME be set - in control panel, system, advanced, environment, or by something else? Can you run your VB app from a shell where you can see it set? – Rup Sep 12 '18 at 14:25
  • The applicacion is running in a Terminal Server client. As far as I know CLIENTNAME is loaded automatically and contains the client computer name, whereas COMPUTERNAME contains the name of the server. – JGarate Sep 12 '18 at 17:56

1 Answers1

1

You can use API calls to get current RDP session client name like this

Option Explicit

'--- for WTSQuerySessionInformation
Private Const WTS_CURRENT_SERVER_HANDLE             As Long = 0
Private Const WTS_CURRENT_SESSION                   As Long = -1
Private Const WTSClientName                         As Long = 10

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Declare Function WTSQuerySessionInformation Lib "wtsapi32" Alias "WTSQuerySessionInformationA" (ByVal hServer As Long, ByVal SessionId As Long, ByVal WtsInfoClass As Long, ppBuffer As Long, pBytesReturned As Long) As Long
Private Declare Sub WTSFreeMemory Lib "wtsapi32" (ByVal pMemory As Long)

Private Function GetSessionClientName() As String
    Dim lPtr            As Long
    Dim lSize           As Long

    Call WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientName, lPtr, lSize)
    If lPtr <> 0 Then
        GetSessionClientName = String$(lSize - 1, 0)
        Call CopyMemory(ByVal GetSessionClientName, ByVal lPtr, lSize - 1)
        Call WTSFreeMemory(lPtr)
    End If
End Function

Private Sub Form_Load()
    MsgBox "GetSessionClientName=[" & GetSessionClientName() & "]", vbExclamation
End Sub
wqw
  • 11,771
  • 1
  • 33
  • 41
  • Thanks, this solution works. However, I still don't know why Environ() stopped working. – JGarate Oct 05 '18 at 20:53
  • @JGarate Did anyone re-install the Session Host server w/ Windows Server 2016 perhaps? Could be automatic appcompat shims for VB6 apps that are not marked TSAware in the newer OS versions/service packs for instance. – wqw Oct 07 '18 at 08:22