Example #1
0
        public static List <string> GetInteractiveUserList()
        {
            List <string> result = new List <string>();

            IntPtr sessionInfoList = IntPtr.Zero;
            int    sessionCount    = 0;
            int    retVal          = SafeNativeMethods.WTSEnumerateSessions(SafeNativeMethods.WTS_CURRENT_SERVER_HANDLE, 0, 1, ref sessionInfoList, ref sessionCount);

            if (retVal != 0)
            {
                int dataSize       = Marshal.SizeOf(typeof(SafeNativeMethods.WTS_SESSION_INFO));
                int currentSession = (int)sessionInfoList;

                for (int x = 0; x < sessionCount; x++)
                {
                    SafeNativeMethods.WTS_SESSION_INFO sessionInfo =
                        (SafeNativeMethods.WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)currentSession, typeof(SafeNativeMethods.WTS_SESSION_INFO));
                    currentSession += dataSize;

                    uint   bytes      = 0;
                    IntPtr userInfo   = IntPtr.Zero;
                    IntPtr domainInfo = IntPtr.Zero;
                    bool   sResult    = SafeNativeMethods.WTSQuerySessionInformation(SafeNativeMethods.WTS_CURRENT_SERVER_HANDLE, sessionInfo.SessionID, SafeNativeMethods.WTS_INFO_CLASS.WTSUserName, out userInfo, out bytes);
                    if (!sResult)
                    {
                        int Win32ErrorResult = Marshal.GetLastWin32Error();
                        SafeNativeMethods.WTSFreeMemory(sessionInfoList);
                        throw new Win32Exception(Win32ErrorResult, "WTSQuerySessionInformation");
                    }
                    string user = Marshal.PtrToStringAnsi(userInfo);
                    SafeNativeMethods.WTSFreeMemory(userInfo);

                    sResult = SafeNativeMethods.WTSQuerySessionInformation(SafeNativeMethods.WTS_CURRENT_SERVER_HANDLE, sessionInfo.SessionID, SafeNativeMethods.WTS_INFO_CLASS.WTSDomainName, out domainInfo, out bytes);
                    if (!sResult)
                    {
                        int Win32ErrorResult = Marshal.GetLastWin32Error();
                        SafeNativeMethods.WTSFreeMemory(sessionInfoList);
                        throw new Win32Exception(Win32ErrorResult, "WTSQuerySessionInformation");
                    }

                    string domain = Marshal.PtrToStringAnsi(domainInfo);
                    SafeNativeMethods.WTSFreeMemory(domainInfo);

                    if (!string.IsNullOrEmpty(domain))
                    {
                        result.Add(string.Format("{0}\\{1}", domain, user));
                    }
                    else if (!string.IsNullOrEmpty(user))
                    {
                        result.Add(user);
                    }
                }

                SafeNativeMethods.WTSFreeMemory(sessionInfoList);
            }

            return(result);
        }
Example #2
0
        public static bool IsSessionLoggedOFF(int session)
        {
            SafeNativeMethods.WTS_SESSION_INFO sessionInfo = new SafeNativeMethods.WTS_SESSION_INFO();
            // if WTSQuerySessionInformation returns false than the session is already closed
            // WTSQuerySessionInformation returns zero if the session is logged off.
            sessionInfo.State = SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSReset;
            uint bytes = 0;
            IntPtr userInfo = IntPtr.Zero;

            bool sResult = SafeNativeMethods.WTSQuerySessionInformation(SafeNativeMethods.WTS_CURRENT_SERVER_HANDLE, session, SafeNativeMethods.WTS_INFO_CLASS.WTSConnectState, out userInfo, out bytes);
            if (sResult)
            {
                int lData = Marshal.ReadInt32(userInfo);
                sessionInfo.State = (SafeNativeMethods.WTS_CONNECTSTATE_CLASS)Enum.ToObject(typeof(SafeNativeMethods.WTS_CONNECTSTATE_CLASS), lData);
                /*
                switch (sessionInfo.State)
                {
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSActive:
                        ret = "WTSActive";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSConnected:
                        ret = "WTSConnected";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSConnectQuery:
                        ret = "WTSConnectQuery";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSDisconnected:
                        ret = "WTSDisconnected";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSDown:
                        ret = "WTSDown";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSIdle:
                        ret = "WTSIdle";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSInit:
                        ret = "WTSInit";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSListen:
                        ret = "WTSListen";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSReset:
                        ret = "WTSReset";
                        break;
                    case SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSShadow:
                        ret = "WTSShadow";
                        break;
                }
                */
            }
            SafeNativeMethods.WTSFreeMemory(userInfo);
            return (sessionInfo.State == SafeNativeMethods.WTS_CONNECTSTATE_CLASS.WTSReset) ? true : false;
        }