Exemplo n.º 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);
            }
        }
Exemplo n.º 2
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;
            }
        }