コード例 #1
0
        private static LuidAndAttributes[] GetTokenPrivileges(AccessTokenHandle accessTokenHandle)
        {
            int tokenInformationLength = 0;
            int returnLength           = 0;

            if (NativeMethods.GetTokenInformation(
                    accessTokenHandle,
                    TokenInformationClass.TokenPrivileges,
                    IntPtr.Zero,
                    tokenInformationLength,
                    ref returnLength))
            {
                return(new LuidAndAttributes[0]);
            }

            int lastWin32Error = Marshal.GetLastWin32Error();

            if (lastWin32Error != NativeMethods.ErrorInsufficientBuffer)
            {
                throw new Win32Exception(lastWin32Error);
            }

            tokenInformationLength = returnLength;
            returnLength           = 0;

            using (AllocatedMemory allocatedMemory = new AllocatedMemory(tokenInformationLength))
            {
                if (!NativeMethods.GetTokenInformation(
                        accessTokenHandle,
                        TokenInformationClass.TokenPrivileges,
                        allocatedMemory.Pointer,
                        tokenInformationLength,
                        ref returnLength))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                int privilegeCount = Marshal.ReadInt32(allocatedMemory.Pointer);
                LuidAndAttributes[] luidAndAttributes = new LuidAndAttributes[privilegeCount];
                long pointer = allocatedMemory.Pointer.ToInt64() + Marshal.SizeOf(privilegeCount);
                Type type    = typeof(LuidAndAttributes);
                long size    = Marshal.SizeOf(type);
                for (int i = 0; i < privilegeCount; i++)
                {
                    luidAndAttributes[i] = (LuidAndAttributes)Marshal.PtrToStructure(new IntPtr(pointer), type);
                    pointer += size;
                }

                return(luidAndAttributes);
            }
        }
コード例 #2
0
        internal static PrivilegeAndAttributesCollection GetPrivileges(AccessTokenHandle accessTokenHandle)
        {
            LuidAndAttributes[] luidAndAttributesArray = GetTokenPrivileges(accessTokenHandle);
            int length = luidAndAttributesArray.Length;
            List <PrivilegeAndAttributes> privilegeAndAttributes = new List <PrivilegeAndAttributes>(length);

            for (int i = 0; i < length; i++)
            {
                LuidAndAttributes luidAndAttributes = luidAndAttributesArray[i];
                string            name = GetPrivilegeName(luidAndAttributes.Luid);
                if (PrivilegesDictionary.ContainsKey(name))
                {
                    privilegeAndAttributes.Add(new PrivilegeAndAttributes(
                                                   PrivilegesDictionary[name],
                                                   luidAndAttributes.Attributes));
                }
            }

            return(new PrivilegeAndAttributesCollection(privilegeAndAttributes));
        }
コード例 #3
0
        private static LuidAndAttributes[] GetTokenPrivileges(AccessTokenHandle accessTokenHandle)
        {
            int tokenInformationLength = 0;
            int returnLength = 0;
            if (NativeMethods.GetTokenInformation(
                accessTokenHandle,
                TokenInformationClass.TokenPrivileges,
                IntPtr.Zero,
                tokenInformationLength,
                ref returnLength))
            {
                return new LuidAndAttributes[0];
            }

            int lastWin32Error = Marshal.GetLastWin32Error();
            if (lastWin32Error != NativeMethods.ErrorInsufficientBuffer)
            {
                throw new Win32Exception(lastWin32Error);
            }

            tokenInformationLength = returnLength;
            returnLength = 0;

            using (AllocatedMemory allocatedMemory = new AllocatedMemory(tokenInformationLength))
            {
                if (!NativeMethods.GetTokenInformation(
                    accessTokenHandle,
                    TokenInformationClass.TokenPrivileges,
                    allocatedMemory.Pointer,
                    tokenInformationLength,
                    ref returnLength))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                int privilegeCount = Marshal.ReadInt32(allocatedMemory.Pointer);
                LuidAndAttributes[] luidAndAttributes = new LuidAndAttributes[privilegeCount];
                long pointer = allocatedMemory.Pointer.ToInt64() + Marshal.SizeOf(privilegeCount);
                Type type = typeof(LuidAndAttributes);
                long size = Marshal.SizeOf(type);
                for (int i = 0; i < privilegeCount; i++)
                {
                    luidAndAttributes[i] = (LuidAndAttributes)Marshal.PtrToStructure(new IntPtr(pointer), type);
                    pointer += size;
                }

                return luidAndAttributes;
            }
        }