public static string GetTXTRecord(string domain) { IntPtr recordsArray = IntPtr.Zero; IntPtr dnsRecord = IntPtr.Zero; flags.TXTRecord txtRecord; flags.IP4_ARRAY dnsServerArray = new flags.IP4_ARRAY(); uint address = BitConverter.ToUInt32(IPAddress.Parse(GetDnsAdress().ToString()).GetAddressBytes(), 0); uint[] ipArray = new uint[1]; ipArray.SetValue(address, 0); dnsServerArray.AddrCount = 1; dnsServerArray.AddrArray = new uint[1]; dnsServerArray.AddrArray[0] = address; ArrayList recordList = new ArrayList(); try { int queryResult = Interop.DnsQuery(ref domain, flags.DnsRecordTypes.DNS_TYPE_TXT, flags.DnsQueryOptions.DNS_QUERY_BYPASS_CACHE, ref dnsServerArray, ref recordsArray, 0); if (queryResult != 0) { throw new Win32Exception(queryResult); } for (dnsRecord = recordsArray; !dnsRecord.Equals(IntPtr.Zero); dnsRecord = txtRecord.pNext) { txtRecord = (flags.TXTRecord)Marshal.PtrToStructure(dnsRecord, typeof(flags.TXTRecord)); if (txtRecord.wType == (int)flags.DnsRecordTypes.DNS_TYPE_TXT) { string txt = Marshal.PtrToStringAuto(txtRecord.pStringArray); recordList.Add(txt); } } } finally { Interop.DnsRecordListFree(recordsArray, 0); } return((string)recordList[0]); }
public static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)] ref string pszName, flags.DnsRecordTypes wType, flags.DnsQueryOptions options, ref flags.IP4_ARRAY dnsServerIpArray, ref IntPtr ppQueryResults, int pReserved);