Search code examples
windowsvb6environ

In Visual Basic 6, Environ() function returns null value for CLIENTNAME


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.


Solution

  • 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