Example #1
0
        private static IEnumerable <NtAtom> GetGlobalAtoms()
        {
            int size = 1024;

            while (size < 5 * 1024 * 1024)
            {
                using (var buffer = new SafeStructureInOutBuffer <AtomTableInformation>(size, true))
                {
                    NtStatus status = NtSystemCalls.NtQueryInformationAtom(0,
                                                                           AtomInformationClass.AtomTableInformation, buffer, buffer.Length, out int return_length);
                    if (status.IsSuccess())
                    {
                        AtomTableInformation table = buffer.Result;
                        IntPtr   data  = buffer.Data.DangerousGetHandle();
                        ushort[] atoms = new ushort[table.NumberOfAtoms];
                        buffer.Data.ReadArray(0, atoms, 0, atoms.Length);
                        return(atoms.Select(a => new NtAtom(a, true)).ToArray());
                    }
                    else if (status != NtStatus.STATUS_INFO_LENGTH_MISMATCH)
                    {
                        throw new NtException(status);
                    }
                    size *= 2;
                }
            }
            return(new NtAtom[0]);
        }
        /// <summary>
        /// Enumerate all atoms.
        /// </summary>
        /// <returns>An enumeration of all atoms on the system.</returns>
        public static IEnumerable <NtAtom> GetAtoms()
        {
            int size = 1024;

            while (size < 5 * 1024 * 1024)
            {
                using (SafeStructureInOutBuffer <AtomTableInformation> buffer = new SafeStructureInOutBuffer <AtomTableInformation>(size, true))
                {
                    int      return_length;
                    NtStatus status = NtSystemCalls.NtQueryInformationAtom(0, AtomInformationClass.AtomTableInformation, buffer, buffer.Length, out return_length);
                    if (status.IsSuccess())
                    {
                        AtomTableInformation table = buffer.Result;
                        IntPtr data = buffer.Data.DangerousGetHandle();
                        for (int i = 0; i < table.NumberOfAtoms; ++i)
                        {
                            ushort atom = (ushort)Marshal.ReadInt16(data);
                            yield return(new NtAtom(atom));

                            data += 2;
                        }
                    }
                    else if (status != NtStatus.STATUS_INFO_LENGTH_MISMATCH)
                    {
                        throw new NtException(status);
                    }
                    size *= 2;
                }
            }
        }