static void Main(string[] args) { var addresses = DnsAddr.GetAddress("google.com"); foreach (var address in addresses) { Console.WriteLine(address.ToString()); } }
public static IEnumerable <IPAddress> GetAddress(string domain) { IntPtr ptr1 = IntPtr.Zero; IntPtr ptr2 = IntPtr.Zero; List <IPAddress> list = new List <IPAddress>(); DnsRecord record = new DnsRecord(); int num1 = DnsAddr.DnsQuery(ref domain, QueryTypes.DNS_TYPE_A, QueryOptions.DNS_QUERY_NONE, 0, ref ptr1, 0); if (num1 != 0) { throw new Win32Exception(num1); } for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = record.pNext) { record = (DnsRecord)Marshal.PtrToStructure(ptr2, typeof(DnsRecord)); list.Add(new IPAddress(record.ipAddress)); } DnsAddr.DnsRecordListFree(ptr1, 0); return(list); }