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 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); } } }
public object GetVlvValue() { AdsVLV structure = new AdsVLV(); Marshal.PtrToStructure(this.adsvalue.octetString.value, structure); byte[] destination = null; if ((structure.contextID != IntPtr.Zero) && (structure.contextIDlength != 0)) { destination = new byte[structure.contextIDlength]; Marshal.Copy(structure.contextID, destination, 0, structure.contextIDlength); } DirectoryVirtualListView view = new DirectoryVirtualListView { Offset = structure.offset, ApproximateTotal = structure.contentCount }; DirectoryVirtualListViewContext context = new DirectoryVirtualListViewContext(destination); view.DirectoryVirtualListViewContext = context; return view; }