コード例 #1
0
        private void GetDomainControllerInfo()
        {
            int    errorCode = 0;
            int    dcCount   = 0;
            IntPtr zero      = IntPtr.Zero;
            int    infoLevel = 0;
            bool   flag      = false;

            this.GetDSHandle();
            IntPtr procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsGetDomainControllerInfoW");

            if (procAddress == IntPtr.Zero)
            {
                throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            System.DirectoryServices.ActiveDirectory.NativeMethods.DsGetDomainControllerInfo delegateForFunctionPointer = (System.DirectoryServices.ActiveDirectory.NativeMethods.DsGetDomainControllerInfo)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.NativeMethods.DsGetDomainControllerInfo));
            infoLevel = 3;
            errorCode = delegateForFunctionPointer(this.dsHandle, this.Domain.Name, infoLevel, out dcCount, out zero);
            if (errorCode != 0)
            {
                infoLevel = 2;
                errorCode = delegateForFunctionPointer(this.dsHandle, this.Domain.Name, infoLevel, out dcCount, out zero);
            }
            if (errorCode != 0)
            {
                throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(errorCode, base.Name);
            }
            try
            {
                IntPtr ptr = zero;
                for (int i = 0; i < dcCount; i++)
                {
                    if (infoLevel == 3)
                    {
                        DsDomainControllerInfo3 structure = new DsDomainControllerInfo3();
                        Marshal.PtrToStructure(ptr, structure);
                        if ((structure != null) && (Utils.Compare(structure.dnsHostName, base.replicaName) == 0))
                        {
                            flag = true;
                            base.cachedSiteName           = structure.siteName;
                            base.cachedSiteObjectName     = structure.siteObjectName;
                            this.cachedComputerObjectName = structure.computerObjectName;
                            base.cachedServerObjectName   = structure.serverObjectName;
                            base.cachedNtdsaObjectName    = structure.ntdsaObjectName;
                            base.cachedNtdsaObjectGuid    = structure.ntdsDsaObjectGuid;
                        }
                        ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(structure));
                    }
                    else
                    {
                        DsDomainControllerInfo2 info3 = new DsDomainControllerInfo2();
                        Marshal.PtrToStructure(ptr, info3);
                        if ((info3 != null) && (Utils.Compare(info3.dnsHostName, base.replicaName) == 0))
                        {
                            flag = true;
                            base.cachedSiteName           = info3.siteName;
                            base.cachedSiteObjectName     = info3.siteObjectName;
                            this.cachedComputerObjectName = info3.computerObjectName;
                            base.cachedServerObjectName   = info3.serverObjectName;
                            base.cachedNtdsaObjectName    = info3.ntdsaObjectName;
                            base.cachedNtdsaObjectGuid    = info3.ntdsDsaObjectGuid;
                        }
                        ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(info3));
                    }
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    procAddress = System.DirectoryServices.ActiveDirectory.UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeDomainControllerInfoW");
                    if (procAddress == IntPtr.Zero)
                    {
                        throw System.DirectoryServices.ActiveDirectory.ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                    }
                    System.DirectoryServices.ActiveDirectory.NativeMethods.DsFreeDomainControllerInfo info4 = (System.DirectoryServices.ActiveDirectory.NativeMethods.DsFreeDomainControllerInfo)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(System.DirectoryServices.ActiveDirectory.NativeMethods.DsFreeDomainControllerInfo));
                    info4(infoLevel, dcCount, zero);
                }
            }
            if (!flag)
            {
                throw new ActiveDirectoryOperationException(Res.GetString("DCInfoNotFound"));
            }
            this.dcInfoInitialized = true;
            base.siteInfoModified  = false;
        }
コード例 #2
0
ファイル: DomainController.cs プロジェクト: chcosta/corefx
        private void GetDomainControllerInfo()
        {
            int result = 0;
            int dcCount = 0;
            IntPtr dcInfoPtr = IntPtr.Zero;
            int dcInfoLevel = 0;
            bool initialized = false;

            // get the handle 
            GetDSHandle();

            // call DsGetDomainControllerInfo
            IntPtr functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsGetDomainControllerInfoW");
            if (functionPtr == (IntPtr)0)
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
            }
            NativeMethods.DsGetDomainControllerInfo dsGetDomainControllerInfo = (NativeMethods.DsGetDomainControllerInfo)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(NativeMethods.DsGetDomainControllerInfo));

            // try DsDomainControllerInfoLevel3 first which supports Read only DC (RODC)
            dcInfoLevel = NativeMethods.DsDomainControllerInfoLevel3;
            result = dsGetDomainControllerInfo(_dsHandle, Domain.Name, dcInfoLevel, out dcCount, out dcInfoPtr);

            if (result != 0)
            {
                // fallback to DsDomainControllerInfoLevel2
                dcInfoLevel = NativeMethods.DsDomainControllerInfoLevel2;
                result = dsGetDomainControllerInfo(_dsHandle, Domain.Name, dcInfoLevel, out dcCount, out dcInfoPtr);
            }

            if (result == 0)
            {
                try
                {
                    IntPtr currentDc = dcInfoPtr;
                    for (int i = 0; i < dcCount; i++)
                    {
                        if (dcInfoLevel == NativeMethods.DsDomainControllerInfoLevel3)
                        {
                            DsDomainControllerInfo3 domainControllerInfo3 = new DsDomainControllerInfo3();
                            Marshal.PtrToStructure(currentDc, domainControllerInfo3);
                            // check if this is the same as "this" DC
                            if (domainControllerInfo3 != null)
                            {
                                if (Utils.Compare(domainControllerInfo3.dnsHostName, replicaName) == 0)
                                {
                                    initialized = true;

                                    // update all the fields
                                    cachedSiteName = domainControllerInfo3.siteName;
                                    cachedSiteObjectName = domainControllerInfo3.siteObjectName;
                                    _cachedComputerObjectName = domainControllerInfo3.computerObjectName;
                                    cachedServerObjectName = domainControllerInfo3.serverObjectName;
                                    cachedNtdsaObjectName = domainControllerInfo3.ntdsaObjectName;
                                    cachedNtdsaObjectGuid = domainControllerInfo3.ntdsDsaObjectGuid;
                                }
                            }
                            currentDc = IntPtr.Add(currentDc, Marshal.SizeOf(domainControllerInfo3));
                        }
                        else
                        { //NativeMethods.DsDomainControllerInfoLevel2
                            DsDomainControllerInfo2 domainControllerInfo2 = new DsDomainControllerInfo2();
                            Marshal.PtrToStructure(currentDc, domainControllerInfo2);
                            // check if this is the same as "this" DC
                            if (domainControllerInfo2 != null)
                            {
                                if (Utils.Compare(domainControllerInfo2.dnsHostName, replicaName) == 0)
                                {
                                    initialized = true;

                                    // update all the fields
                                    cachedSiteName = domainControllerInfo2.siteName;
                                    cachedSiteObjectName = domainControllerInfo2.siteObjectName;
                                    _cachedComputerObjectName = domainControllerInfo2.computerObjectName;
                                    cachedServerObjectName = domainControllerInfo2.serverObjectName;
                                    cachedNtdsaObjectName = domainControllerInfo2.ntdsaObjectName;
                                    cachedNtdsaObjectGuid = domainControllerInfo2.ntdsDsaObjectGuid;
                                }
                            }
                            currentDc = IntPtr.Add(currentDc, Marshal.SizeOf(domainControllerInfo2));
                        }
                    }
                }
                finally
                {
                    // free the domain controller info structure
                    if (dcInfoPtr != IntPtr.Zero)
                    {
                        // call DsFreeDomainControllerInfo
                        functionPtr = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeDomainControllerInfoW");
                        if (functionPtr == (IntPtr)0)
                        {
                            throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
                        }
                        NativeMethods.DsFreeDomainControllerInfo dsFreeDomainControllerInfo = (NativeMethods.DsFreeDomainControllerInfo)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(NativeMethods.DsFreeDomainControllerInfo));
                        dsFreeDomainControllerInfo(dcInfoLevel, dcCount, dcInfoPtr);
                    }
                }

                // if we couldn't get this DC's info, throw an exception
                if (!initialized)
                {
                    throw new ActiveDirectoryOperationException(Res.GetString(Res.DCInfoNotFound));
                }

                _dcInfoInitialized = true;
                siteInfoModified = false;
            }
            else
            {
                throw ExceptionHelper.GetExceptionFromErrorCode(result, Name);
            }
        }
コード例 #3
0
ファイル: DomainController.cs プロジェクト: nickchal/pash
		private void GetDomainControllerInfo()
		{
			int num = 0;
			IntPtr zero = IntPtr.Zero;
			bool flag = false;
			this.GetDSHandle();
			IntPtr procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsGetDomainControllerInfoW");
			if (procAddress != (IntPtr)0)
			{
				NativeMethods.DsGetDomainControllerInfo delegateForFunctionPointer = (NativeMethods.DsGetDomainControllerInfo)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(NativeMethods.DsGetDomainControllerInfo));
				int num1 = 3;
				int name = delegateForFunctionPointer(this.dsHandle, this.Domain.Name, num1, out num, out zero);
				if (name != 0)
				{
					num1 = 2;
					name = delegateForFunctionPointer(this.dsHandle, this.Domain.Name, num1, out num, out zero);
				}
				if (name != 0)
				{
					throw ExceptionHelper.GetExceptionFromErrorCode(name, base.Name);
				}
				else
				{
					try
					{
						IntPtr intPtr = zero;
						for (int i = 0; i < num; i++)
						{
							if (num1 != 3)
							{
								DsDomainControllerInfo2 dsDomainControllerInfo2 = new DsDomainControllerInfo2();
								Marshal.PtrToStructure(intPtr, dsDomainControllerInfo2);
								if (dsDomainControllerInfo2 != null && Utils.Compare(dsDomainControllerInfo2.dnsHostName, this.replicaName) == 0)
								{
									flag = true;
									this.cachedSiteName = dsDomainControllerInfo2.siteName;
									this.cachedSiteObjectName = dsDomainControllerInfo2.siteObjectName;
									this.cachedComputerObjectName = dsDomainControllerInfo2.computerObjectName;
									this.cachedServerObjectName = dsDomainControllerInfo2.serverObjectName;
									this.cachedNtdsaObjectName = dsDomainControllerInfo2.ntdsaObjectName;
									this.cachedNtdsaObjectGuid = dsDomainControllerInfo2.ntdsDsaObjectGuid;
								}
								intPtr = (IntPtr)((long)intPtr + (long)Marshal.SizeOf(dsDomainControllerInfo2));
							}
							else
							{
								DsDomainControllerInfo3 dsDomainControllerInfo3 = new DsDomainControllerInfo3();
								Marshal.PtrToStructure(intPtr, dsDomainControllerInfo3);
								if (dsDomainControllerInfo3 != null && Utils.Compare(dsDomainControllerInfo3.dnsHostName, this.replicaName) == 0)
								{
									flag = true;
									this.cachedSiteName = dsDomainControllerInfo3.siteName;
									this.cachedSiteObjectName = dsDomainControllerInfo3.siteObjectName;
									this.cachedComputerObjectName = dsDomainControllerInfo3.computerObjectName;
									this.cachedServerObjectName = dsDomainControllerInfo3.serverObjectName;
									this.cachedNtdsaObjectName = dsDomainControllerInfo3.ntdsaObjectName;
									this.cachedNtdsaObjectGuid = dsDomainControllerInfo3.ntdsDsaObjectGuid;
								}
								intPtr = (IntPtr)((long)intPtr + (long)Marshal.SizeOf(dsDomainControllerInfo3));
							}
						}
					}
					finally
					{
						if (zero != IntPtr.Zero)
						{
							procAddress = UnsafeNativeMethods.GetProcAddress(DirectoryContext.ADHandle, "DsFreeDomainControllerInfoW");
							if (procAddress != (IntPtr)0)
							{
								NativeMethods.DsFreeDomainControllerInfo dsFreeDomainControllerInfo = (NativeMethods.DsFreeDomainControllerInfo)Marshal.GetDelegateForFunctionPointer(procAddress, typeof(NativeMethods.DsFreeDomainControllerInfo));
								dsFreeDomainControllerInfo(num1, num, zero);
							}
							else
							{
								throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
							}
						}
					}
					if (flag)
					{
						this.dcInfoInitialized = true;
						this.siteInfoModified = false;
						return;
					}
					else
					{
						throw new ActiveDirectoryOperationException(Res.GetString("DCInfoNotFound"));
					}
				}
			}
			else
			{
				throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
			}
		}