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); // asynchronous if (Asynchronous == true) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.ASYNCHRONOUS; info.vValue = new AdsValueHelper(Asynchronous).GetStruct(); prefList.Add(info); } // tombstone if (Tombstone == true) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.TOMBSTONE; info.vValue = new AdsValueHelper(Tombstone).GetStruct(); prefList.Add(info); } // attributescopequery if (_attributeScopeQuerySpecified) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.ATTRIBUTE_QUERY; info.vValue = new AdsValueHelper(AttributeScopeQuery, AdsType.ADSTYPE_CASE_IGNORE_STRING).GetStruct(); prefList.Add(info); } // derefalias if (DerefAlias != DereferenceAlias.Never) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.DEREF_ALIASES; info.vValue = new AdsValueHelper((int)DerefAlias).GetStruct(); prefList.Add(info); } // securitymask if (SecurityMasks != SecurityMasks.None) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.SECURITY_MASK; info.vValue = new AdsValueHelper((int)SecurityMasks).GetStruct(); prefList.Add(info); } // extendeddn if (ExtendedDN != ExtendedDN.None) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.EXTENDED_DN; info.vValue = new AdsValueHelper((int)ExtendedDN).GetStruct(); prefList.Add(info); } // dirsync if (directorySynchronizationSpecified) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.DIRSYNC; info.vValue = new AdsValueHelper(DirectorySynchronization.GetDirectorySynchronizationCookie(), AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct(); prefList.Add(info); if (DirectorySynchronization.Option != DirectorySynchronizationOptions.None) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.DIRSYNC_FLAG; info.vValue = new AdsValueHelper((int)DirectorySynchronization.Option).GetStruct(); prefList.Add(info); } } IntPtr ptrToFree = (IntPtr)0; IntPtr ptrVLVToFree = (IntPtr)0; IntPtr ptrVLVContexToFree = (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)(&sortKey), sortKeyBytes, 0, sortKeyBytes.Length); info.vValue = new AdsValueHelper(sortKeyBytes, AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct(); prefList.Add(info); } // vlv if (directoryVirtualListViewSpecified) { info = new AdsSearchPreferenceInfo(); info.dwSearchPref = (int)AdsSearchPreferences.VLV; AdsVLV vlvValue = new AdsVLV(); vlvValue.beforeCount = _vlv.BeforeCount; vlvValue.afterCount = _vlv.AfterCount; vlvValue.offset = _vlv.Offset; //we need to treat the empty string as null here if (_vlv.Target.Length != 0) vlvValue.target = Marshal.StringToCoTaskMemUni(_vlv.Target); else vlvValue.target = IntPtr.Zero; ptrVLVToFree = vlvValue.target; if (_vlv.DirectoryVirtualListViewContext == null) { vlvValue.contextIDlength = 0; vlvValue.contextID = (IntPtr)0; } else { vlvValue.contextIDlength = _vlv.DirectoryVirtualListViewContext.context.Length; vlvValue.contextID = Marshal.AllocCoTaskMem(vlvValue.contextIDlength); ptrVLVContexToFree = vlvValue.contextID; Marshal.Copy(_vlv.DirectoryVirtualListViewContext.context, 0, vlvValue.contextID, vlvValue.contextIDlength); } IntPtr vlvPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(AdsVLV))); byte[] vlvBytes = new byte[Marshal.SizeOf(vlvValue)]; try { Marshal.StructureToPtr(vlvValue, vlvPtr, false); Marshal.Copy(vlvPtr, vlvBytes, 0, vlvBytes.Length); } finally { Marshal.FreeHGlobal(vlvPtr); } info.vValue = new AdsValueHelper(vlvBytes, AdsType.ADSTYPE_PROV_SPECIFIC).GetStruct(); prefList.Add(info); } // cacheResults if (_cacheResultsSpecified) { 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); if (ptrVLVToFree != (IntPtr)0) Marshal.FreeCoTaskMem(ptrVLVToFree); if (ptrVLVContexToFree != (IntPtr)0) Marshal.FreeCoTaskMem(ptrVLVContexToFree); } }
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(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); } }
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); } }