public static TerminalServer GetSessions(string serverName, Kohl.Framework.Security.Credential credential)
        {
            TerminalServer terminalServer = new TerminalServer {
                ServerName = serverName
            };

            Kohl.Framework.Security.Impersonator impersonator = null;

            // Start impersonation if we requested to do so ...
            try
            {
                if (credential != null)
                {
                    impersonator = new Kohl.Framework.Security.Impersonator(credential);
                }
            }
            catch (Exception ex)
            {
                Log.Warn("Error impersonating RDP session enumerator. Trying to query RDP session details without impersonation ...", ex);
            }

            // Open a WTS connection to the server to be able to get details about the sessions and processes running on it
            IntPtr ptrOpenedServer = IntPtr.Zero;

            try
            {
                ptrOpenedServer = WTSOpenServer(terminalServer.ServerName);
            }
            catch (Exception ex)
            {
                Log.Error("Error opening WTS server connection. Aborting to query RDP sessions.", ex);
                return(terminalServer);
            }

            if (ptrOpenedServer == IntPtr.Zero)
            {
                terminalServer.IsTerminalServer = false;
                return(terminalServer);
            }

            terminalServer.ServerPointer    = ptrOpenedServer;
            terminalServer.IsTerminalServer = true;

            // Try to get information about the sessions and the clients connected to it.
            GetClientInfos(terminalServer);

            // Try to get information about the server's processes.
            GetProcessInfos(terminalServer);

            if (ptrOpenedServer != IntPtr.Zero)
            {
                WTSCloseServer(ptrOpenedServer);
            }

            if (impersonator != null)
            {
                impersonator.Dispose();
            }

            return(terminalServer);
        }
        public static TerminalServer GetSessions(string serverName, Kohl.Framework.Security.Credential credential)
        {
            TerminalServer terminalServer = new TerminalServer {ServerName = serverName};

            Kohl.Framework.Security.Impersonator impersonator = null;

            // Start impersonation if we requested to do so ...
            try
            {
                if (credential != null)
                    impersonator = new Kohl.Framework.Security.Impersonator(credential);
            }
            catch (Exception ex)
            {
                Log.Warn("Error impersonating RDP session enumerator. Trying to query RDP session details without impersonation ...", ex);
            }

            // Open a WTS connection to the server to be able to get details about the sessions and processes running on it
            IntPtr ptrOpenedServer = IntPtr.Zero;
            try
            {
                ptrOpenedServer = WTSOpenServer(terminalServer.ServerName);
            }
            catch (Exception ex)
            {
                Log.Error("Error opening WTS server connection. Aborting to query RDP sessions.", ex);
                return terminalServer;
            }

            if (ptrOpenedServer == IntPtr.Zero)
            {
                terminalServer.IsTerminalServer = false;
                return terminalServer;
            }

            terminalServer.ServerPointer = ptrOpenedServer;
            terminalServer.IsTerminalServer = true;

            // Try to get information about the sessions and the clients connected to it.
            GetClientInfos(terminalServer);

            // Try to get information about the server's processes.
            GetProcessInfos(terminalServer);

            if (ptrOpenedServer != IntPtr.Zero)
                WTSCloseServer(ptrOpenedServer);

            if (impersonator != null)
                impersonator.Dispose();

            return terminalServer;
        }