在 Visual Basic 6 中,Environ() 函数 returns CLIENTNAME 的空值

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

我在 Visual Basic 6 中有一个遗留应用程序,它使用 Environ() 函数获取环境变量 CLIENTNAME。

函数returns没有值。但是,如果我使用 shell 中的命令 "SET CLIENTNAME",我会得到正确的值。

如果用户被授予管理员权限,Environ() 工作正常,返回正确的值,提示存在安全问题。

如有任何建议,我将不胜感激。

您可以像这样使用 API 调用来获取当前 RDP 会话客户端名称

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