public static extern int WinStationQueryInformationRemoteAddress( IntPtr hServer, int sessionId, WINSTATIONINFOCLASS information, ref WINSTATIONREMOTEADDRESS buffer, int bufferLength, out int returnedLength);
public static EndPoint QuerySessionInformationForEndPoint(ITerminalServerHandle server, int sessionId) { int retLen; var remoteAddress = new WINSTATIONREMOTEADDRESS(); if (NativeMethods.WinStationQueryInformationRemoteAddress( server.Handle, sessionId, WINSTATIONINFOCLASS.WinStationRemoteAddress, ref remoteAddress, Marshal.SizeOf(typeof(WINSTATIONREMOTEADDRESS)), out retLen) != 0) { var ipAddress = ExtractIPAddress(remoteAddress.Family, remoteAddress.Address); int port = NativeMethods.ntohs((ushort)remoteAddress.Port); return(ipAddress == null ? null : new IPEndPoint(ipAddress, port)); } throw new Win32Exception(); }