Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than dnsapi, prefix the name with the module name and a period.
dnsquery (dnsapi)
.
C# Signature:
/// <summary>
/// Provides a DNS query resolution interface
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682016(v=vs.85).aspx
/// </summary>
/// <param name="lpstrName">A pointer to a string that represents the DNS name to query</param>
/// <param name="wType">A value that represents the Resource Record DNS Record Type that is queried</param>
/// <param name="Options">A value that contains a bitmap of the DNS Query Options to use in the DNS query</param>
/// <param name="pExtra">Reserved for future use and must be set to NULL</param>
/// <param name="ppQueryResultsSet">A pointer to a pointer that points to the list of RRs that comprise the response</param>
/// <param name="pReserved">Reserved for future use and must be set to NULL</param>
/// <returns>Success (0), or the DNS-specific error defined in Winerror.h</returns>
[DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
public static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpstrName, DnsRecordTypes wType, DnsQueryOptions Options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
/// <summary>
/// Frees memory allocated for DNS records obtained by using the DnsQuery function
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682021(v=vs.85).aspx
/// </summary>
/// <param name="pRecordList">A pointer to the DNS_RECORD structure that contains the list of DNS records to be freed</param>
/// <param name="FreeType">A specifier of how the record list should be freed</param>
[DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void DnsRecordListFree(IntPtr pRecordList, DNS_FREE_TYPE FreeType);
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682082(v=vs.85).aspx
/// These field offsets could be different depending on endianness and bitness
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public struct DNS_RECORD
{
[FieldOffset(0)]
public IntPtr pNext; // DNS_RECORD*
[FieldOffset(4)]
public IntPtr pName; // string
[FieldOffset(8)]
public ushort wType;
[FieldOffset(10)]
public ushort wDataLength;
[FieldOffset(12)]
public FlagsUnion Flags;
[FieldOffset(16)]
public uint dwTtl;
[FieldOffset(20)]
public uint dwReserved;
[FieldOffset(24)]
public DataUnion Data;
}
[StructLayout(LayoutKind.Explicit)]
public struct FlagsUnion
{
[FieldOffset(0)]
public uint DW;
[FieldOffset(0)]
public DNS_RECORD_FLAGS S;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682084(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_RECORD_FLAGS
{
internal uint data;
// DWORD Section :2;
public uint Section
{
get { return data & 0x3u; }
set { data = (data & ~0x3u) | (value & 0x3u); }
}
// DWORD Delete :1;
public uint Delete
{
get { return (data >> 2) & 0x1u; }
set { data = (data & ~(0x1u << 2)) | (value & 0x1u) << 2; }
}
// DWORD CharSet :2;
public uint CharSet
{
get { return (data >> 3) & 0x3u; }
set { data = (data & ~(0x3u << 3)) | (value & 0x3u) << 3; }
}
// DWORD Unused :3;
public uint Unused
{
get { return (data >> 5) & 0x7u; }
set { data = (data & ~(0x7u << 5)) | (value & 0x7u) << 5; }
}
// DWORD Reserved :24;
public uint Reserved
{
get { return (data >> 8) & 0xFFFFFFu; }
set { data = (data & ~(0xFFFFFFu << 8)) | (value & 0xFFFFFFu) << 8; }
}
}
[StructLayout(LayoutKind.Explicit)]
public struct DataUnion
{
[FieldOffset(0)]
public DNS_A_DATA A;
[FieldOffset(0)]
public DNS_SOA_DATA SOA, Soa;
[FieldOffset(0)]
public DNS_PTR_DATA PTR, Ptr, NS, Ns, CNAME, Cname, DNAME, Dname, MB, Mb, MD, Md, MF, Mf, MG, Mg, MR, Mr;
[FieldOffset(0)]
public DNS_MINFO_DATA MINFO, Minfo, RP, Rp;
[FieldOffset(0)]
public DNS_MX_DATA MX, Mx, AFSDB, Afsdb, RT, Rt;
[FieldOffset(0)]
public DNS_TXT_DATA HINFO, Hinfo, ISDN, Isdn, TXT, Txt, X25;
[FieldOffset(0)]
public DNS_NULL_DATA Null;
[FieldOffset(0)]
public DNS_WKS_DATA WKS, Wks;
[FieldOffset(0)]
public DNS_AAAA_DATA AAAA;
[FieldOffset(0)]
public DNS_KEY_DATA KEY, Key;
[FieldOffset(0)]
public DNS_SIG_DATA SIG, Sig;
[FieldOffset(0)]
public DNS_ATMA_DATA ATMA, Atma;
[FieldOffset(0)]
public DNS_NXT_DATA NXT, Nxt;
[FieldOffset(0)]
public DNS_SRV_DATA SRV, Srv;
[FieldOffset(0)]
public DNS_NAPTR_DATA NAPTR, Naptr;
[FieldOffset(0)]
public DNS_OPT_DATA OPT, Opt;
[FieldOffset(0)]
public DNS_DS_DATA DS, Ds;
[FieldOffset(0)]
public DNS_RRSIG_DATA RRSIG, Rrsig;
[FieldOffset(0)]
public DNS_NSEC_DATA NSEC, Nsec;
[FieldOffset(0)]
public DNS_DNSKEY_DATA DNSKEY, Dnskey;
[FieldOffset(0)]
public DNS_TKEY_DATA TKEY, Tkey;
[FieldOffset(0)]
public DNS_TSIG_DATA TSIG, Tsig;
[FieldOffset(0)]
public DNS_WINS_DATA WINS, Wins;
[FieldOffset(0)]
public DNS_WINSR_DATA WINSR, WinsR, NBSTAT, Nbstat;
[FieldOffset(0)]
public DNS_DHCID_DATA DHCID;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682044(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_A_DATA
{
public uint IpAddress; // IP4_ADDRESS IpAddress;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682096(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SOA_DATA
{
public IntPtr pNamePrimaryServer; // string
public IntPtr pNameAdministrator; // string
public uint dwSerialNo;
public uint dwRefresh;
public uint dwRetry;
public uint dwExpire;
public uint dwDefaultTtl;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682080(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_PTR_DATA
{
public IntPtr pNameHost; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682067(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MINFO_DATA
{
public IntPtr pNameMailbox; // string
public IntPtr pNameErrorsMailbox; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682070(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MX_DATA
{
public IntPtr pNameExchange; // string
public ushort wPreference;
public ushort Pad;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682109(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TXT_DATA
{
public uint dwStringCount;
public IntPtr pStringArray; // PWSTR pStringArray[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682074(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NULL_DATA
{
public uint dwByteCount;
public IntPtr Data; // BYTE Data[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682120(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WKS_DATA
{
public uint IpAddress; // IP4_ADDRESS IpAddress;
public byte chProtocol; // UCHAR chProtocol;
public IntPtr BitMask; // BYTE BitMask[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682035(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_AAAA_DATA
{
// IP6_ADDRESS Ip6Address;
// DWORD IP6Dword[4];
// This isn't ideal, but it should work without using the fixed and unsafe keywords
public uint Ip6Address0;
public uint Ip6Address1;
public uint Ip6Address2;
public uint Ip6Address3;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682061(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_KEY_DATA
{
public ushort wFlags;
public byte chProtocol;
public byte chAlgorithm;
public IntPtr Key; // BYTE Key[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682094(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SIG_DATA
{
public IntPtr pNameSigner; // string
public ushort wTypeCovered;
public byte chAlgorithm;
public byte chLabelCount;
public uint dwOriginalTtl;
public uint dwExpiration;
public uint dwTimeSigned;
public ushort wKeyTag;
public ushort Pad;
public IntPtr Signature; // BYTE Signature[1];
}
public const int DNS_ATMA_MAX_ADDR_LENGTH = 20;
public const int DNS_ATMA_FORMAT_E164 = 1;
public const int DNS_ATMA_FORMAT_AESA = 2;
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682041(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_ATMA_DATA
{
public byte AddressType;
// BYTE Address[DNS_ATMA_MAX_ADDR_LENGTH];
// This isn't ideal, but it should work without using the fixed and unsafe keywords
public byte Address0;
public byte Address1;
public byte Address2;
public byte Address3;
public byte Address4;
public byte Address5;
public byte Address6;
public byte Address7;
public byte Address8;
public byte Address9;
public byte Address10;
public byte Address11;
public byte Address12;
public byte Address13;
public byte Address14;
public byte Address15;
public byte Address16;
public byte Address17;
public byte Address18;
public byte Address19;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682076(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NXT_DATA
{
public IntPtr pNameNext; // string
public ushort wNumTypes;
public IntPtr wTypes; // WORD wTypes[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682097(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SRV_DATA
{
public IntPtr pNameTarget; // string
public ushort uPriority;
public ushort wWeight;
public ushort wPort;
public ushort Pad;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/cc982164(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NAPTR_DATA
{
public ushort wOrder;
public ushort wPreference;
public IntPtr pFlags; // string
public IntPtr pService; // string
public IntPtr pRegularExpression; // string
public IntPtr pReplacement; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392298(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_OPT_DATA
{
public ushort wDataLength;
public ushort wPad;
public IntPtr Data; // BYTE Data[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392296(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_DS_DATA
{
public ushort wKeyTag;
public byte chAlgorithm;
public byte chDigestType;
public ushort wDigestLength;
public ushort wPad;
public IntPtr Digest; // BYTE Digest[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392301(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_RRSIG_DATA
{
public IntPtr pNameSigner; // string
public ushort wTypeCovered;
public byte chAlgorithm;
public byte chLabelCount;
public uint dwOriginalTtl;
public uint dwExpiration;
public uint dwTimeSigned;
public ushort wKeyTag;
public ushort Pad;
public IntPtr Signature; // BYTE Signature[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392297(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NSEC_DATA
{
public IntPtr pNextDomainName; // string
public ushort wTypeBitMapsLength;
public ushort wPad;
public IntPtr TypeBitMaps; // BYTE TypeBitMaps[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392295(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_DNSKEY_DATA
{
public ushort wFlags;
public byte chProtocol;
public byte chAlgorithm;
public ushort wKeyLength;
public ushort wPad;
public IntPtr Key; // BYTE Key[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682104(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TKEY_DATA
{
public IntPtr pNameAlgorithm; // string
public IntPtr pAlgorithmPacket; // PBYTE (which is BYTE*)
public IntPtr pKey; // PBYTE (which is BYTE*)
public IntPtr pOtherData; // PBYTE (which is BYTE*)
public uint dwCreateTime;
public uint dwExpireTime;
public ushort wMode;
public ushort wError;
public ushort wKeyLength;
public ushort wOtherLength;
public byte cAlgNameLength; // UCHAR cAlgNameLength;
public int bPacketPointers; // BOOL bPacketPointers;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682106(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TSIG_DATA
{
public IntPtr pNameAlgorithm; // string
public IntPtr pAlgorithmPacket; // PBYTE (which is BYTE*)
public IntPtr pSignature; // PBYTE (which is BYTE*)
public IntPtr pOtherData; // PBYTE (which is BYTE*)
public long i64CreateTime;
public ushort wFudgeTime;
public ushort wOriginalXid;
public ushort wError;
public ushort wSigLength;
public ushort wOtherLength;
public byte cAlgNameLength; // UCHAR cAlgNameLength;
public int bPacketPointers; // BOOL bPacketPointers;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682114(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINS_DATA
{
public uint dwMappingFlag;
public uint dwLookupTimeout;
public uint dwCacheTimeout;
public uint cWinsServerCount;
public uint WinsServers; // IP4_ADDRESS WinsServers[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682113(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINSR_DATA
{
public uint dwMappingFlag;
public uint dwLookupTimeout;
public uint dwCacheTimeout;
public IntPtr pNameResultDomain; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392294(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_DHCID_DATA
{
public uint dwByteCount;
public IntPtr DHCID; // BYTE DHCID[1];
}
Helper Functions:
/// <summary>
/// Converts an unsigned int to an ip address object
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/cc982163(v=vs.85).aspx
/// </summary>
/// <param name="ipAddress">The unsigned int to convert to an ip address object</param>
/// <returns>The converted ip address</returns>
public static IPAddress ConvertUintToIpAddress(uint ipAddress)
{
// x86 is in little endian
// Network byte order (what the IPAddress object requires) is big endian
// Ex - 0x7F000001 is 127.0.0.1
var addressBytes = new byte[4];
addressBytes[0] = (byte)((ipAddress & 0xFF000000u) >> 24);
addressBytes[1] = (byte)((ipAddress & 0x00FF0000u) >> 16);
addressBytes[2] = (byte)((ipAddress & 0x0000FF00u) >> 8);
addressBytes[3] = (byte)(ipAddress & 0x000000FFu);
return new IPAddress(addressBytes);
}
/// <summary>
/// Converts the data from the AAAA record into an ip address object
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682140(v=vs.85).aspx
/// </summary>
/// <param name="data">The AAAA record to convert</param>
/// <returns>The converted ip address</returns>
public static IPAddress ConvertAAAAToIpAddress(DNS_AAAA_DATA data)
{
var addressBytes = new byte[16];
addressBytes[0] = (byte)(data.Ip6Address0 & 0x000000FF);
addressBytes[1] = (byte)((data.Ip6Address0 & 0x0000FF00) >> 8);
addressBytes[2] = (byte)((data.Ip6Address0 & 0x00FF0000) >> 16);
addressBytes[3] = (byte)((data.Ip6Address0 & 0xFF000000) >> 24);
addressBytes[4] = (byte)(data.Ip6Address1 & 0x000000FF);
addressBytes[5] = (byte)((data.Ip6Address1 & 0x0000FF00) >> 8);
addressBytes[6] = (byte)((data.Ip6Address1 & 0x00FF0000) >> 16);
addressBytes[7] = (byte)((data.Ip6Address1 & 0xFF000000) >> 24);
addressBytes[8] = (byte)(data.Ip6Address2 & 0x000000FF);
addressBytes[9] = (byte)((data.Ip6Address2 & 0x0000FF00) >> 8);
addressBytes[10] = (byte)((data.Ip6Address2 & 0x00FF0000) >> 16);
addressBytes[11] = (byte)((data.Ip6Address2 & 0xFF000000) >> 24);
addressBytes[12] = (byte)(data.Ip6Address3 & 0x000000FF);
addressBytes[13] = (byte)((data.Ip6Address3 & 0x0000FF00) >> 8);
addressBytes[14] = (byte)((data.Ip6Address3 & 0x00FF0000) >> 16);
addressBytes[15] = (byte)((data.Ip6Address3 & 0xFF000000) >> 24);
return new IPAddress(addressBytes);
}
Alternative Managed API:
Do you know one? Please contribute it!
Notes:
See original example provided by Peter A Bromberg, Ph.D.
New example provided by Dylan Jones (jonesdyn@gmail.com).
Tips & Tricks:
Please add some!
Sample Code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
namespace DnsTest
{
public class Dns
{
/// <summary>
/// Returns a list of exchange server names from the MX records found from the given domain
/// This function can be easily changed to return data from other DNS records
/// </summary>
/// <param name="domain">The domain to query for MX records</param>
/// <returns>A list of exchange server names from the MX records found from the given domain</returns>
public static List<string> GetMxRecords(string domain)
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw new NotSupportedException();
}
var recordsArray = IntPtr.Zero;
try
{
var result = DnsQuery(ref domain, DnsRecordTypes.DNS_TYPE_MX, DnsQueryOptions.DNS_QUERY_BYPASS_CACHE,
IntPtr.Zero, ref recordsArray, IntPtr.Zero);
if (result != 0)
{
throw new Win32Exception(result);
}
DNS_RECORD record;
var recordList = new List<string>();
for (var recordPtr = recordsArray; !recordPtr.Equals(IntPtr.Zero); recordPtr = record.pNext)
{
record = (DNS_RECORD)Marshal.PtrToStructure(recordPtr, typeof(DNS_RECORD));
if (record.wType == (int)DnsRecordTypes.DNS_TYPE_MX)
{
recordList.Add(Marshal.PtrToStringAuto(record.Data.MX.pNameExchange));
}
}
/// <summary>
/// Provides a DNS query resolution interface
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682016(v=vs.85).aspx
/// </summary>
/// <param name="lpstrName">A pointer to a string that represents the DNS name to query</param>
/// <param name="wType">A value that represents the Resource Record DNS Record Type that is queried</param>
/// <param name="Options">A value that contains a bitmap of the DNS Query Options to use in the DNS query</param>
/// <param name="pExtra">Reserved for future use and must be set to NULL</param>
/// <param name="ppQueryResultsSet">A pointer to a pointer that points to the list of RRs that comprise the response</param>
/// <param name="pReserved">Reserved for future use and must be set to NULL</param>
/// <returns>Success (0), or the DNS-specific error defined in Winerror.h</returns>
[DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true,
ExactSpelling = true)]
public static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpstrName, DnsRecordTypes wType,
DnsQueryOptions Options, IntPtr pExtra, ref IntPtr ppQueryResultsSet, IntPtr pReserved);
/// <summary>
/// Frees memory allocated for DNS records obtained by using the DnsQuery function
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682021(v=vs.85).aspx
/// </summary>
/// <param name="pRecordList">A pointer to the DNS_RECORD structure that contains the list of DNS records to be freed</param>
/// <param name="FreeType">A specifier of how the record list should be freed</param>
[DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void DnsRecordListFree(IntPtr pRecordList, DNS_FREE_TYPE FreeType);
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682082(v=vs.85).aspx
/// These field offsets could be different depending on endianness and bitness
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public struct DNS_RECORD
{
[FieldOffset(0)]
public IntPtr pNext; // DNS_RECORD*
[FieldOffset(4)]
public IntPtr pName; // string
[FieldOffset(8)]
public ushort wType;
[FieldOffset(10)]
public ushort wDataLength;
[FieldOffset(12)]
public FlagsUnion Flags;
[FieldOffset(16)]
public uint dwTtl;
[FieldOffset(20)]
public uint dwReserved;
[FieldOffset(24)]
public DataUnion Data;
}
[StructLayout(LayoutKind.Explicit)]
public struct FlagsUnion
{
[FieldOffset(0)]
public uint DW;
[FieldOffset(0)]
public DNS_RECORD_FLAGS S;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682084(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_RECORD_FLAGS
{
internal uint data;
// DWORD Section :2;
public uint Section
{
get { return data & 0x3u; }
set { data = (data & ~0x3u) | (value & 0x3u); }
}
// DWORD Delete :1;
public uint Delete
{
get { return (data >> 2) & 0x1u; }
set { data = (data & ~(0x1u << 2)) | (value & 0x1u) << 2; }
}
// DWORD CharSet :2;
public uint CharSet
{
get { return (data >> 3) & 0x3u; }
set { data = (data & ~(0x3u << 3)) | (value & 0x3u) << 3; }
}
// DWORD Unused :3;
public uint Unused
{
get { return (data >> 5) & 0x7u; }
set { data = (data & ~(0x7u << 5)) | (value & 0x7u) << 5; }
}
// DWORD Reserved :24;
public uint Reserved
{
get { return (data >> 8) & 0xFFFFFFu; }
set { data = (data & ~(0xFFFFFFu << 8)) | (value & 0xFFFFFFu) << 8; }
}
}
[StructLayout(LayoutKind.Explicit)]
public struct DataUnion
{
[FieldOffset(0)]
public DNS_A_DATA A;
[FieldOffset(0)]
public DNS_SOA_DATA SOA, Soa;
[FieldOffset(0)]
public DNS_PTR_DATA PTR, Ptr, NS, Ns, CNAME, Cname, DNAME, Dname, MB, Mb, MD, Md, MF, Mf, MG, Mg, MR, Mr;
[FieldOffset(0)]
public DNS_MINFO_DATA MINFO, Minfo, RP, Rp;
[FieldOffset(0)]
public DNS_MX_DATA MX, Mx, AFSDB, Afsdb, RT, Rt;
[FieldOffset(0)]
public DNS_TXT_DATA HINFO, Hinfo, ISDN, Isdn, TXT, Txt, X25;
[FieldOffset(0)]
public DNS_NULL_DATA Null;
[FieldOffset(0)]
public DNS_WKS_DATA WKS, Wks;
[FieldOffset(0)]
public DNS_AAAA_DATA AAAA;
[FieldOffset(0)]
public DNS_KEY_DATA KEY, Key;
[FieldOffset(0)]
public DNS_SIG_DATA SIG, Sig;
[FieldOffset(0)]
public DNS_ATMA_DATA ATMA, Atma;
[FieldOffset(0)]
public DNS_NXT_DATA NXT, Nxt;
[FieldOffset(0)]
public DNS_SRV_DATA SRV, Srv;
[FieldOffset(0)]
public DNS_NAPTR_DATA NAPTR, Naptr;
[FieldOffset(0)]
public DNS_OPT_DATA OPT, Opt;
[FieldOffset(0)]
public DNS_DS_DATA DS, Ds;
[FieldOffset(0)]
public DNS_RRSIG_DATA RRSIG, Rrsig;
[FieldOffset(0)]
public DNS_NSEC_DATA NSEC, Nsec;
[FieldOffset(0)]
public DNS_DNSKEY_DATA DNSKEY, Dnskey;
[FieldOffset(0)]
public DNS_TKEY_DATA TKEY, Tkey;
[FieldOffset(0)]
public DNS_TSIG_DATA TSIG, Tsig;
[FieldOffset(0)]
public DNS_WINS_DATA WINS, Wins;
[FieldOffset(0)]
public DNS_WINSR_DATA WINSR, WinsR, NBSTAT, Nbstat;
[FieldOffset(0)]
public DNS_DHCID_DATA DHCID;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682044(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_A_DATA
{
public uint IpAddress; // IP4_ADDRESS IpAddress;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682096(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SOA_DATA
{
public IntPtr pNamePrimaryServer; // string
public IntPtr pNameAdministrator; // string
public uint dwSerialNo;
public uint dwRefresh;
public uint dwRetry;
public uint dwExpire;
public uint dwDefaultTtl;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682080(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_PTR_DATA
{
public IntPtr pNameHost; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682067(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MINFO_DATA
{
public IntPtr pNameMailbox; // string
public IntPtr pNameErrorsMailbox; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682070(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_MX_DATA
{
public IntPtr pNameExchange; // string
public ushort wPreference;
public ushort Pad;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682109(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TXT_DATA
{
public uint dwStringCount;
public IntPtr pStringArray; // PWSTR pStringArray[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682074(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NULL_DATA
{
public uint dwByteCount;
public IntPtr Data; // BYTE Data[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682120(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WKS_DATA
{
public uint IpAddress; // IP4_ADDRESS IpAddress;
public byte chProtocol; // UCHAR chProtocol;
public IntPtr BitMask; // BYTE BitMask[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682035(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_AAAA_DATA
{
// IP6_ADDRESS Ip6Address;
// DWORD IP6Dword[4];
// This isn't ideal, but it should work without using the fixed and unsafe keywords
public uint Ip6Address0;
public uint Ip6Address1;
public uint Ip6Address2;
public uint Ip6Address3;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682061(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_KEY_DATA
{
public ushort wFlags;
public byte chProtocol;
public byte chAlgorithm;
public IntPtr Key; // BYTE Key[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682094(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SIG_DATA
{
public IntPtr pNameSigner; // string
public ushort wTypeCovered;
public byte chAlgorithm;
public byte chLabelCount;
public uint dwOriginalTtl;
public uint dwExpiration;
public uint dwTimeSigned;
public ushort wKeyTag;
public ushort Pad;
public IntPtr Signature; // BYTE Signature[1];
}
public const int DNS_ATMA_MAX_ADDR_LENGTH = 20;
public const int DNS_ATMA_FORMAT_E164 = 1;
public const int DNS_ATMA_FORMAT_AESA = 2;
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682041(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_ATMA_DATA
{
public byte AddressType;
// BYTE Address[DNS_ATMA_MAX_ADDR_LENGTH];
// This isn't ideal, but it should work without using the fixed and unsafe keywords
public byte Address0;
public byte Address1;
public byte Address2;
public byte Address3;
public byte Address4;
public byte Address5;
public byte Address6;
public byte Address7;
public byte Address8;
public byte Address9;
public byte Address10;
public byte Address11;
public byte Address12;
public byte Address13;
public byte Address14;
public byte Address15;
public byte Address16;
public byte Address17;
public byte Address18;
public byte Address19;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682076(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NXT_DATA
{
public IntPtr pNameNext; // string
public ushort wNumTypes;
public IntPtr wTypes; // WORD wTypes[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682097(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_SRV_DATA
{
public IntPtr pNameTarget; // string
public ushort uPriority;
public ushort wWeight;
public ushort wPort;
public ushort Pad;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/cc982164(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NAPTR_DATA
{
public ushort wOrder;
public ushort wPreference;
public IntPtr pFlags; // string
public IntPtr pService; // string
public IntPtr pRegularExpression; // string
public IntPtr pReplacement; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392298(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_OPT_DATA
{
public ushort wDataLength;
public ushort wPad;
public IntPtr Data; // BYTE Data[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392296(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_DS_DATA
{
public ushort wKeyTag;
public byte chAlgorithm;
public byte chDigestType;
public ushort wDigestLength;
public ushort wPad;
public IntPtr Digest; // BYTE Digest[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392301(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_RRSIG_DATA
{
public IntPtr pNameSigner; // string
public ushort wTypeCovered;
public byte chAlgorithm;
public byte chLabelCount;
public uint dwOriginalTtl;
public uint dwExpiration;
public uint dwTimeSigned;
public ushort wKeyTag;
public ushort Pad;
public IntPtr Signature; // BYTE Signature[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392297(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_NSEC_DATA
{
public IntPtr pNextDomainName; // string
public ushort wTypeBitMapsLength;
public ushort wPad;
public IntPtr TypeBitMaps; // BYTE TypeBitMaps[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392295(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_DNSKEY_DATA
{
public ushort wFlags;
public byte chProtocol;
public byte chAlgorithm;
public ushort wKeyLength;
public ushort wPad;
public IntPtr Key; // BYTE Key[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682104(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TKEY_DATA
{
public IntPtr pNameAlgorithm; // string
public IntPtr pAlgorithmPacket; // PBYTE (which is BYTE*)
public IntPtr pKey; // PBYTE (which is BYTE*)
public IntPtr pOtherData; // PBYTE (which is BYTE*)
public uint dwCreateTime;
public uint dwExpireTime;
public ushort wMode;
public ushort wError;
public ushort wKeyLength;
public ushort wOtherLength;
public byte cAlgNameLength; // UCHAR cAlgNameLength;
public int bPacketPointers; // BOOL bPacketPointers;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682106(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_TSIG_DATA
{
public IntPtr pNameAlgorithm; // string
public IntPtr pAlgorithmPacket; // PBYTE (which is BYTE*)
public IntPtr pSignature; // PBYTE (which is BYTE*)
public IntPtr pOtherData; // PBYTE (which is BYTE*)
public long i64CreateTime;
public ushort wFudgeTime;
public ushort wOriginalXid;
public ushort wError;
public ushort wSigLength;
public ushort wOtherLength;
public byte cAlgNameLength; // UCHAR cAlgNameLength;
public int bPacketPointers; // BOOL bPacketPointers;
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682114(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINS_DATA
{
public uint dwMappingFlag;
public uint dwLookupTimeout;
public uint dwCacheTimeout;
public uint cWinsServerCount;
public uint WinsServers; // IP4_ADDRESS WinsServers[1];
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682113(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_WINSR_DATA
{
public uint dwMappingFlag;
public uint dwLookupTimeout;
public uint dwCacheTimeout;
public IntPtr pNameResultDomain; // string
}
/// <summary>
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/dd392294(v=vs.85).aspx
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct DNS_DHCID_DATA
{
public uint dwByteCount;
public IntPtr DHCID; // BYTE DHCID[1];
}
/// <summary>
/// Converts an unsigned int to an ip address object
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/cc982163(v=vs.85).aspx
/// </summary>
/// <param name="ipAddress">The unsigned int to convert to an ip address object</param>
/// <returns>The converted ip address</returns>
public static IPAddress ConvertUintToIpAddress(uint ipAddress)
{
// x86 is in little endian
// Network byte order (what the IPAddress object requires) is big endian
// Ex - 0x7F000001 is 127.0.0.1
var addressBytes = new byte[4];
addressBytes[0] = (byte)((ipAddress & 0xFF000000u) >> 24);
addressBytes[1] = (byte)((ipAddress & 0x00FF0000u) >> 16);
addressBytes[2] = (byte)((ipAddress & 0x0000FF00u) >> 8);
addressBytes[3] = (byte)(ipAddress & 0x000000FFu);
return new IPAddress(addressBytes);
}
/// <summary>
/// Converts the data from the AAAA record into an ip address object
/// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682140(v=vs.85).aspx
/// </summary>
/// <param name="data">The AAAA record to convert</param>
/// <returns>The converted ip address</returns>
public static IPAddress ConvertAAAAToIpAddress(DNS_AAAA_DATA data)
{
var addressBytes = new byte[16];
addressBytes[0] = (byte)(data.Ip6Address0 & 0x000000FF);
addressBytes[1] = (byte)((data.Ip6Address0 & 0x0000FF00) >> 8);
addressBytes[2] = (byte)((data.Ip6Address0 & 0x00FF0000) >> 16);
addressBytes[3] = (byte)((data.Ip6Address0 & 0xFF000000) >> 24);
addressBytes[4] = (byte)(data.Ip6Address1 & 0x000000FF);
addressBytes[5] = (byte)((data.Ip6Address1 & 0x0000FF00) >> 8);
addressBytes[6] = (byte)((data.Ip6Address1 & 0x00FF0000) >> 16);
addressBytes[7] = (byte)((data.Ip6Address1 & 0xFF000000) >> 24);
addressBytes[8] = (byte)(data.Ip6Address2 & 0x000000FF);
addressBytes[9] = (byte)((data.Ip6Address2 & 0x0000FF00) >> 8);
addressBytes[10] = (byte)((data.Ip6Address2 & 0x00FF0000) >> 16);
addressBytes[11] = (byte)((data.Ip6Address2 & 0xFF000000) >> 24);
addressBytes[12] = (byte)(data.Ip6Address3 & 0x000000FF);
addressBytes[13] = (byte)((data.Ip6Address3 & 0x0000FF00) >> 8);
addressBytes[14] = (byte)((data.Ip6Address3 & 0x00FF0000) >> 16);
addressBytes[15] = (byte)((data.Ip6Address3 & 0xFF000000) >> 24);
return new IPAddress(addressBytes);
}
}
}
Alternative Managed API:
System.Net.Dns.GetHostEntry(Name)
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).