/// <summary>
        /// Initializes a new instance of the <see cref="TerminalServicesProcess" /> class.
        /// </summary>
        /// <param name="server">TerminalServer instance.</param>
        /// <param name="processInfo">WTS_PROCESS_INFO instance.</param>
        internal TerminalServicesProcess(TerminalServer server, WTS_PROCESS_INFO processInfo)
        {
            this._server      = server;
            this._sessionId   = processInfo.SessionId;
            this._processId   = processInfo.ProcessId;
            this._processName = processInfo.ProcessName;

            if (processInfo.UserSid != IntPtr.Zero)
            {
                this._securityIdentifier = new SecurityIdentifier(processInfo.UserSid);
            }
        }
Ejemplo n.º 2
0
        public static WTS_PROCESS_INFO[] WTSEnumerateProcesses(IntPtr WTS_CURRENT_SERVER_HANDLE, TerminalServer Data)
        {
            IntPtr pProcessInfo = IntPtr.Zero;
            int    processCount = 0;

            if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, ref pProcessInfo, ref processCount))
            {
                return(null);
            }

            const int NO_ERROR = 0;
            const int ERROR_INSUFFICIENT_BUFFER = 122;
            int       err     = NO_ERROR;
            IntPtr    pMemory = pProcessInfo;

            WTS_PROCESS_INFO[] processInfos = new WTS_PROCESS_INFO[processCount];
            for (int i = 0; i < processCount; i++)
            {
                processInfos[i] = (WTS_PROCESS_INFO)Marshal.PtrToStructure(pProcessInfo, typeof(WTS_PROCESS_INFO));
                pProcessInfo    = (IntPtr)((int)pProcessInfo + Marshal.SizeOf(processInfos[i]));

                SessionProcess p = new SessionProcess();
                p.ProcessID   = processInfos[i].ProcessID;
                p.ProcessName = Marshal.PtrToStringAnsi(processInfos[i].ProcessName);

                if (processInfos[i].UserSid != IntPtr.Zero)
                {
                    byte[] Sid = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                    Marshal.Copy(processInfos[i].UserSid, Sid, 0, 14);
                    System.Text.StringBuilder name = new StringBuilder();
                    uint          cchName          = (uint)name.Capacity;
                    SID_NAME_USE  sidUse;
                    StringBuilder referencedDomainName    = new StringBuilder();
                    uint          cchReferencedDomainName = (uint)referencedDomainName.Capacity;
                    if (LookupAccountSid(Data.ServerName, Sid, name, ref cchName, referencedDomainName, ref cchReferencedDomainName, out sidUse))
                    {
                        err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                        if (err == ERROR_INSUFFICIENT_BUFFER)
                        {
                            name.EnsureCapacity((int)cchName);
                            referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);
                            err = NO_ERROR;
                            if (!LookupAccountSid(null, Sid, name, ref cchName, referencedDomainName, ref cchReferencedDomainName, out sidUse))
                            {
                                err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
                            }
                        }


                        p.UserType = sidUse.ToString();
                        p.User     = name.ToString();
                    }
                }
                //string userSID = Marshal.PtrToStringAuto(processInfos[i].UserSid);
                p.SessionID = processInfos[i].SessionID;

                //LookupAccountSid(Data.ServerName,
                //p.User = Marshal.PtrToStringAnsi(processInfos[i].UserSid);
                foreach (Session s in Data.Sessions)
                {
                    if (s.SessionID == p.SessionID)
                    {
                        if (s.Processes == null)
                        {
                            s.Processes = new List <SessionProcess>();
                        }
                        s.Processes.Add(p);
                        break;
                    }
                }
            }



            WTSFreeMemory(pMemory);
            return(processInfos);
        }
Ejemplo n.º 3
0
        private static WTS_PROCESS_INFO[] WTSEnumerateProcesses(IntPtr WTS_CURRENT_SERVER_HANDLE, TerminalServer Data)
        {
            IntPtr pProcessInfo = IntPtr.Zero;
            int processCount = 0;

            if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, ref pProcessInfo, ref processCount))
                return null;

            const int NO_ERROR = 0;
            const int ERROR_INSUFFICIENT_BUFFER = 122;

            IntPtr pMemory = pProcessInfo;
            WTS_PROCESS_INFO[] processInfos = new WTS_PROCESS_INFO[processCount];

            for (int i = 0; i < processCount; i++)
            {
                processInfos[i] = (WTS_PROCESS_INFO) Marshal.PtrToStructure(pProcessInfo, typeof (WTS_PROCESS_INFO));
                pProcessInfo = (IntPtr) ((int) pProcessInfo + Marshal.SizeOf(processInfos[i]));

                SessionProcess p = new SessionProcess
                                       {
                                           ProcessID = processInfos[i].ProcessID,
                                           ProcessName =
                                               Marshal.PtrToStringAnsi(processInfos[i].ProcessName)
                                       };

                if (processInfos[i].UserSid != IntPtr.Zero)
                {
                    byte[] Sid = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                    Marshal.Copy(processInfos[i].UserSid, Sid, 0, 14);
                    StringBuilder name = new StringBuilder();

                    uint cchName = (uint) name.Capacity;
                    SID_NAME_USE sidUse;
                    StringBuilder referencedDomainName = new StringBuilder();

                    uint cchReferencedDomainName = (uint) referencedDomainName.Capacity;

                    if (LookupAccountSid(Data.ServerName, Sid, name, ref cchName, referencedDomainName,
                                         ref cchReferencedDomainName, out sidUse))
                    {
                        int err = Marshal.GetLastWin32Error();

                        if (err == ERROR_INSUFFICIENT_BUFFER)
                        {
                            name.EnsureCapacity((int) cchName);
                            referencedDomainName.EnsureCapacity((int) cchReferencedDomainName);
                            err = NO_ERROR;

                            if (
                                !LookupAccountSid(null, Sid, name, ref cchName, referencedDomainName,
                                                  ref cchReferencedDomainName, out sidUse))
                                err = Marshal.GetLastWin32Error();
                        }

                        p.UserType = sidUse.ToString();
                        p.User = name.ToString();
                    }
                }

                p.SessionID = processInfos[i].SessionID;

                foreach (Session s in Data.Sessions)
                {
                    if (s.SessionId == p.SessionID)
                    {
                        if (s.Processes == null) s.Processes = new List<SessionProcess>();
                        s.Processes.Add(p);
                        break;
                    }
                }
            }

            WTSFreeMemory(pMemory);
            return processInfos;
        }