Ejemplo n.º 1
0
        public static WTS_USER_INFO[] EnumerateUsers(string serverName = null)
        {
            var list = new List <WTS_USER_INFO>();

            serverName = serverName ?? Environment.MachineName;

            var serverHandle = OpenServer(serverName);

            try
            {
                var sessionInfoPtr = IntPtr.Zero;
                var sessionCount   = 0;

                var retVal         = WtsApi32.WTSEnumerateSessions(serverHandle, 0, 1, ref sessionInfoPtr, ref sessionCount);
                var dataSize       = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
                var currentSession = (long)sessionInfoPtr;

                if (retVal != 0)
                {
                    for (int i = 0; i < sessionCount; i++)
                    {
                        var si = (WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)currentSession, typeof(WTS_SESSION_INFO));

                        currentSession += dataSize;

                        IntPtr userPtr;
                        IntPtr domainPtr;
                        uint   bytes;

                        WtsApi32.WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSUserName, out userPtr, out bytes);
                        WtsApi32.WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSDomainName, out domainPtr, out bytes);

                        var userName   = Marshal.PtrToStringAnsi(userPtr);
                        var domainName = Marshal.PtrToStringAnsi(domainPtr);

                        var info = new WTS_USER_INFO
                        {
                            State     = si.State,
                            Domain    = domainName,
                            UserName  = userName,
                            SessionID = si.SessionID
                        };

                        list.Add(info);
                    }

                    WtsApi32.WTSFreeMemory(sessionInfoPtr);
                }
            }
            finally
            {
                CloseServer(serverHandle);
            }

            return(list.ToArray());
        }
Ejemplo n.º 2
0
 public static void CloseServer(IntPtr ServerHandle)
 {
     WtsApi32.WTSCloseServer(ServerHandle);
 }
Ejemplo n.º 3
0
 public static IntPtr OpenServer(String Name)
 {
     IntPtr server = WtsApi32.WTSOpenServer(Name); return(server);
 }