dsrolegetprimarydomaininformation (netapi32)
Last changed: -131.107.0.87

.
Summary
TODO - a short description

C# Signature:

    /// <summary>Retrieves state data for the computer, which includes the state of the directory service installation and domain data.</summary>
    /// <param name="server">Name of the computer on which to retrieve state data.</param>
    /// <param name="infoLevel">The type of information to retrieve.</param>
    /// <param name="buffer">Pointer to a buffer.</param>
    /// <returns>Win32 error code.</returns>
    [DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
    private static extern int DsRoleGetPrimaryDomainInformation(
        [In] string server,
        [In] PrimaryDomainInfoLevel infoLevel,
        [Out] out IntPtr buffer);

VB Signature:

Declare Function DsRoleGetPrimaryDomainInformation Lib "netapi32.dll" (TODO) As TODO

User-Defined Types:

    /// <summary>Information flags for the primary domain.</summary>
    [Flags]
    public enum BasicPrimaryDomainInfoAttributes
    {
    /// <summary>The directory service is running on this computer.</summary>
    ServerRunning = 1,

    /// <summary>The directory service is running in mixed mode.</summary>
    ServerMixedMode = 2,

    /// <summary>The computer is being upgraded from a previous version of Windows.</summary>
    UpgradeInProgress = 4,

    /// <summary>The directory service is running as read-only on this computer.</summary>
    ServerReadOnly = 8,

    /// <summary>The DomainGuid member contains a valid domain GUID.</summary>
    GloballyUniqueIdentifierPresent = 16777216
    }

    /// <summary>Computer roles.</summary>
    public enum MachineRole
    {
    /// <summary>The computer is a workstation that is not a member of a domain.</summary>
    StandaloneWorkstation,

    /// <summary>The computer is a workstation that is a member of a domain.</summary>
    MemberWorkstation,

    /// <summary>The computer is a server that is not a member of a domain.</summary>
    StandaloneServer,

    /// <summary>The computer is a server that is a member of a domain.</summary>
    MemberServer,

    /// <summary>The computer is a backup domain controller.</summary>
    BackupDomainController,

    /// <summary>The computer is a primary domain controller.</summary>
    PrimaryDomainController,

    /// <summary>The computer is a workstation that is a member of a castle.</summary>
    WorkstationWithSharedAccountDomain,

    /// <summary>The computer is a server that is a member of a castle.</summary>
    ServerWithSharedAccountDomain,

    /// <summary>The computer is a workstation that is a member of a castle and a domain simultaneously.</summary>
    MemberWorkstationWithSharedAccountDomain,

    /// <summary>The computer is a server that is a member of a castle and a domain simultaneously.</summary>
    MemberServerWithSharedAccountDomain
    }

    /// <summary>Operational state of the domain.</summary>
    [Flags]
    public enum OperationStateAttributes
    {
    /// <summary>Primary domain server is running.</summary>
    Running = 1,

    /// <summary>Primary domain server is in mixed mode.</summary>
    MixedMode = 2,

    /// <summary>Upgrade in progress.</summary>
    UpgradeInProgress = 4,

    /// <summary>Primary domain server is read-only.</summary>
    ReadOnly = 8,

    /// <summary>Primary domain server GUID is present.</summary>
    DomainGuidPresent = 16777216
    }

    /// <summary>State of the server.</summary>
    public enum ServerState
    {
    /// <summary>Server state is unknown.</summary>
    Unknown,

    /// <summary>Server is the primary domain controller.</summary>
    Primary,

    /// <summary>Server is a backup domain controller.</summary>
    Backup
    }

    /// <summary>Primary domain information levels.</summary>
    internal enum PrimaryDomainInfoLevel
    {
    /// <summary>The basis primary domain.</summary>
    BasicPrimaryDomain = 1,

    /// <summary>The upgrade status of the domain.</summary>
    UpgradeStatus,

    /// <summary>The operational state of the domain.</summary>
    OperationState
    }

    /// <summary>Information pertaining to the primary domain.</summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct BasicPrimaryDomainInfo
    {
    /// <summary>The computer role.</summary>
    private MachineRole machineRole;

    /// <summary>Additional domain data.</summary>
    private BasicPrimaryDomainInfoAttributes flags;

    /// <summary>The NetBIOS domain name.</summary>
    private string domainNameFlat;

    /// <summary>The DNS domain name.</summary>
    private string domainNameDns;

    /// <summary>The forest name.</summary>
    private string domainForestName;

    /// <summary>The domain identifier.</summary>
    private GloballyUniqueIdentifier domainGloballyUniqueIdentifier;

    /// <summary>Gets the computer role.</summary>
    /// <value>The computer role.</value>
    public MachineRole MachineRole
    {
        get { return this.machineRole; }
    }

    /// <summary>Gets additional domain data.</summary>
    /// <value>Additional domain data</value>
    public BasicPrimaryDomainInfoAttributes Attributes
    {
        get { return this.flags; }
    }

    /// <summary>Gets the NetBIOS domain name.</summary>
    /// <value>The NetBIOS domain name.</value>
    public string DomainNameFlat
    {
        get { return this.domainNameFlat; }
    }

    /// <summary>Gets the DNS domain name.</summary>
    /// <value>The DNS domain name.</value>
    public string DomainNameDns
    {
        get { return this.domainNameDns; }
    }

    /// <summary>Gets the forest name.</summary>
    /// <value>The forest name.</value>
    public string DomainForestName
    {
        get { return this.domainForestName; }
    }

    /// <summary>Gets the domain identifier.</summary>
    /// <value>The domain identifier.</value>
    public GloballyUniqueIdentifier DomainGloballyUniqueIdentifier
    {
        get { return this.domainGloballyUniqueIdentifier; }
    }
    }

    /// <summary>Globally unique identifier.</summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct GloballyUniqueIdentifier
    {
    /// <summary>The first data.</summary>
    private int firstData;

    /// <summary>The second data.</summary>
    private short secondData;

    /// <summary>The third data.</summary>
    private short thirdData;

    /// <summary>The fourth data.</summary>
    private long fourthData;

    /// <summary>Gets the first data.</summary>
    /// <value>The first data.</value>
    public int FirstData
    {
        get { return this.firstData; }
    }

    /// <summary>Gets the second data.</summary>
    /// <value>The second data.</value>
    public short SecondData
    {
        get { return this.secondData; }
    }

    /// <summary>Gets the third data.</summary>
    /// <value>The third data.</value>
    public short ThirdData
    {
        get { return this.thirdData; }
    }

    /// <summary>Gets the fourth data.</summary>
    /// <value>The fourth data.</value>
    public long FourthData
    {
        get { return this.fourthData; }
    }
    }

    /// <summary>Information pertaining to the operational state of the domain.</summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct OperationStateInfo
    {
    /// <summary>The operational state of the domain.</summary>
    private OperationStateAttributes operationStateAttributes;

    /// <summary>Gets the operational state of the domain.</summary>
    /// <value>The operational state of the domain.</value>
    public OperationStateAttributes OperationStateAttributes
    {
        get { return this.operationStateAttributes; }
    }
    }

    /// <summary>Information pertaining to the upgrade status of the domain.</summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct UpgradeStatusInfo
    {
    /// <summary>The operational state of the domain.</summary>
    private OperationStateAttributes operationStateAttributes;

    /// <summary>The state of the server.</summary>
    private ServerState serverState;

    /// <summary>Gets the operational state of the domain.</summary>
    /// <value>The operational state of the domain.</value>
    public OperationStateAttributes OperationStateAttributes
    {
        get { return this.operationStateAttributes; }
    }

    /// <summary>Gets the state of the server.</summary>
    /// <value>The state of the server.</value>
    public ServerState ServerState
    {
        get { return this.serverState; }
    }
    }

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    /// <summary>Gets information pertaining to the primary domain.</summary>
    /// <returns>Information pertaining to the primary domain.</returns>
    /// <exception cref="Win32Exception">Thrown when the domain information cannot be retrieved.</exception>
    /// <permission cref="SecurityAction.LinkDemand">Requires the immediate caller to have FullTrust.</permission>
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
    public static BasicPrimaryDomainInfo GetBasicPrimaryDomainInfo()
    {
        return GetBasicPrimaryDomainInfo(null);
    }

    /// <summary>Gets information pertaining to the primary domain.</summary>
    /// <param name="server">Name of the computer on which to retrieve the information.</param>
    /// <returns>Information pertaining to the primary domain.</returns>
    /// <exception cref="Win32Exception">Thrown when the domain information cannot be retrieved.</exception>
    /// <permission cref="SecurityAction.LinkDemand">Requires the immediate caller to have FullTrust.</permission>
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
    public static BasicPrimaryDomainInfo GetBasicPrimaryDomainInfo(string server)
    {
        return (BasicPrimaryDomainInfo)NativeMethods.GetPrimaryDomainInfo(
        server,
        PrimaryDomainInfoLevel.BasicPrimaryDomain);
    }

    /// <summary>Gets information pertaining to the operational state of the domain.</summary>
    /// <returns>Information pertaining to the operational state of the domain.</returns>
    /// <exception cref="Win32Exception">Thrown when the domain information cannot be retrieved.</exception>
    /// <permission cref="SecurityAction.LinkDemand">Requires the immediate caller to have FullTrust.</permission>
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
    public static OperationStateInfo GetOperationStateInfo()
    {
        return GetOperationStateInfo(null);
    }

    /// <summary>Gets information pertaining to the operational state of the domain.</summary>
    /// <param name="server">Name of the computer on which to retrieve the information.</param>
    /// <returns>Information pertaining to the operational state of the domain.</returns>
    /// <exception cref="Win32Exception">Thrown when the domain information cannot be retrieved.</exception>
    /// <permission cref="SecurityAction.LinkDemand">Requires the immediate caller to have FullTrust.</permission>
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
    public static OperationStateInfo GetOperationStateInfo(string server)
    {
        return (OperationStateInfo)GetPrimaryDomainInfo(
        server,
        PrimaryDomainInfoLevel.OperationState);
    }

    /// <summary>Gets information pertaining to the upgrade status of the domain.</summary>
    /// <returns>Information pertaining to the upgrade status of the domain.</returns>
    /// <exception cref="Win32Exception">Thrown when the domain information cannot be retrieved.</exception>
    /// <permission cref="SecurityAction.LinkDemand">Requires the immediate caller to have FullTrust.</permission>
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
    public static UpgradeStatusInfo GetUpgradeStatusInfo()
    {
        return GetUpgradeStatusInfo(null);
    }

    /// <summary>Gets information pertaining to the upgrade status of the domain.</summary>
    /// <param name="server">Name of the computer on which to retrieve the information.</param>
    /// <returns>Information pertaining to the upgrade status of the domain.</returns>
    /// <exception cref="Win32Exception">Thrown when the domain information cannot be retrieved.</exception>
    /// <permission cref="SecurityAction.LinkDemand">Requires the immediate caller to have FullTrust.</permission>
    [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
    public static UpgradeStatusInfo GetUpgradeStatusInfo(string server)
    {
        return (UpgradeStatusInfo)GetPrimaryDomainInfo(
        server,
        PrimaryDomainInfoLevel.UpgradeStatus);
    }

    /// <summary>Gets state data for the computer, which includes the state of the directory service installation and domain data.</summary>
    /// <param name="server">Name of the computer on which to retrieve state data.</param>
    /// <param name="infoLevel">The type of information to retrieve.</param>
    /// <returns>State data for the computer.</returns>
    /// <exception cref="Win32Exception">Thrown when the domain information cannot be retrieved.</exception>
    internal static object GetPrimaryDomainInfo(string server, PrimaryDomainInfoLevel infoLevel)
    {
        IntPtr buffer = IntPtr.Zero;
        object primaryDomainInformation;

        try
        {
        int lastWin32Error = DsRoleGetPrimaryDomainInformation(server, infoLevel, out buffer);
        if (lastWin32Error != 0)
        {
            throw new Win32Exception(lastWin32Error);
        }

        switch (infoLevel)
        {
            case PrimaryDomainInfoLevel.BasicPrimaryDomain:
            primaryDomainInformation = Marshal.PtrToStructure(buffer, typeof(BasicPrimaryDomainInfo));
            break;
            case PrimaryDomainInfoLevel.OperationState:
            primaryDomainInformation = Marshal.PtrToStructure(buffer, typeof(OperationStateInfo));
            break;
            case PrimaryDomainInfoLevel.UpgradeStatus:
            primaryDomainInformation = Marshal.PtrToStructure(buffer, typeof(UpgradeStatusInfo));
            break;
            default:
            throw new InvalidEnumArgumentException(
                "infoLevel",
                (int)infoLevel,
                typeof(PrimaryDomainInfoLevel));
        }
        }
        finally
        {
        if (buffer != IntPtr.Zero)
        {
            DsRoleFreeMemory(buffer);
        }
        }

        return primaryDomainInformation;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation