I am trying to use an API [DNSQuery()] from the DNSAPI.dll
I get a return value of 87 from the API which states that there was an incorrect parameter... My code is below, can anyone see anything I did wrong?
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Private Enum QueryOptions
DNS_QUERY_STANDARD = &H0
DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = &H1
DNS_QUERY_USE_TCP_ONLY = &H2
DNS_QUERY_NO_RECURSION = &H4
DNS_QUERY_BYPASS_CACHE = 8
DNS_QUERY_NO_WIRE_QUERY = &H10
DNS_QUERY_NO_LOCAL_NAME = &H20
DNS_QUERY_NO_HOSTS_FILE = &H40
DNS_QUERY_NO_NETBT = &H80
DNS_QUERY_TREAT_AS_FQDN = &H1000
DNS_QUERY_WIRE_ONLY = &H100
DNS_QUERY_RETURN_MESSAGE = &H200
DNS_QUERY_DONT_RESET_TTL_VALUES = &H100000
DNS_QUERY_RESERVED = &HFF000000 '-16777216
End Enum
Private Enum QueryTypes
DNS_TYPE_A = 1
DNS_TYPE_NS = 2
DNS_TYPE_MD = 3
DNS_TYPE_MF = 4
DNS_TYPE_CNAME = 5
DNS_TYPE_SOA = 6
DNS_TYPE_MB = 7
DNS_TYPE_MG = 8
DNS_TYPE_MR = 9
DNS_TYPE_NULL = 10
DNS_TYPE_WKS = 11
DNS_TYPE_PTR = 12
DNS_TYPE_HINFO = 13
DNS_TYPE_MINFO = 14
DNS_TYPE_MX = 15
DNS_TYPE_TEXT = 16
End Enum
Private Structure MXRecord
Dim ptrNext As IntPtr
Dim strName As String
Dim intType As Short
Dim intDataLength As Short
Dim intFlags As Integer
Dim intTTL As Integer
Dim intReserved As Integer
Dim ptrNameExchange As IntPtr
Dim intPreference As Short
Dim intPad As Short
End Structure
Private Declare Auto Function DNSQuery Lib "DNSAPI.dll" Alias "DnsQuery_W" (ByVal Name As String, ByVal Type As QueryTypes, ByVal Options As QueryOptions, ByVal Servers As Integer, ByVal QueryResults As IntPtr, ByVal Reserved As Integer) As Integer
Private Declare Auto Sub DNSRecordListFree Lib "DNSAPI.dll" Alias "DnsRecordListFree" (ByVal RecordList As IntPtr, ByVal FreeType As Integer)
Private Function GetMXRecord(ByVal Domain As String) As String()
If Not (Environment.OSVersion.Platform = PlatformID.Win32NT) Then
Throw New NotSupportedException
End If
Dim strServerName As String = ""
Dim ptrFirstItemInList As IntPtr = IntPtr.Zero
Dim ptrCurrentItemInList As IntPtr = IntPtr.Zero
Dim objMXRecord As MXRecord
Dim arrReturnList As New ArrayList
Dim intReturnValue As Integer = DNSQuery(Domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, Nothing, ptrFirstItemInList, 0)
If intReturnValue <> 0 Then
Throw New Win32Exception(intReturnValue)
End If
ptrCurrentItemInList = ptrFirstItemInList
While Not ptrCurrentItemInList.Equals(IntPtr.Zero)
objMXRecord = CType(Marshal.PtrToStructure(ptrCurrentItemInList, GetType(MXRecord)), MXRecord)
If objMXRecord.intType = 15 Then
strServerName = Marshal.PtrToStringAuto(objMXRecord.ptrNameExchange)
arrReturnList.Add(strServerName)
End If
ptrCurrentItemInList = objMXRecord.ptrNext
End While
DNSRecordListFree(ptrCurrentItemInList, 0)
Return CType(arrReturnList.ToArray(GetType(String)), String())
End Function
________________________
Ben Santiago, MCP & A+
Programmer Analyst (SQL, FoxPro, VB, VB.Net, Java, HTML, ASP, JSP, VBS)
Eastern Suffolk BOCES - Student Data Services
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
-Rich Cook