Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi, im working in a compant that alot of people are remote connecting the same computer with the same user.

I want to get the name of the computer that the user did the remote from.

I want to make a small program to check which computer the logged on user remoted to the computer and It doesn't matter if c# or java script or vbscript.

Also, if possible, to check the computer name of user logged and installed some program.
I mean that if i check installed program B, i get the computer name of the logged in user that installed it.

Thanks, Imbaro.
Posted

Without having any information on your application I can only assume that you want to run an external program and find the DNS names of any connected computers. I'll go through the steps I would take:

First, I'd start the process "netstat" with the command line arguments "-a". Redirect the output so you can read it:

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();


Now you have the data for everything connected to the computer (in and out). You can run the netstat command in a console window to see the output. You would then split this by lines to get each entry, and then split it again by tabs to get each column. You are interested in the third column, which has the computername:port or ipaddress:port if the name can't be determined.
 
Share this answer
 
Comments
imbaro 18-Dec-13 3:02am    
How can i check it on a remote comouter?
Ron Beyer 18-Dec-13 8:30am    
So you are trying to get who's logged into the remote computer from a remote computer? Can you install any software (like the above) on the remote computer? Do you have admin privileges on the remote computer?
Hi :)

Not my code, just adapted for x64 versions (maybe helps someone so many years later). Is just for getting current RDP (Remote Desktop) client hostname.

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900