Пример #1
0
        internal static bool IsMachineDC(string computerName)
        {
            int    num;
            bool   flag;
            bool   machineRole;
            IntPtr zero = IntPtr.Zero;

            try
            {
                if (computerName != null)
                {
                    num = UnsafeNativeMethods.DsRoleGetPrimaryDomainInformation(computerName, UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_LEVEL.DsRolePrimaryDomainInfoBasic, out zero);
                }
                else
                {
                    num = UnsafeNativeMethods.DsRoleGetPrimaryDomainInformation(IntPtr.Zero, UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_LEVEL.DsRolePrimaryDomainInfoBasic, out zero);
                }
                if (num == 0)
                {
                    UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC structure = (UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC)Marshal.PtrToStructure(zero, typeof(UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC));
                    if (structure.MachineRole == UnsafeNativeMethods.DSROLE_MACHINE_ROLE.DsRole_RoleBackupDomainController)
                    {
                        machineRole = true;
                    }
                    else
                    {
                        machineRole = structure.MachineRole == UnsafeNativeMethods.DSROLE_MACHINE_ROLE.DsRole_RolePrimaryDomainController;
                    }
                    flag = machineRole;
                }
                else
                {
                    object[] objArray = new object[1];
                    objArray[0] = num;
                    throw new PrincipalOperationException(string.Format(CultureInfo.CurrentCulture, StringResources.UnableToRetrieveDomainInfo, objArray));
                }
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    UnsafeNativeMethods.DsRoleFreeMemory(zero);
                }
            }
            return(flag);
        }
Пример #2
0
        internal static bool IsMachineDC(String computerName)
        {
            IntPtr dsRoleInfoPtr = IntPtr.Zero;
            int    err           = -1;

            try
            {
                if (null == computerName)
                {
                    err = UnsafeNativeMethods.DsRoleGetPrimaryDomainInformation(IntPtr.Zero, UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_LEVEL.DsRolePrimaryDomainInfoBasic, out dsRoleInfoPtr);
                }
                else
                {
                    err = UnsafeNativeMethods.DsRoleGetPrimaryDomainInformation(computerName, UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_LEVEL.DsRolePrimaryDomainInfoBasic, out dsRoleInfoPtr);
                }

                if (err != 0)
                {
                    GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "IsMachineDC: DsRoleGetPrimaryDomainInformation failed, err=" + err);
                    throw new PrincipalOperationException(
                              String.Format(
                                  CultureInfo.CurrentCulture,
                                  StringResources.UnableToRetrieveDomainInfo,
                                  err));
                }

                UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC dsRolePrimaryDomainInfo =
                    (UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC)Marshal.PtrToStructure(dsRoleInfoPtr, typeof(UnsafeNativeMethods.DSROLE_PRIMARY_DOMAIN_INFO_BASIC));

                return(dsRolePrimaryDomainInfo.MachineRole == UnsafeNativeMethods.DSROLE_MACHINE_ROLE.DsRole_RoleBackupDomainController ||
                       dsRolePrimaryDomainInfo.MachineRole == UnsafeNativeMethods.DSROLE_MACHINE_ROLE.DsRole_RolePrimaryDomainController);
            }
            finally
            {
                if (dsRoleInfoPtr != IntPtr.Zero)
                {
                    UnsafeNativeMethods.DsRoleFreeMemory(dsRoleInfoPtr);
                }
            }
        }