从远程登录用户获取计算机名称。


你好,我在一家公司工作,很多人都与同一用户远程连接同一台计算机。

我想获取用户进行远程操作的计算机的名称。

我想制作一个小程序来检查登录用户远程连接到哪台计算机,无论是 c#、java 脚本还是 vbscript。

另外,如果可能的话,检查用户登录并安装某些程序的计算机名称。
我的意思是,如果我检查已安装的程序 B,我会得到安装它的登录用户的计算机名称。

谢谢,因巴罗。

解决方案1

如果没有关于您的应用程序的任何信息,我只能假设您想要运行外部程序并查找任何连接的计算机的 DNS 名称。 我将完成我将采取的步骤:

首先,我使用命令行参数“-a”启动进程“netstat”。 重定向输出以便您可以阅读它:

C#
Process netStat = new Process();
netStat.StartInfo.UseShellExecute = true;
netStat.StartInfo.CreateNoWindow = true;
netStat.StartInfo.FileName = "netstat";
netStat.StartInfo.Arguments ="-a";
netStat.StartInfo.RedirectStandardOutput = true;

netStat.Start();   //Can take several seconds to execute, good use for BackgroundWorker

string output = netStat.StandardOutput.ReadToEnd();

现在您已拥有连接到计算机的所有内容(输入和输出)的数据。 您可以在控制台窗口中运行 netstat 命令来查看输出。 然后,您可以按行拆分以获取每个条目,然后再次按选项卡拆分以获取每个列。 您对第三列感兴趣,其中包含计算机名称:端口或 IP 地址:端口(如果无法确定名称)。

解决方案3

你好 :)

不是我的代码,只是针对 x64 版本进行了修改(也许对多年后的某人有帮助)。 仅用于获取当前 RDP(远程桌面)客户端主机名。

Private Declare PtrSafe Sub WTSFreeMemory Lib "wtsapi32.dll" (ByVal pMemory As Any)
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongLong)
Private Declare PtrSafe Function WTSQuerySessionInformationA Lib "wtsapi32.dll" (ByVal hServer As LongLong, ByVal sessionId As LongLong, ByVal wtsInfoClass As LongLong, ByRef pBuffer As LongLong, ByRef dwSize As LongLong) As LongLong

 Public Function GetMachinenameofCurrentSession() As String
        Dim RetVal As LongLong      'Return Value of API Call
        Dim lpBuffer As LongLong    'Buffer to Hold Info Returned
        Dim Count As LongLong       'Length of Buffer info
        Dim MachineName As String
        'If the function succeeds, the return value is a nonzero value.
        'If the function fails, the return value is zero.
        'To get extended error information, call GetLastError API.
        RetVal = WTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, _
        WTS_CURRENT_SESSION, _
        WTSClientName, _
        lpBuffer, _
        Count)
        
        MachineName = GetStringFromLP(lpBuffer)
        WTSFreeMemory lpBuffer 'Free the memory used by the buffer.
        GetMachinenameofCurrentSession = MachineName
End Function

Private Function GetStringFromLP(ByVal StrPtr As LongLong) As String
    
    Dim b As Byte
    Dim tempStr As String
    Dim bufferStr As String
    Dim Done As Boolean
    Done = False
    Do
        ' Get the byte/character that StrPtr is pointing to.
        CopyMemory b, ByVal StrPtr, 1
        If b = 0 Then ' If you've found a null character,
            Done = True
        Else
            tempStr = Chr$(b)
            bufferStr = bufferStr & tempStr 'Add it to the string
            StrPtr = StrPtr + 1 ' Increment the pointer to next
        End If
    Loop Until Done
    
    GetStringFromLP = bufferStr

End Function

コメント

タイトルとURLをコピーしました