private static void DoSetSearchPrefs(UnsafeNativeMethods.IDirectorySearch adsSearch, AdsSearchPreferenceInfo[] prefs) { int structSize = Marshal.SizeOf(typeof(AdsSearchPreferenceInfo)); IntPtr ptr = Marshal.AllocHGlobal((IntPtr)(structSize * prefs.Length)); try { IntPtr tempPtr = ptr; for (int i = 0; i < prefs.Length; i++) { Marshal.StructureToPtr(prefs[i], tempPtr, false); tempPtr = IntPtr.Add(tempPtr, structSize); } adsSearch.SetSearchPreference(ptr, prefs.Length); // Check for the result status for all preferences tempPtr = ptr; for (int i = 0; i < prefs.Length; i++) { int status = Marshal.ReadInt32(tempPtr, 32); if (status != 0) { int prefIndex = prefs[i].dwSearchPref; string property = ""; switch (prefIndex) { case (int)AdsSearchPreferences.SEARCH_SCOPE: property = "SearchScope"; break; case (int)AdsSearchPreferences.SIZE_LIMIT: property = "SizeLimit"; break; case (int)AdsSearchPreferences.TIME_LIMIT: property = "ServerTimeLimit"; break; case (int)AdsSearchPreferences.ATTRIBTYPES_ONLY: property = "PropertyNamesOnly"; break; case (int)AdsSearchPreferences.TIMEOUT: property = "ClientTimeout"; break; case (int)AdsSearchPreferences.PAGESIZE: property = "PageSize"; break; case (int)AdsSearchPreferences.PAGED_TIME_LIMIT: property = "ServerPageTimeLimit"; break; case (int)AdsSearchPreferences.CHASE_REFERRALS: property = "ReferralChasing"; break; case (int)AdsSearchPreferences.SORT_ON: property = "Sort"; break; case (int)AdsSearchPreferences.CACHE_RESULTS: property = "CacheResults"; break; case (int)AdsSearchPreferences.ASYNCHRONOUS: property = "Asynchronous"; break; case (int)AdsSearchPreferences.TOMBSTONE: property = "Tombstone"; break; case (int)AdsSearchPreferences.ATTRIBUTE_QUERY: property = "AttributeScopeQuery"; break; case (int)AdsSearchPreferences.DEREF_ALIASES: property = "DerefAlias"; break; case (int)AdsSearchPreferences.SECURITY_MASK: property = "SecurityMasks"; break; case (int)AdsSearchPreferences.EXTENDED_DN: property = "ExtendedDn"; break; case (int)AdsSearchPreferences.DIRSYNC: property = "DirectorySynchronization"; break; case (int)AdsSearchPreferences.DIRSYNC_FLAG: property = "DirectorySynchronizationFlag"; break; case (int)AdsSearchPreferences.VLV: property = "VirtualListView"; break; } throw new InvalidOperationException(Res.GetString(Res.DSSearchPreferencesNotAccepted, property)); } tempPtr = IntPtr.Add(tempPtr, structSize); } } finally { Marshal.FreeHGlobal(ptr); } }