private SearchResultCollection FindAll(bool findMoreThanOne)
        {
            IntPtr ptr;

            this.searchResult = null;
            DirectoryEntry root = null;

            if (this.assertDefaultNamingContext == null)
            {
                root = this.SearchRoot.CloneBrowsable();
            }
            else
            {
                root = this.SearchRoot.CloneBrowsable();
            }
            System.DirectoryServices.Interop.UnsafeNativeMethods.IAds adsObject = root.AdsObject;
            if (!(adsObject is System.DirectoryServices.Interop.UnsafeNativeMethods.IDirectorySearch))
            {
                throw new NotSupportedException(Res.GetString("DSSearchUnsupported", new object[] { this.SearchRoot.Path }));
            }
            if (this.directoryVirtualListViewSpecified)
            {
                this.SearchRoot.Bind(true);
            }
            System.DirectoryServices.Interop.UnsafeNativeMethods.IDirectorySearch adsSearch = (System.DirectoryServices.Interop.UnsafeNativeMethods.IDirectorySearch)adsObject;
            this.SetSearchPreferences(adsSearch, findMoreThanOne);
            string[] array = null;
            if (this.PropertiesToLoad.Count > 0)
            {
                if (!this.PropertiesToLoad.Contains("ADsPath"))
                {
                    this.PropertiesToLoad.Add("ADsPath");
                }
                array = new string[this.PropertiesToLoad.Count];
                this.PropertiesToLoad.CopyTo(array, 0);
            }
            if (array != null)
            {
                adsSearch.ExecuteSearch(this.Filter, array, array.Length, out ptr);
            }
            else
            {
                adsSearch.ExecuteSearch(this.Filter, null, -1, out ptr);
                array = new string[0];
            }
            SearchResultCollection results = new SearchResultCollection(root, ptr, array, this);

            this.searchResult = results;
            return(results);
        }
        private unsafe void SetSearchPreferences(System.DirectoryServices.Interop.UnsafeNativeMethods.IDirectorySearch adsSearch, bool findMoreThanOne)
        {
            ArrayList list = new ArrayList();
            AdsSearchPreferenceInfo info = new AdsSearchPreferenceInfo {
                dwSearchPref = 5,
                vValue       = new AdsValueHelper((int)this.SearchScope).GetStruct()
            };

            list.Add(info);
            if ((this.sizeLimit != 0) || !findMoreThanOne)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 2,
                    vValue       = new AdsValueHelper(findMoreThanOne ? this.SizeLimit : 1).GetStruct()
                };
                list.Add(info);
            }
            if (this.ServerTimeLimit >= new TimeSpan(0L))
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 3,
                    vValue       = new AdsValueHelper((int)this.ServerTimeLimit.TotalSeconds).GetStruct()
                };
                list.Add(info);
            }
            info = new AdsSearchPreferenceInfo {
                dwSearchPref = 4,
                vValue       = new AdsValueHelper(this.PropertyNamesOnly).GetStruct()
            };
            list.Add(info);
            if (this.ClientTimeout >= new TimeSpan(0L))
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 6,
                    vValue       = new AdsValueHelper((int)this.ClientTimeout.TotalSeconds).GetStruct()
                };
                list.Add(info);
            }
            if (this.PageSize != 0)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 7,
                    vValue       = new AdsValueHelper(this.PageSize).GetStruct()
                };
                list.Add(info);
            }
            if (this.ServerPageTimeLimit >= new TimeSpan(0L))
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 8,
                    vValue       = new AdsValueHelper((int)this.ServerPageTimeLimit.TotalSeconds).GetStruct()
                };
                list.Add(info);
            }
            info = new AdsSearchPreferenceInfo {
                dwSearchPref = 9,
                vValue       = new AdsValueHelper((int)this.ReferralChasing).GetStruct()
            };
            list.Add(info);
            if (this.Asynchronous)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 0,
                    vValue       = new AdsValueHelper(this.Asynchronous).GetStruct()
                };
                list.Add(info);
            }
            if (this.Tombstone)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 13,
                    vValue       = new AdsValueHelper(this.Tombstone).GetStruct()
                };
                list.Add(info);
            }
            if (this.attributeScopeQuerySpecified)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 15,
                    vValue       = new AdsValueHelper(this.AttributeScopeQuery, AdsType.ADSTYPE_CASE_IGNORE_STRING).GetStruct()
                };
                list.Add(info);
            }
            if (this.DerefAlias != DereferenceAlias.Never)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 1,
                    vValue       = new AdsValueHelper((int)this.DerefAlias).GetStruct()
                };
                list.Add(info);
            }
            if (this.SecurityMasks != System.DirectoryServices.SecurityMasks.None)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 0x10,
                    vValue       = new AdsValueHelper((int)this.SecurityMasks).GetStruct()
                };
                list.Add(info);
            }
            if (this.ExtendedDN != System.DirectoryServices.ExtendedDN.None)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 0x12,
                    vValue       = new AdsValueHelper((int)this.ExtendedDN).GetStruct()
                };
                list.Add(info);
            }
            if (this.directorySynchronizationSpecified)
            {
                info = new AdsSearchPreferenceInfo {
                    dwSearchPref = 12,
                    vValue       = new AdsValueHelper(this.DirectorySynchronization.GetDirectorySynchronizationCookie(), AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct()
                };
                list.Add(info);
                if (this.DirectorySynchronization.Option != DirectorySynchronizationOptions.None)
                {
                    info = new AdsSearchPreferenceInfo {
                        dwSearchPref = 0x11,
                        vValue       = new AdsValueHelper((int)this.DirectorySynchronization.Option).GetStruct()
                    };
                    list.Add(info);
                }
            }
            IntPtr zero      = IntPtr.Zero;
            IntPtr ptr       = IntPtr.Zero;
            IntPtr contextID = IntPtr.Zero;

            try
            {
                if ((this.Sort.PropertyName != null) && (this.Sort.PropertyName.Length > 0))
                {
                    info = new AdsSearchPreferenceInfo {
                        dwSearchPref = 10
                    };
                    AdsSortKey structure = new AdsSortKey {
                        pszAttrType = Marshal.StringToCoTaskMemUni(this.Sort.PropertyName)
                    };
                    zero = structure.pszAttrType;
                    structure.pszReserved   = IntPtr.Zero;
                    structure.fReverseOrder = (this.Sort.Direction == SortDirection.Descending) ? -1 : 0;
                    byte[] destination = new byte[Marshal.SizeOf(structure)];
                    Marshal.Copy((IntPtr)((ulong)((IntPtr) & structure)), destination, 0, destination.Length);
                    info.vValue = new AdsValueHelper(destination, AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct();
                    list.Add(info);
                }
                if (this.directoryVirtualListViewSpecified)
                {
                    info = new AdsSearchPreferenceInfo {
                        dwSearchPref = 14
                    };
                    AdsVLV svlv = new AdsVLV {
                        beforeCount = this.vlv.BeforeCount,
                        afterCount  = this.vlv.AfterCount,
                        offset      = this.vlv.Offset
                    };
                    if (this.vlv.Target.Length != 0)
                    {
                        svlv.target = Marshal.StringToCoTaskMemUni(this.vlv.Target);
                    }
                    else
                    {
                        svlv.target = IntPtr.Zero;
                    }
                    ptr = svlv.target;
                    if (this.vlv.DirectoryVirtualListViewContext == null)
                    {
                        svlv.contextIDlength = 0;
                        svlv.contextID       = IntPtr.Zero;
                    }
                    else
                    {
                        svlv.contextIDlength = this.vlv.DirectoryVirtualListViewContext.context.Length;
                        svlv.contextID       = Marshal.AllocCoTaskMem(svlv.contextIDlength);
                        contextID            = svlv.contextID;
                        Marshal.Copy(this.vlv.DirectoryVirtualListViewContext.context, 0, svlv.contextID, svlv.contextIDlength);
                    }
                    IntPtr ptr4    = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AdsVLV)));
                    byte[] buffer2 = new byte[Marshal.SizeOf(svlv)];
                    try
                    {
                        Marshal.StructureToPtr(svlv, ptr4, false);
                        Marshal.Copy(ptr4, buffer2, 0, buffer2.Length);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(ptr4);
                    }
                    info.vValue = new AdsValueHelper(buffer2, AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct();
                    list.Add(info);
                }
                if (this.cacheResultsSpecified)
                {
                    info = new AdsSearchPreferenceInfo {
                        dwSearchPref = 11,
                        vValue       = new AdsValueHelper(this.CacheResults).GetStruct()
                    };
                    list.Add(info);
                }
                AdsSearchPreferenceInfo[] prefs = new AdsSearchPreferenceInfo[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    prefs[i] = (AdsSearchPreferenceInfo)list[i];
                }
                DoSetSearchPrefs(adsSearch, prefs);
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(zero);
                }
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ptr);
                }
                if (contextID != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(contextID);
                }
            }
        }
        private static void DoSetSearchPrefs(System.DirectoryServices.Interop.UnsafeNativeMethods.IDirectorySearch adsSearch, AdsSearchPreferenceInfo[] prefs)
        {
            int    num          = Marshal.SizeOf(typeof(AdsSearchPreferenceInfo));
            IntPtr pSearchPrefs = Marshal.AllocHGlobal((IntPtr)(num * prefs.Length));

            try
            {
                IntPtr ptr = pSearchPrefs;
                for (int i = 0; i < prefs.Length; i++)
                {
                    Marshal.StructureToPtr(prefs[i], ptr, false);
                    ptr = (IntPtr)(((long)ptr) + num);
                }
                adsSearch.SetSearchPreference(pSearchPrefs, prefs.Length);
                ptr = pSearchPrefs;
                for (int j = 0; j < prefs.Length; j++)
                {
                    if (Marshal.ReadInt32(ptr, 0x20) != 0)
                    {
                        int    dwSearchPref = prefs[j].dwSearchPref;
                        string str          = "";
                        switch (dwSearchPref)
                        {
                        case 0:
                            str = "Asynchronous";
                            break;

                        case 1:
                            str = "DerefAlias";
                            break;

                        case 2:
                            str = "SizeLimit";
                            break;

                        case 3:
                            str = "ServerTimeLimit";
                            break;

                        case 4:
                            str = "PropertyNamesOnly";
                            break;

                        case 5:
                            str = "SearchScope";
                            break;

                        case 6:
                            str = "ClientTimeout";
                            break;

                        case 7:
                            str = "PageSize";
                            break;

                        case 8:
                            str = "ServerPageTimeLimit";
                            break;

                        case 9:
                            str = "ReferralChasing";
                            break;

                        case 10:
                            str = "Sort";
                            break;

                        case 11:
                            str = "CacheResults";
                            break;

                        case 12:
                            str = "DirectorySynchronization";
                            break;

                        case 13:
                            str = "Tombstone";
                            break;

                        case 14:
                            str = "VirtualListView";
                            break;

                        case 15:
                            str = "AttributeScopeQuery";
                            break;

                        case 0x10:
                            str = "SecurityMasks";
                            break;

                        case 0x11:
                            str = "DirectorySynchronizationFlag";
                            break;

                        case 0x12:
                            str = "ExtendedDn";
                            break;
                        }
                        throw new InvalidOperationException(Res.GetString("DSSearchPreferencesNotAccepted", new object[] { str }));
                    }
                    ptr = (IntPtr)(((long)ptr) + num);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(pSearchPrefs);
            }
        }