public static RecordCollection Lookup(string DnsName, QueryTypes LookupType) { IntPtr ptr = IntPtr.Zero; IntPtr temp = IntPtr.Zero; RecordCollection RC = null; DnsRecord DNSR; int intAddress = BitConverter.ToInt32(IPAddress.Parse("8.8.8.8").GetAddressBytes(), 0); int result = DnsQuery(ref DnsName, LookupType, QueryOptions.DNS_QUERY_STANDARD, 0, ref ptr, 0); if (result == 0) { for (temp = ptr; !temp.Equals(IntPtr.Zero); temp = DNSR.pNext) { DNSR = (DnsRecord)Marshal.PtrToStructure(temp, typeof(DnsRecord)); if (RC == null) { RC = new RecordCollection(temp, DNSR); } RC.Add((QueryTypes)DNSR.wType, getRecord((QueryTypes)DNSR.wType, temp)); } } else { throw new Win32Exception(result); } return(RC); }
/// <summary> /// Looks up a type of DNS record /// </summary> /// <param name="DnsName">Query</param> /// <param name="LookupType">lookup type</param> /// <returns>Record collection</returns> public static RecordCollection Lookup(string DnsName, QueryTypes LookupType) { IntPtr ptr = IntPtr.Zero; IntPtr temp = IntPtr.Zero; RecordCollection RC = null; DnsRecord DNSR; //INFO: I have commented what is the custom DNS option. //INFO: It was unreliable for me. //int intAddress = BitConverter.ToInt32(IPAddress.Parse("8.8.8.8").GetAddressBytes(), 0); int result = DnsQuery(ref DnsName, LookupType, QueryOptions.DNS_QUERY_STANDARD, 0 /*intAddress*/, ref ptr, 0); if (result == 0) { for (temp = ptr; !temp.Equals(IntPtr.Zero); temp = DNSR.pNext) { DNSR = (DnsRecord)Marshal.PtrToStructure(temp, typeof(DnsRecord)); if (RC == null) { RC = new RecordCollection(temp, DNSR); } if (Marshal.PtrToStringAuto(DNSR.pName).ToLower() == DnsName.ToLower()) { #if XX_DEBUG_XX Console.WriteLine("{0} {1} {2} {3}", Marshal.PtrToStringAuto(DNSR.pName), DNSR.wType, DNSR.wDataLength, new IPAddress(BitConverter.GetBytes((uint)DNSR.Record.ToInt64())) ); #endif RC.Add((QueryTypes)DNSR.wType, getRecord((QueryTypes)DNSR.wType, temp)); } } } else { //throw new Win32Exception(result); return(null); } return(RC); }