【解決方法】リモートにログインしているユーザーからコンピューター名を取得します。


こんにちは、私は多くの人々が同じコンピュータを同じユーザーとリモート接続している会社で働いています。

ユーザーがリモート操作を行ったコンピューターの名前を取得したいと考えています。

ログオンしているユーザーがどのコンピュータにリモート接続しているかを確認するための小さなプログラムを作成したいと考えています。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 コマンドを実行して出力を確認できます。 次に、これを行で分割して各エントリを取得し、さらにタブで分割して各列を取得します。 3 番目の列に注目してください。名前が特定できない場合は、コンピューター名:ポートまたは ipaddress:ポートが含まれます。

解決策 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をコピーしました