Search code examples
c#windows-servicesip-addressremote-connection

c# getting ip address of user who remotely connect to machine


I am using following piece of c# code in windows service to capture Remote connect and disconnect events.

How I can get the IPaddress of end user who remotely connect to that machine.

protected override void OnSessionChange(SessionChangeDescription changeDescription)
 {
        switch (changeDescription.Reason)
        {
            case SessionChangeReason.RemoteConnect:
                //Remote Connect
                break;
            case SessionChangeReason.RemoteDisconnect:
                //Remote Disconnect
                break;
            default:
                break;
        }
 }

Solution

  • I found related solution in c#, it's using PInvoke WTSQuerySessionInformation with WTS_INFO_CLASS.WTSClientAddress

    For more details you can go here: Grabbing Information of a Terminal Services Session Programmatically

    Note from Selvin: solution doesn't free returned buffer ... you sould call WTSFreeMemory(pAddress)