Abstractions.WindowsApi.pInvokes.GetSessionContext C# (CSharp) 메소드

GetSessionContext() 공개 정적인 메소드

returns a Dictionary of SessionID Keys and a List of usernames based on context in which a program is running. a program running as administrator will add administrator to the list
public static GetSessionContext ( ) : List>.Dictionary
리턴 List>.Dictionary
        public static Dictionary<int, List<string>> GetSessionContext()
        {
            Dictionary<int, List<string>> ret = new Dictionary<int, List<string>>();

            foreach (Process process in Process.GetProcesses())
            {
                try { var test = process.Handle; }
                catch { continue; }

                //Console.WriteLine("process:{0}", process.ProcessName);
                IntPtr tokenHandle;
                // Get the Process Token
                if (!SafeNativeMethods.OpenProcessToken(process.Handle, SafeNativeMethods.TokenAccessRights.TOKEN_READ, out tokenHandle))
                {
                    LibraryLogging.Error("OpenProcessToken error:{0}", LastError());
                    return ret;
                }

                int TokenInfLength = 0;
                if (!SafeNativeMethods.GetTokenInformation(tokenHandle, SafeNativeMethods.TOKEN_INFORMATION_CLASS.TokenUser, IntPtr.Zero, TokenInfLength, out TokenInfLength))
                {
                    int error = Marshal.GetLastWin32Error();
                    if (error != 122 /*ERROR_INSUFFICIENT_BUFFER*/)
                    {
                        LibraryLogging.Error("GetTokenInformation error:{0} {1}", error, LastError());
                    }
                    else
                    {
                        IntPtr TokenInformation = Marshal.AllocHGlobal(TokenInfLength);
                        if (!SafeNativeMethods.GetTokenInformation(tokenHandle, SafeNativeMethods.TOKEN_INFORMATION_CLASS.TokenUser, TokenInformation, TokenInfLength, out TokenInfLength))
                        {
                            LibraryLogging.Error("GetTokenInformation error:{0}", LastError());
                        }
                        else
                        {
                            SafeNativeMethods.TOKEN_USER TokenUser = (SafeNativeMethods.TOKEN_USER)Marshal.PtrToStructure(TokenInformation, typeof(SafeNativeMethods.TOKEN_USER));

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


                            bool WinAPI = SafeNativeMethods.LookupAccountSid(null, TokenUser.User.Sid, name, ref cchName, referencedDomainName, ref cchReferencedDomainName, out sidUse);
                            if (!WinAPI)
                            {
                                error = Marshal.GetLastWin32Error();
                                if (error == 122 /*ERROR_INSUFFICIENT_BUFFER*/)
                                {
                                    name.EnsureCapacity((int)cchName);
                                    referencedDomainName.EnsureCapacity((int)cchReferencedDomainName);
                                    WinAPI = SafeNativeMethods.LookupAccountSid(null, TokenUser.User.Sid, name, ref cchName, referencedDomainName, ref cchReferencedDomainName, out sidUse);
                                    if (!WinAPI)
                                    {
                                        LibraryLogging.Error("LookupAccountSid error:{0}", LastError());
                                    }
                                }
                                else
                                {
                                    LibraryLogging.Error("LookupAccountSid error:{0}", LastError());
                                }
                            }
                            if (WinAPI)
                            {
                                //LibraryLogging.Info("Found process:{0} in session:{1} account:{2}", process.ProcessName, process.SessionId, name.ToString());
                                if (!ret.ContainsKey(process.SessionId))
                                {
                                    ret.Add(process.SessionId, new List<string>() { name.ToString() });
                                }
                                else
                                {
                                    if (!ret[process.SessionId].Contains(name.ToString()))
                                    {
                                        ret[process.SessionId].Add(name.ToString());
                                    }
                                }
                            }
                        }
                        Marshal.FreeHGlobal(TokenInformation);
                    }
                }
                SafeNativeMethods.CloseHandle(tokenHandle);
            }

            return ret;
        }

Same methods

pInvokes::GetSessionContext ( int sessionID ) : List