private unsafe void SetSearchPreferences(UnsafeNativeMethods.IDirectorySearch adsSearch, bool findMoreThanOne)
        {
            ArrayList prefList = new ArrayList();
            AdsSearchPreferenceInfo info;

            // search scope
            info = new AdsSearchPreferenceInfo();
            info.dwSearchPref = (int)AdsSearchPreferences.SEARCH_SCOPE;
            info.vValue       = new AdsValueHelper((int)SearchScope).GetStruct();
            prefList.Add(info);

            // size limit
            if (sizeLimit != 0 || !findMoreThanOne)
            {
                info = new AdsSearchPreferenceInfo();
                info.dwSearchPref = (int)AdsSearchPreferences.SIZE_LIMIT;
                info.vValue       = new AdsValueHelper(findMoreThanOne ? SizeLimit : 1).GetStruct();
                prefList.Add(info);
            }

            // time limit
            if (ServerTimeLimit >= new TimeSpan(0))
            {
                info = new AdsSearchPreferenceInfo();
                info.dwSearchPref = (int)AdsSearchPreferences.TIME_LIMIT;
                info.vValue       = new AdsValueHelper((int)ServerTimeLimit.TotalSeconds).GetStruct();
                prefList.Add(info);
            }

            // propertyNamesOnly
            info = new AdsSearchPreferenceInfo();
            info.dwSearchPref = (int)AdsSearchPreferences.ATTRIBTYPES_ONLY;
            info.vValue       = new AdsValueHelper(PropertyNamesOnly).GetStruct();
            prefList.Add(info);

            // Timeout
            if (ClientTimeout >= new TimeSpan(0))
            {
                info = new AdsSearchPreferenceInfo();
                info.dwSearchPref = (int)AdsSearchPreferences.TIMEOUT;
                info.vValue       = new AdsValueHelper((int)ClientTimeout.TotalSeconds).GetStruct();
                prefList.Add(info);
            }

            // page size
            if (PageSize != 0)
            {
                info = new AdsSearchPreferenceInfo();
                info.dwSearchPref = (int)AdsSearchPreferences.PAGESIZE;
                info.vValue       = new AdsValueHelper(PageSize).GetStruct();
                prefList.Add(info);
            }

            // page time limit
            if (ServerPageTimeLimit >= new TimeSpan(0))
            {
                info = new AdsSearchPreferenceInfo();
                info.dwSearchPref = (int)AdsSearchPreferences.PAGED_TIME_LIMIT;
                info.vValue       = new AdsValueHelper((int)ServerPageTimeLimit.TotalSeconds).GetStruct();
                prefList.Add(info);
            }

            // chase referrals
            info = new AdsSearchPreferenceInfo();
            info.dwSearchPref = (int)AdsSearchPreferences.CHASE_REFERRALS;
            info.vValue       = new AdsValueHelper((int)ReferralChasing).GetStruct();
            prefList.Add(info);

            IntPtr ptrToFree = (IntPtr)0;

            try {
                // sort
                if (Sort.PropertyName != null && Sort.PropertyName.Length > 0)
                {
                    info = new AdsSearchPreferenceInfo();
                    info.dwSearchPref = (int)AdsSearchPreferences.SORT_ON;
                    AdsSortKey sortKey = new AdsSortKey();
                    sortKey.pszAttrType   = Marshal.StringToCoTaskMemUni(Sort.PropertyName);
                    ptrToFree             = sortKey.pszAttrType; // so we can free it later.
                    sortKey.pszReserved   = (IntPtr)0;
                    sortKey.fReverseOrder = (Sort.Direction == SortDirection.Descending) ? -1 : 0;
                    byte[] sortKeyBytes = new byte[Marshal.SizeOf(sortKey)];
                    Marshal.Copy((INTPTR_INTPTRCAST)(long)&sortKey, sortKeyBytes, 0, sortKeyBytes.Length);
                    info.vValue = new AdsValueHelper(sortKeyBytes, AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct();
                    prefList.Add(info);
                }

                // cacheResults
                info = new AdsSearchPreferenceInfo();
                info.dwSearchPref = (int)AdsSearchPreferences.CACHE_RESULTS;
                info.vValue       = new AdsValueHelper(CacheResults).GetStruct();
                prefList.Add(info);

                //
                // now make the call
                //
                AdsSearchPreferenceInfo[] prefs = new AdsSearchPreferenceInfo[prefList.Count];
                for (int i = 0; i < prefList.Count; i++)
                {
                    prefs[i] = (AdsSearchPreferenceInfo)prefList[i];
                }

                DoSetSearchPrefs(adsSearch, prefs);
            }
            finally {
                if (ptrToFree != (IntPtr)0)
                {
                    Marshal.FreeCoTaskMem(ptrToFree);
                }
            }
        }
        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);
                }
            }
        }