Example #1
0
        /// <summary>
        ///     Setup the size of the buffer Windows provides for an LSA_REFERENCED_DOMAIN_LIST
        /// </summary>

        internal static void InitializeReferencedDomainsPointer(SafeLsaMemoryHandle referencedDomains)
        {
            Debug.Assert(referencedDomains != null, "referencedDomains != null");

            // We don't know the real size of the referenced domains yet, so we need to set an initial
            // size based on the LSA_REFERENCED_DOMAIN_LIST structure, then resize it to include all of
            // the domains.
            referencedDomains.Initialize((uint)Marshal.SizeOf <Interop.LSA_REFERENCED_DOMAIN_LIST>());
            Interop.LSA_REFERENCED_DOMAIN_LIST domainList = referencedDomains.Read <Interop.LSA_REFERENCED_DOMAIN_LIST>(0);

            unsafe
            {
                byte *pRdl = null;
                try
                {
                    referencedDomains.AcquirePointer(ref pRdl);

                    // If there is a trust information list, then the buffer size is the end of that list minus
                    // the beginning of the domain list. Otherwise, then the buffer is just the size of the
                    // referenced domain list structure, which is what we defaulted to.
                    if (domainList.Domains != IntPtr.Zero)
                    {
                        Interop.LSA_TRUST_INFORMATION *pTrustInformation = (Interop.LSA_TRUST_INFORMATION *)domainList.Domains;
                        pTrustInformation = pTrustInformation + domainList.Entries;

                        long bufferSize = (byte *)pTrustInformation - pRdl;
                        Debug.Assert(bufferSize > 0, "bufferSize > 0");
                        referencedDomains.Initialize((ulong)bufferSize);
                    }
                }
                finally
                {
                    if (pRdl != null)
                    {
                        referencedDomains.ReleasePointer();
                    }
                }
            }
        }