using System;
using System.Runtime.InteropServices;
using Vanara.Extensions;
using Vanara.InteropServices;
namespace Vanara.PInvoke
{
public static partial class NetApi32
{
/// Flags that provide additional domain data.
[PInvokeData("dsrole.h", MSDNShortId = "8a7b34e8-46d6-46dc-9fef-ec37b0f65eea")]
[Flags]
public enum DSROLE_FLAGS : uint
{
///
/// The directory service is running on this computer.
///
DSROLE_PRIMARY_DS_RUNNING = 0x00000001,
///
///
/// The directory service is running in mixed mode. This flag is valid only if the DSROLE_PRIMARY_DS_RUNNING flag is set.
///
///
DSROLE_PRIMARY_DS_MIXED_MODE = 0x00000002,
///
/// The computer is being upgraded from a previous version of Windows NT/Windows 2000.
///
DSROLE_UPGRADE_IN_PROGRESS = 0x00000004,
///
/// The directory service is running as read-only on this computer.
///
DSROLE_PRIMARY_DS_READONLY = 0x00000008,
///
/// The DomainGuid member contains a valid domain GUID.
///
DSROLE_PRIMARY_DOMAIN_GUID_PRESENT = 0x01000000,
}
///
///
/// The DSROLE_MACHINE_ROLE enumeration is used with the MachineRole member of the DSROLE_PRIMARY_DOMAIN_INFO_BASIC
/// structure to specify the computer role.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/ne-dsrole-_dsrole_machine_role typedef enum _DSROLE_MACHINE_ROLE {
// DsRole_RoleStandaloneWorkstation , DsRole_RoleMemberWorkstation , DsRole_RoleStandaloneServer , DsRole_RoleMemberServer ,
// DsRole_RoleBackupDomainController , DsRole_RolePrimaryDomainController } DSROLE_MACHINE_ROLE;
[PInvokeData("dsrole.h", MSDNShortId = "d5255070-71dd-4510-8bec-a84726a241c6")]
public enum DSROLE_MACHINE_ROLE
{
/// The computer is a workstation that is not a member of a domain.
DsRole_RoleStandaloneWorkstation,
/// The computer is a workstation that is a member of a domain.
DsRole_RoleMemberWorkstation,
/// The computer is a server that is not a member of a domain.
DsRole_RoleStandaloneServer,
/// The computer is a server that is a member of a domain.
DsRole_RoleMemberServer,
/// The computer is a backup domain controller.
DsRole_RoleBackupDomainController,
/// The computer is a primary domain controller.
DsRole_RolePrimaryDomainController,
}
///
///
/// The DSROLE_OPERATION_STATE enumeration is used with the DSROLE_OPERATION_STATE_INFO structure to indicate the operational
/// state of a computer.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/ne-dsrole-_dsrole_operation_state typedef enum _DSROLE_OPERATION_STATE
// { DsRoleOperationIdle , DsRoleOperationActive , DsRoleOperationNeedReboot } DSROLE_OPERATION_STATE;
[PInvokeData("dsrole.h", MSDNShortId = "de294893-e78a-4b51-9a48-0c71f91b6fde")]
public enum DSROLE_OPERATION_STATE
{
/// The computer is idle.
DsRoleOperationIdle,
/// The computer is active.
DsRoleOperationActive,
/// The computer requires a restart.
DsRoleOperationNeedReboot,
}
///
///
/// The DSROLE_PRIMARY_DOMAIN_INFO_LEVEL enumeration is used with the DsRoleGetPrimaryDomainInformation function to specify
/// the type of data to retrieve.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/ne-dsrole-_dsrole_primary_domain_info_level typedef enum
// _DSROLE_PRIMARY_DOMAIN_INFO_LEVEL { DsRolePrimaryDomainInfoBasic , DsRoleUpgradeStatus , DsRoleOperationState } DSROLE_PRIMARY_DOMAIN_INFO_LEVEL;
[PInvokeData("dsrole.h", MSDNShortId = "c8b141b1-d5fa-4ec9-8899-a1b0f6a4ce1d")]
public enum DSROLE_PRIMARY_DOMAIN_INFO_LEVEL
{
/// The DsRoleGetPrimaryDomainInformation function retrieves data from a DSROLE_PRIMARY_DOMAIN_INFO_BASIC structure.
[CorrespondingType(typeof(DSROLE_PRIMARY_DOMAIN_INFO_BASIC), CorrespondingAction.Get)]
DsRolePrimaryDomainInfoBasic = 1,
/// The DsRoleGetPrimaryDomainInformation function retrieves from a DSROLE_UPGRADE_STATUS_INFO structure.
[CorrespondingType(typeof(DSROLE_UPGRADE_STATUS_INFO), CorrespondingAction.Get)]
DsRoleUpgradeStatus,
/// The DsRoleGetPrimaryDomainInformation function retrieves data from a DSROLE_OPERATION_STATE_INFO structure.
[CorrespondingType(typeof(DSROLE_OPERATION_STATE_INFO), CorrespondingAction.Get)]
DsRoleOperationState,
}
///
///
/// The DSROLE_SERVER_STATE enumeration is used with the DSROLE_UPGRADE_STATUS_INFO structure to indicate the role of a server.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/ne-dsrole-_dsrole_server_state typedef enum _DSROLE_SERVER_STATE {
// DsRoleServerUnknown , DsRoleServerPrimary , DsRoleServerBackup } DSROLE_SERVER_STATE, *PDSROLE_SERVER_STATE;
[PInvokeData("dsrole.h", MSDNShortId = "cd15aa25-7a73-475f-b163-30e5dc1f52bd")]
public enum DSROLE_SERVER_STATE
{
/// The server role is unknown.
DsRoleServerUnknown,
/// The server was, or is, a primary domain controller.
DsRoleServerPrimary,
/// The server was, or is, a backup domain controller.
DsRoleServerBackup,
}
///
/// The DsRoleFreeMemory function frees memory allocated by the DsRoleGetPrimaryDomainInformation function.
///
///
/// Pointer to the buffer to be freed.
///
///
/// This function does not return a value.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/nf-dsrole-dsrolefreememory void DsRoleFreeMemory( IN PVOID Buffer );
[DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("dsrole.h", MSDNShortId = "5560dfec-2134-4e02-9c87-26d246cd5841")]
public static extern void DsRoleFreeMemory(IntPtr Buffer);
///
/// The DsRoleGetPrimaryDomainInformation function retrieves state data for the computer. This data includes the state of the
/// directory service installation and domain data.
///
///
/// Pointer to null-terminated Unicode string that contains the name of the computer on which to call the function. If this parameter
/// is NULL, the local computer is used.
///
///
/// Contains one of the DSROLE_PRIMARY_DOMAIN_INFO_LEVEL values that specify the type of data to retrieve. This parameter also
/// determines the format of the data supplied in Buffer.
///
///
///
/// Pointer to the address of a buffer that receives the requested data. The format of this data depends on the value of the
/// InfoLevel parameter.
///
/// The caller must free this memory when it is no longer required by calling DsRoleFreeMemory.
///
///
/// If the function is successful, the return value is ERROR_SUCCESS.
/// If the function fails, the return value can be one of the following values.
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/nf-dsrole-dsrolegetprimarydomaininformation DWORD
// DsRoleGetPrimaryDomainInformation( IN LPCWSTR lpServer, IN DSROLE_PRIMARY_DOMAIN_INFO_LEVEL InfoLevel, OUT PBYTE *Buffer );
[DllImport(Lib.NetApi32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("dsrole.h", MSDNShortId = "d54876e3-a622-4b44-a597-db0f710f7758")]
public static extern Win32Error DsRoleGetPrimaryDomainInformation([In, MarshalAs(UnmanagedType.LPWStr), Optional] string lpServer, DSROLE_PRIMARY_DOMAIN_INFO_LEVEL InfoLevel, out SafeDcRoleBuffer Buffer);
///
///
/// The DSROLE_OPERATION_STATE_INFO structure is used with the DsRoleGetPrimaryDomainInformation function to contain the
/// operational state data for a computer.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/ns-dsrole-_dsrole_operation_state_info typedef struct
// _DSROLE_OPERATION_STATE_INFO { DSROLE_OPERATION_STATE OperationState; } DSROLE_OPERATION_STATE_INFO, *PDSROLE_OPERATION_STATE_INFO;
[PInvokeData("dsrole.h", MSDNShortId = "c6c8e510-190a-47ad-805c-b8d3fbee836d")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DSROLE_OPERATION_STATE_INFO
{
///
/// Contains one of the DSROLE_OPERATION_STATE values that indicates the computer operational state.
///
public DSROLE_OPERATION_STATE OperationState;
}
///
///
/// The DSROLE_PRIMARY_DOMAIN_INFO_BASIC structure is used with the DsRoleGetPrimaryDomainInformation function to contain
/// domain data.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/ns-dsrole-_dsrole_primary_domain_info_basic typedef struct
// _DSROLE_PRIMARY_DOMAIN_INFO_BASIC { DSROLE_MACHINE_ROLE MachineRole; ULONG Flags; LPWSTR DomainNameFlat; LPWSTR DomainNameDns;
// LPWSTR DomainForestName; GUID DomainGuid; } DSROLE_PRIMARY_DOMAIN_INFO_BASIC, *PDSROLE_PRIMARY_DOMAIN_INFO_BASIC;
[PInvokeData("dsrole.h", MSDNShortId = "8a7b34e8-46d6-46dc-9fef-ec37b0f65eea")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DSROLE_PRIMARY_DOMAIN_INFO_BASIC
{
///
/// Contains one of the DSROLE_MACHINE_ROLE values that specifies the role of the computer.
///
public DSROLE_MACHINE_ROLE MachineRole;
///
///
/// Contains a set of flags that provide additional domain data. This can be a combination of one or more of the following values.
///
/// DSROLE_PRIMARY_DOMAIN_GUID_PRESENT
/// The DomainGuid member contains a valid domain GUID.
/// DSROLE_PRIMARY_DS_MIXED_MODE
///
/// The directory service is running in mixed mode. This flag is valid only if the DSROLE_PRIMARY_DS_RUNNING flag is set.
///
/// DSROLE_PRIMARY_DS_RUNNING
/// The directory service is running on this computer.
/// DSROLE_PRIMARY_DS_READONLY
/// The directory service is running as read-only on this computer.
/// DSROLE_UPGRADE_IN_PROGRESS
/// The computer is being upgraded from a previous version of Windows NT/Windows 2000.
///
public DSROLE_FLAGS Flags;
///
/// Pointer to a null-terminated Unicode string that contains the NetBIOS domain name.
///
public string DomainNameFlat;
///
///
/// Pointer to a null-terminated Unicode string that contains the DNS domain name. This member is optional and may be NULL.
///
///
public string DomainNameDns;
///
/// Pointer to a null-terminated Unicode string that contains the forest name. This member is optional and may be NULL.
///
public string DomainForestName;
///
///
/// Contains the domain identifier. This member is valid only if the Flags member contains the
/// DSROLE_PRIMARY_DOMAIN_GUID_PRESENT flag.
///
///
public Guid DomainGuid;
}
///
///
/// The DSROLE_UPGRADE_STATUS_INFO structure is used with the DsRoleGetPrimaryDomainInformation function to contain domain
/// upgrade status data.
///
///
// https://docs.microsoft.com/en-us/windows/desktop/api/dsrole/ns-dsrole-_dsrole_upgrade_status_info typedef struct
// _DSROLE_UPGRADE_STATUS_INFO { ULONG OperationState; DSROLE_SERVER_STATE PreviousServerState; } DSROLE_UPGRADE_STATUS_INFO, *PDSROLE_UPGRADE_STATUS_INFO;
[PInvokeData("dsrole.h", MSDNShortId = "c368d8d9-a91d-4013-880e-36a47d42a697")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DSROLE_UPGRADE_STATUS_INFO
{
///
/// Specifies the current state of the upgrade. This member can be one of the following values.
/// 0
/// An upgrade is not in progress.
/// DSROLE_UPGRADE_IN_PROGRESS
/// An upgrade is in progress.
///
public DSROLE_FLAGS OperationState;
///
///
/// If an upgrade is in progress, this member contains one of the DSROLE_SERVER_STATE values that indicate the previous role of
/// the server.
///
///
public DSROLE_SERVER_STATE PreviousServerState;
}
/// Provides a to a buffer that releases a created handle at disposal using DsRoleFreeMemory.
public class SafeDcRoleBuffer : SafeHANDLE
{
/// Initializes a new instance of the class and assigns an existing handle.
/// An object that represents the pre-existing handle to use.
///
/// to reliably release the handle during the finalization phase; otherwise, (not recommended).
///
public SafeDcRoleBuffer(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { }
/// Initializes a new instance of the class.
private SafeDcRoleBuffer() : base() { }
/// Returns an extracted structure from this buffer.
/// The structure type to extract.
/// Extracted structure or default(T) if the buffer is invalid.
public T ToStructure() where T : struct => IsInvalid ? default : handle.ToStructure();
///
protected override bool InternalReleaseHandle() { DsRoleFreeMemory(handle); return true; }
}
}
}