/// <summary>
 /// Initializes a new instance of the <see cref="T:Oerlikon.Balzers.Rdp.Interfaces.TerminalSessionInfo"/> structure.
 /// </summary>
 /// <param name="SessionId">Only used to force an initialization of members.</param>
 public TerminalSessionInfo(int SessionId)
 {
     this.SessionInfo                      = new WTS_SESSION_INFO();
     this.SessionInfo.iSessionID           = SessionId;
     this.SessionInfo.sWinsWorkstationName = String.Empty;
     this.UserName                     = String.Empty;
     this.Domain                       = String.Empty;
     this.ClientIPAddress              = String.Empty;
     this.ProtocolType                 = WTS_CLIENT_PROTOCOL_TYPE.UNKNOWN;
     this.ClientMachineName            = String.Empty;
     this.ClientInfo                   = new WTS_CLIENT_INFO();
     this.ClientInfo.ClientMachineName = String.Empty;
     this.ClientInfo.Domain            = String.Empty;
     this.ClientInfo.UserName          = String.Empty;
     this.ClientInfo.WorkDirectory     = String.Empty;
     this.ClientInfo.InitialProgram    = String.Empty;
     this.ClientInfo.ClientDirectory   = String.Empty;
     this.ClientInfo.DeviceId          = String.Empty;
     this.WtsInfo                      = new WTSINFO();
     this.WtsInfo.Domain               = String.Empty;
     this.WtsInfo.UserName             = String.Empty;
     this.WtsInfo.WinStationName       = String.Empty;
 }
        /// <param name="pServer"></param>
        /// <param name="pSessionInfo"></param>
        private TerminalSessionInfo GetSessionInfo(IntPtr pServer, IntPtr pSessionInfo)
        {
            int  iCurrent  = (int)pSessionInfo;
            uint iReturned = 0;
            WTS_CLIENT_ADDRESS       oClientAddres = new WTS_CLIENT_ADDRESS();
            WTS_CLIENT_DISPLAY       oClientDisplay = new WTS_CLIENT_DISPLAY();
            WTS_CLIENT_PROTOCOL_TYPE oClientProtocol = WTS_CLIENT_PROTOCOL_TYPE.UNKNOWN;
            WTS_CLIENT_INFO          oClientInfo = new WTS_CLIENT_INFO();
            WTSINFO             oWtsInfo = new WTSINFO();
            string              sIPAddress = string.Empty;
            string              sUserName = string.Empty, sClientName = string.Empty;
            string              sDomain = string.Empty;
            string              sClientApplicationDirectory = string.Empty;
            TerminalSessionInfo retval  = new TerminalSessionInfo(0);
            // Get session info structure
            WTS_SESSION_INFO oSessionInfo = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)iCurrent, typeof(WTS_SESSION_INFO));
            //Get the IP address of the Terminal Services User
            IntPtr pAddress = IntPtr.Zero;

            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientAddress, out pAddress, out iReturned) == true)
            {
                oClientAddres = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(pAddress, oClientAddres.GetType());
                sIPAddress    = oClientAddres.bAddress[2] + "." + oClientAddres.bAddress[3] + "." + oClientAddres.bAddress[4] + "." + oClientAddres.bAddress[5];
            }
            //Get the User Name of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSUserName, out pAddress, out iReturned) == true)
            {
                sUserName = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get the Client Name of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientName, out pAddress, out iReturned) == true)
            {
                sClientName = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get the Domain Name of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSDomainName, out pAddress, out iReturned) == true)
            {
                sDomain = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get the Display Information  of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDisplay, out pAddress, out iReturned) == true)
            {
                oClientDisplay = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure(pAddress, oClientDisplay.GetType());
            }
            //Get the Application Directory of the Terminal Services User
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDirectory, out pAddress, out iReturned) == true)
            {
                sClientApplicationDirectory = Marshal.PtrToStringAnsi(pAddress);
            }
            //Get protocol type
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientProtocolType, out pAddress, out iReturned) == true)
            {
                oClientProtocol = (WTS_CLIENT_PROTOCOL_TYPE)Marshal.ReadInt16(pAddress);
            }
            //Get client info
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientInfo, out pAddress, out iReturned) == true)
            {
                oClientInfo = (WTS_CLIENT_INFO)Marshal.PtrToStructure(pAddress, oClientInfo.GetType());
                //sUserName = String.IsNullOrEmpty(sUserName) ? oClientInfo.UserName : sUserName;
            }
            //Get WTS info
            if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSSessionInfo, out pAddress, out iReturned) == true)
            {
                oWtsInfo = (WTSINFO)Marshal.PtrToStructure(pAddress, oWtsInfo.GetType());
            }
            // fill result
            retval.SessionInfo = oSessionInfo;
            //retval.SessionInfo.oState = oSessionInfo.oState;
            //retval.SessionInfo.sWinsWorkstationName = oSessionInfo.sWinsWorkstationName == null ? "" : oSessionInfo.sWinsWorkstationName;
            retval.UserName          = sUserName == null ? "" : sUserName;
            retval.ClientMachineName = sClientName == null ? "" : sClientName;
            retval.ClientIPAddress   = sIPAddress == null ? "" : sIPAddress;
            retval.Domain            = sDomain == null ? "" : sDomain;
            retval.ProtocolType      = oClientProtocol;
            retval.ClientInfo        = oClientInfo;
            retval.WtsInfo           = oWtsInfo;
            return(retval);
        }