Question dnsapi.dll and DnsQuery() in VB.NET

Galaxy_Stranger

New member
Joined
Oct 25, 2019
Messages
2
Programming Experience
5-10
I've been tasked with looking up MX records from a DNS server. Of course, the DNSClient class omits this functionality, so I've imported the dnsapi.dll and setup the DnsQuery() function. I'm not a Windows API developer and documentation on this functionality is scant, on top of not being familiar with the C# data types and constructs used in all the samples I find.

So, I've set everything up and started up a query. Right now, the problem is I'm getting a query response code of "123". I can't find any documentation on all possible codes, I don't know what to do with "123", and my record result set is empty so I'm pretty much stuck in terms of troubleshooting why I'm not getting my records.

Here's my code so far:

VB.NET:
Declare Auto Function DnsQuery Lib "dnsapi.dll" Alias "DnsQuery_UTF8" (

                ByRef pszName As String,            '   DNS Name

                ByVal wType As enumQueryTypes,      '   DNS Record Type, e.g.: "DNS_TYPE_MX"

                ByVal Options As enumQueryOptions,  '   DNS Query Options, e.g.: "DNS_QUERY_BYPASS_CACHE" - bypasses resolver cache

                ByVal pExtra As ArrayList,          '   Array of DNS Server(s) to query

                ByRef ppQueryResults As IntPtr,     '   Pointer to list of records returned

                ByVal pReserved As Integer          '   Unused - set to NULL

                ) As Integer


Declare Auto Function DnsRecordListFree Lib "dnsapi.dll" (

                ByRef p As IntPtr,     '   Pointer to DNS Record structure

                ByVal t As Integer     '   DNS_FREE_TYPE enumeration.  Specifies how the record should be freed: enumDNS_FREE_TYPE.DnsFreeFlat

                ) As Integer


Dim objDnsServerList As ArrayList = New ArrayList()   ' Net.IPAddress()

Dim objRecordsArray As New ArrayList

Dim objMxRecord As MXRecord

Dim objCurrentIpAddress As IPAddress

Dim objLocalIpAddress As IPAddress

Dim intDnsQueryStatus As Integer = 0

'Dim intptrQueryResults1 As IntPtr = IntPtr.Zero

Dim intptrQueryResults1 As IntPtr = Nothing

Dim intptrQueryResults2 As IntPtr = Nothing

Dim strErrorMessageText = ""


objDnsServerList.Add("208.67.222.222")

objDnsServerList.Add("208.67.222.220")

objDnsServerList.Add("8.8.8.8")


intDnsQueryStatus = PCI_Dns.DnsQuery(strDomain, enumQueryTypes.DNS_TYPE_MX, enumQueryOptions.DNS_QUERY_BYPASS_CACHE, objDnsServerList, intptrQueryResults1, 0)

Does anybody have any experience with this?
 
The docs says this will only work with windows 2000 client/server. But there are versions of these functions for windows 8 and up in the documentation Domain Name System (DNS) however, you will find most of the docs are covered in C++ and C++ headers. DNS query completed normally returns 123, but in windows error codes it can also mean that there was an invalid syntax used, which is likely your case.
 
Thanks for the reply.

So, ALL of this functionality is deprecated? How is anyone supposed to code this? How does Microsoft code Exchange or Outlook? How do these 3rd-party VB.NET libraries do it?
 
So, ALL of this functionality is deprecated?
They're not really deprecated, they're just old and dated. Where does it say DnsQuery_A function (windns.h) are deprecated? As I said, the requirements are for older versions, with more modern versions covered under windows 8 and upwards, but significantly in less detail. The work that's going on in Microsoft with Windows 10 and the publishing of their git source will likely expedite all these old API calls with requests to be modernised by the community, but that may take some time. But to say :
How do these 3rd-party VB.NET libraries do it?
Has been answered with :
you would presumably call the appropriate function(s) in the same way you do other Windows API functions
I presume when you call these API's using unspecified non recommended client/servers. You may experience undesired behaviours. You really should do more research into this yourself. When searching Pinvoke, and other resources for answers, I found source code as well as documentation : pinvoke.net: DnsQuery (dnsapi) - I haven't tried it, but these are maintained pages submitted by contributors, and so they should work. However, you should be careful converting them to VB.NET from C#.

However, you could take a completely different approach to this if you don't want to become entangled in Pinvoke. Search for a web source such as intoDNS: checks DNS and mail servers health - except one whom provide an API to query other servers for DNS results. You would essentially make web requests against their API and this might be easier than working with Microsoft's dated API's, regardless of the API's available under Windows 8 and upwards, as they're not documented as scholarly as those linked above.
 
Back
Top