Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

DsGetDcName (netapi32)
 
.
Summary
The DsGetDcName function returns the name of a domain controller in a specified domain. This function accepts additional domain controller selection criteria to indicate preference for a domain controller with particular characteristics.

C# Signature:

[DllImport("Netapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern int DsGetDcName
(
    [MarshalAs(UnmanagedType.LPTStr)]
    string ComputerName,
    [MarshalAs(UnmanagedType.LPTStr)]
    string DomainName,
    [In] GuidClass DomainGuid,
    [MarshalAs(UnmanagedType.LPTStr)]
    string SiteName,
    int Flags,
    out IntPtr pDOMAIN_CONTROLLER_INFO
);

VB .NET Signature:

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

User-Defined Types:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct DOMAIN_CONTROLLER_INFO
{
    [MarshalAs(UnmanagedType.LPTStr)]
    public string    DomainControllerName;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string    DomainControllerAddress;
    public uint        DomainControllerAddressType;
    public Guid        DomainGuid;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string    DomainName;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string    DnsForestName;
    public uint        Flags;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string    DcSiteName;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string    ClientSiteName;
}

//used for level of indirection and to pass null
[StructLayout(LayoutKind.Sequential)]
public class GuidClass
{
    public Guid DomainGuid;
}

Notes:

Pointer to a PDOMAIN_CONTROLLER_INFO value that receives a pointer to a DOMAIN_CONTROLLER_INFO structure that contains data about the domain controller selected. This structure is allocated by DsGetDcName. The caller must free the structure using the NetApiBufferFree function when it is no longer required.

Tips & Tricks:

Please add some!

Sample Code:

private DOMAIN_CONTROLLER_INFO GetDomainInfo()
{
    DOMAIN_CONTROLLER_INFO domainInfo;
    IntPtr pDCI = IntPtr.Zero;

    try
    {
        int val = DsGetDcName(null, null, null, null, 0, out pDCI);

        //check return value for error
        if(ERROR_SUCCESS == val)
        {
            domainInfo = (DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(pDCI, typeof(DOMAIN_CONTROLLER_INFO));
        }
        else
        {
            throw new Win32Exception(val);
        }
    }
    finally
    {
        NetApiBufferFree(pDCI);
    }
    return domainInfo;
}

Alternative Managed API:

TODO

Documentation
DsGetDcName on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions