public object Invoke(string methodName, params object[] args)
        {
            object nativeObject = this.NativeObject;
            Type   type         = nativeObject.GetType();
            object adsObject    = null;

            try
            {
                adsObject = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, nativeObject, args, CultureInfo.InvariantCulture);
                GC.KeepAlive(this);
            }
            catch (COMException exception)
            {
                throw COMExceptionHelper.CreateFormattedComException(exception);
            }
            catch (TargetInvocationException exception2)
            {
                if ((exception2.InnerException != null) && (exception2.InnerException is COMException))
                {
                    COMException innerException = (COMException)exception2.InnerException;
                    throw new TargetInvocationException(exception2.Message, COMExceptionHelper.CreateFormattedComException(innerException));
                }
                throw exception2;
            }
            if (adsObject is System.DirectoryServices.Interop.UnsafeNativeMethods.IAds)
            {
                return(new DirectoryEntry(adsObject, this.UsePropertyCache, this.GetUsername(), this.GetPassword(), this.AuthenticationType));
            }
            return(adsObject);
        }
 internal DirectoryEntry(object adsObject, bool useCache, string username, string password, AuthenticationTypes authenticationType, bool AdsObjIsExternal)
 {
     this.path               = "";
     this.useCache           = true;
     this.authenticationType = AuthenticationTypes.Secure;
     this.adsObject          = adsObject as System.DirectoryServices.Interop.UnsafeNativeMethods.IAds;
     if (this.adsObject == null)
     {
         throw new ArgumentException(Res.GetString("DSDoesNotImplementIADs"));
     }
     this.path               = this.adsObject.ADsPath;
     this.useCache           = useCache;
     this.authenticationType = authenticationType;
     this.credentials        = new NetworkCredential(username, password);
     if (username == null)
     {
         this.userNameIsNull = true;
     }
     if (password == null)
     {
         this.passwordIsNull = true;
     }
     if (!useCache)
     {
         this.CommitChanges();
     }
     this.options = new DirectoryEntryConfiguration(this);
     if (!AdsObjIsExternal)
     {
         this.InitADsObjectOptions();
     }
 }
 private void Unbind()
 {
     if (this.adsObject != null)
     {
         Marshal.ReleaseComObject(this.adsObject);
     }
     this.adsObject                 = null;
     this.propertyCollection        = null;
     this.objectSecurityInitialized = false;
     this.objectSecurityModified    = false;
 }
        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);
        }
        public void MoveTo(DirectoryEntry newParent, string newName)
        {
            object obj2 = null;

            if (!(newParent.AdsObject is System.DirectoryServices.Interop.UnsafeNativeMethods.IAdsContainer))
            {
                throw new InvalidOperationException(Res.GetString("DSNotAContainer", new object[] { newParent.Path }));
            }
            try
            {
                if (this.AdsObject.ADsPath.StartsWith("WinNT:", StringComparison.Ordinal))
                {
                    string aDsPath = this.AdsObject.ADsPath;
                    string str2    = newParent.AdsObject.ADsPath;
                    if (Utils.Compare(aDsPath, 0, str2.Length, str2, 0, str2.Length) == 0)
                    {
                        uint compareFlags = ((Utils.NORM_IGNORENONSPACE | Utils.NORM_IGNOREKANATYPE) | Utils.NORM_IGNOREWIDTH) | Utils.SORT_STRINGSORT;
                        if (Utils.Compare(aDsPath, 0, str2.Length, str2, 0, str2.Length, compareFlags) != 0)
                        {
                            aDsPath = str2 + aDsPath.Substring(str2.Length);
                        }
                    }
                    obj2 = newParent.ContainerObject.MoveHere(aDsPath, newName);
                }
                else
                {
                    obj2 = newParent.ContainerObject.MoveHere(this.Path, newName);
                }
            }
            catch (COMException exception)
            {
                throw COMExceptionHelper.CreateFormattedComException(exception);
            }
            if (this.Bound)
            {
                Marshal.ReleaseComObject(this.adsObject);
            }
            this.adsObject = (System.DirectoryServices.Interop.UnsafeNativeMethods.IAds)obj2;
            this.path      = this.adsObject.ADsPath;
            this.InitADsObjectOptions();
            if (!this.useCache)
            {
                this.CommitChanges();
            }
            else
            {
                this.RefreshCache();
            }
        }
        public DirectoryEntry CopyTo(DirectoryEntry newParent, string newName)
        {
            if (!newParent.IsContainer)
            {
                throw new InvalidOperationException(Res.GetString("DSNotAContainer", new object[] { newParent.Path }));
            }
            object adsObject = null;

            try
            {
                adsObject = newParent.ContainerObject.CopyHere(this.Path, newName);
            }
            catch (COMException exception)
            {
                throw COMExceptionHelper.CreateFormattedComException(exception);
            }
            return(new DirectoryEntry(adsObject, newParent.UsePropertyCache, this.GetUsername(), this.GetPassword(), this.AuthenticationType));
        }
 internal void Bind(bool throwIfFail)
 {
     if (this.disposed)
     {
         throw new ObjectDisposedException(base.GetType().Name);
     }
     if (this.adsObject == null)
     {
         string path = this.Path;
         if ((path == null) || (path.Length == 0))
         {
             DirectoryEntry entry = new DirectoryEntry("LDAP://RootDSE", true, null, null, AuthenticationTypes.Secure);
             string         str2  = (string)entry.Properties["defaultNamingContext"][0];
             entry.Dispose();
             path = "LDAP://" + str2;
         }
         if (Thread.CurrentThread.GetApartmentState() == ApartmentState.Unknown)
         {
             Thread.CurrentThread.SetApartmentState(ApartmentState.MTA);
         }
         System.Guid iid      = new System.Guid("00000000-0000-0000-c000-000000000046");
         object      ppObject = null;
         int         hr       = System.DirectoryServices.Interop.UnsafeNativeMethods.ADsOpenObject(path, this.GetUsername(), this.GetPassword(), (int)this.authenticationType, ref iid, out ppObject);
         if (hr != 0)
         {
             if (throwIfFail)
             {
                 throw COMExceptionHelper.CreateFormattedComException(hr);
             }
         }
         else
         {
             this.adsObject = (System.DirectoryServices.Interop.UnsafeNativeMethods.IAds)ppObject;
         }
         this.InitADsObjectOptions();
     }
 }
 private void Unbind()
 {
     if (this.adsObject != null)
     {
         Marshal.ReleaseComObject(this.adsObject);
     }
     this.adsObject = null;
     this.propertyCollection = null;
     this.objectSecurityInitialized = false;
     this.objectSecurityModified = false;
 }
 public void MoveTo(DirectoryEntry newParent, string newName)
 {
     object obj2 = null;
     if (!(newParent.AdsObject is System.DirectoryServices.Interop.UnsafeNativeMethods.IAdsContainer))
     {
         throw new InvalidOperationException(Res.GetString("DSNotAContainer", new object[] { newParent.Path }));
     }
     try
     {
         if (this.AdsObject.ADsPath.StartsWith("WinNT:", StringComparison.Ordinal))
         {
             string aDsPath = this.AdsObject.ADsPath;
             string str2 = newParent.AdsObject.ADsPath;
             if (Utils.Compare(aDsPath, 0, str2.Length, str2, 0, str2.Length) == 0)
             {
                 uint compareFlags = ((Utils.NORM_IGNORENONSPACE | Utils.NORM_IGNOREKANATYPE) | Utils.NORM_IGNOREWIDTH) | Utils.SORT_STRINGSORT;
                 if (Utils.Compare(aDsPath, 0, str2.Length, str2, 0, str2.Length, compareFlags) != 0)
                 {
                     aDsPath = str2 + aDsPath.Substring(str2.Length);
                 }
             }
             obj2 = newParent.ContainerObject.MoveHere(aDsPath, newName);
         }
         else
         {
             obj2 = newParent.ContainerObject.MoveHere(this.Path, newName);
         }
     }
     catch (COMException exception)
     {
         throw COMExceptionHelper.CreateFormattedComException(exception);
     }
     if (this.Bound)
     {
         Marshal.ReleaseComObject(this.adsObject);
     }
     this.adsObject = (System.DirectoryServices.Interop.UnsafeNativeMethods.IAds) obj2;
     this.path = this.adsObject.ADsPath;
     this.InitADsObjectOptions();
     if (!this.useCache)
     {
         this.CommitChanges();
     }
     else
     {
         this.RefreshCache();
     }
 }
 public object Invoke(string methodName, params object[] args)
 {
     object nativeObject = this.NativeObject;
     Type type = nativeObject.GetType();
     object adsObject = null;
     try
     {
         adsObject = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, nativeObject, args, CultureInfo.InvariantCulture);
         GC.KeepAlive(this);
     }
     catch (COMException exception)
     {
         throw COMExceptionHelper.CreateFormattedComException(exception);
     }
     catch (TargetInvocationException exception2)
     {
         if ((exception2.InnerException != null) && (exception2.InnerException is COMException))
         {
             COMException innerException = (COMException) exception2.InnerException;
             throw new TargetInvocationException(exception2.Message, COMExceptionHelper.CreateFormattedComException(innerException));
         }
         throw exception2;
     }
     if (adsObject is System.DirectoryServices.Interop.UnsafeNativeMethods.IAds)
     {
         return new DirectoryEntry(adsObject, this.UsePropertyCache, this.GetUsername(), this.GetPassword(), this.AuthenticationType);
     }
     return adsObject;
 }
 public DirectoryEntry CopyTo(DirectoryEntry newParent, string newName)
 {
     if (!newParent.IsContainer)
     {
         throw new InvalidOperationException(Res.GetString("DSNotAContainer", new object[] { newParent.Path }));
     }
     object adsObject = null;
     try
     {
         adsObject = newParent.ContainerObject.CopyHere(this.Path, newName);
     }
     catch (COMException exception)
     {
         throw COMExceptionHelper.CreateFormattedComException(exception);
     }
     return new DirectoryEntry(adsObject, newParent.UsePropertyCache, this.GetUsername(), this.GetPassword(), this.AuthenticationType);
 }
 internal void Bind(bool throwIfFail)
 {
     if (this.disposed)
     {
         throw new ObjectDisposedException(base.GetType().Name);
     }
     if (this.adsObject == null)
     {
         string path = this.Path;
         if ((path == null) || (path.Length == 0))
         {
             DirectoryEntry entry = new DirectoryEntry("LDAP://RootDSE", true, null, null, AuthenticationTypes.Secure);
             string str2 = (string) entry.Properties["defaultNamingContext"][0];
             entry.Dispose();
             path = "LDAP://" + str2;
         }
         if (Thread.CurrentThread.GetApartmentState() == ApartmentState.Unknown)
         {
             Thread.CurrentThread.SetApartmentState(ApartmentState.MTA);
         }
         System.Guid iid = new System.Guid("00000000-0000-0000-c000-000000000046");
         object ppObject = null;
         int hr = System.DirectoryServices.Interop.UnsafeNativeMethods.ADsOpenObject(path, this.GetUsername(), this.GetPassword(), (int) this.authenticationType, ref iid, out ppObject);
         if (hr != 0)
         {
             if (throwIfFail)
             {
                 throw COMExceptionHelper.CreateFormattedComException(hr);
             }
         }
         else
         {
             this.adsObject = (System.DirectoryServices.Interop.UnsafeNativeMethods.IAds) ppObject;
         }
         this.InitADsObjectOptions();
     }
 }
 internal DirectoryEntry(object adsObject, bool useCache, string username, string password, AuthenticationTypes authenticationType, bool AdsObjIsExternal)
 {
     this.path = "";
     this.useCache = true;
     this.authenticationType = AuthenticationTypes.Secure;
     this.adsObject = adsObject as System.DirectoryServices.Interop.UnsafeNativeMethods.IAds;
     if (this.adsObject == null)
     {
         throw new ArgumentException(Res.GetString("DSDoesNotImplementIADs"));
     }
     this.path = this.adsObject.ADsPath;
     this.useCache = useCache;
     this.authenticationType = authenticationType;
     this.credentials = new NetworkCredential(username, password);
     if (username == null)
     {
         this.userNameIsNull = true;
     }
     if (password == null)
     {
         this.passwordIsNull = true;
     }
     if (!useCache)
     {
         this.CommitChanges();
     }
     this.options = new DirectoryEntryConfiguration(this);
     if (!AdsObjIsExternal)
     {
         this.InitADsObjectOptions();
     }
 }