Using DsGetDcName in C# sample: How to get a Global Catalog

The directory service functions provide a utility for locating a domain controller (DC) in a Windows domain.
The DsGetDcName function (implemented by the Netlogon service) returns the name of a domain controller in a specified domain

Sample usage:

How to get a Global Catalog

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct DomainControllerInfo
{
	public string DomainControllerName;
	public string DomainControllerAddress;
	public int DomainControllerAddressType;
	public Guid DomainGuid;
	public string DomainName;
	public string DnsForestName;
	public int Flags;
	public string DcSiteName;
	public string ClientSiteName;
}

[Flags]
private enum DSGETDCNAME_FLAGS : uint
{
	DS_FORCE_REDISCOVERY = 0x00000001,
	DS_DIRECTORY_SERVICE_REQUIRED = 0x00000010,
	DS_DIRECTORY_SERVICE_PREFERRED = 0x00000020,
	DS_GC_SERVER_REQUIRED = 0x00000040,
	DS_PDC_REQUIRED = 0x00000080,
	DS_BACKGROUND_ONLY = 0x00000100,
	DS_IP_REQUIRED = 0x00000200,
	DS_KDC_REQUIRED = 0x00000400,
	DS_TIMESERV_REQUIRED = 0x00000800,
	DS_WRITABLE_REQUIRED = 0x00001000,
	DS_GOOD_TIMESERV_PREFERRED = 0x00002000,
	DS_AVOID_SELF = 0x00004000,
	DS_ONLY_LDAP_NEEDED = 0x00008000,
	DS_IS_FLAT_NAME = 0x00010000,
	DS_IS_DNS_NAME = 0x00020000,
	DS_RETURN_DNS_NAME = 0x40000000,
	DS_RETURN_FLAT_NAME = 0x80000000
}

[DllImport("Netapi32.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "DsGetDcNameW", CharSet = CharSet.Unicode)]
private static extern int DsGetDcName(
	[In] string computerName,
	[In] string domainName,
	[In] IntPtr domainGuid,
	[In] string siteName,
	[In] DSGETDCNAME_FLAGS flags,
	[Out] out IntPtr domainControllerInfo);

[DllImport("Netapi32.dll")]
private static extern int NetApiBufferFree(
	[In] IntPtr buffer);

private const int ERROR_SUCCESS = 0;

static void Main(string[] args)
{
	IntPtr pDomainInfo;
	if (DsGetDcName(
			string.Empty,//ComputerName
			"contoso.com",//DomainName
			IntPtr.Zero,//DomainGuid
			string.Empty,//SiteName
			DSGETDCNAME_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED |
			DSGETDCNAME_FLAGS.DS_GC_SERVER_REQUIRED |
			DSGETDCNAME_FLAGS.DS_IS_DNS_NAME |
			DSGETDCNAME_FLAGS.DS_RETURN_DNS_NAME,
			out pDomainInfo) == ERROR_SUCCESS)
	{
		DomainControllerInfo dci = new DomainControllerInfo();
		dci = (DomainControllerInfo)Marshal.PtrToStructure(pDomainInfo, typeof(DomainControllerInfo));
		NetApiBufferFree(pDomainInfo);
		pDomainInfo = IntPtr.Zero;
		Console.WriteLine("Found Global catalog DC: " + dci.DomainControllerName);
	}
}