Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
dnsquery (dnsapi)
 
.
Summary
A generic query interface to the DNS namespace, implemented in multiple forms to facilitate different character encoding.

C# Signature:

[DllImport("dnsapi", EntryPoint="DnsQuery_W", CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
    private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);

[DllImport("dnsapi", CharSet=CharSet.Auto, SetLastError=true)]
    private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

VB Signature:

    Private Declare Unicode Function DnsQuery Lib "dnsapi" Alias "DnsQuery_W" (<MarshalAs(UnmanagedType.VBByRefStr)> _
    ByRef pszName As String, ByVal wType As QueryTypes, ByVal options As QueryOptions, ByVal aipServers As Integer, ByRef ppQueryResults As IntPtr, ByVal pReserved As Integer) As Integer
    <DllImport("dnsapi", CharSet := CharSet.Auto, SetLastError := True)> _
    Private Shared Sub DnsRecordListFree(ByVal pRecordList As IntPtr, ByVal FreeType As Integer)
    End Sub

User-Defined Types:

C#

    private enum QueryOptions
    {    
        DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
        DNS_QUERY_BYPASS_CACHE = 8,
        DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
        DNS_QUERY_NO_HOSTS_FILE = 0x40,
        DNS_QUERY_NO_LOCAL_NAME = 0x20,
        DNS_QUERY_NO_NETBT = 0x80,
        DNS_QUERY_NO_RECURSION = 4,
        DNS_QUERY_NO_WIRE_QUERY = 0x10,
        DNS_QUERY_RESERVED = -16777216,
        DNS_QUERY_RETURN_MESSAGE = 0x200,
        DNS_QUERY_STANDARD = 0,
        DNS_QUERY_TREAT_AS_FQDN = 0x1000,
        DNS_QUERY_USE_TCP_ONLY = 2,
        DNS_QUERY_WIRE_ONLY = 0x100
    }

    private enum QueryTypes
    {
      DNS_TYPE_A = 1,
      DNS_TYPE_NS = 2,
      DNS_TYPE_CNAME = 5,
      DNS_TYPE_SOA = 6,
      DNS_TYPE_PTR = 12,
      DNS_TYPE_HINFO = 13,
      DNS_TYPE_MX = 15,
      DNS_TYPE_TXT = 16,
      DNS_TYPE_AAAA = 28
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct MXRecord
    {
        public IntPtr pNext;
        public string pName;
        public short wType;
        public short wDataLength;
        public int flags;
        public int dwTtl;
        public int dwReserved;
        public IntPtr pNameExchange;
        public short wPreference;
        public short Pad;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Example provided by Peter A Bromberg, Ph.D.

http://www.eggheadcafe.com/articles/20050129.asp

Tips & Tricks:

Please add some!

Sample Code:

C#

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Runtime.InteropServices;

    public class DnsMx
    {    
    public DnsMx()
    {
    }
    [DllImport("dnsapi", EntryPoint="DnsQuery_W", CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
    private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int         aipServers, ref IntPtr ppQueryResults, int pReserved);

    [DllImport("dnsapi", CharSet=CharSet.Auto, SetLastError=true)]
    private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

    public static string[] GetMXRecords(string domain)
    {

        IntPtr ptr1=IntPtr.Zero ;
        IntPtr ptr2=IntPtr.Zero ;
        MXRecord recMx;
        if (Environment.OSVersion.Platform != PlatformID.Win32NT)
        {
        throw new NotSupportedException();
        }
        ArrayList list1 = new ArrayList();
        int num1 = DnsMx.DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
        if (num1 != 0)
        {
        throw new Win32Exception(num1);
        }
        for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
        {
        recMx = ( MXRecord) Marshal.PtrToStructure(ptr2, typeof(MXRecord));
        if (recMx.wType == 15)
        {
            string text1 = Marshal.PtrToStringAuto(recMx.pNameExchange);
            list1.Add(text1);
        }
        }
        DnsMx.DnsRecordListFree(ptr1, 0);
        return (string[]) list1.ToArray(typeof(string));
    }

       private enum QueryOptions
    {    
        DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1,
        DNS_QUERY_BYPASS_CACHE = 8,
        DNS_QUERY_DONT_RESET_TTL_VALUES = 0x100000,
        DNS_QUERY_NO_HOSTS_FILE = 0x40,
        DNS_QUERY_NO_LOCAL_NAME = 0x20,
        DNS_QUERY_NO_NETBT = 0x80,
        DNS_QUERY_NO_RECURSION = 4,
        DNS_QUERY_NO_WIRE_QUERY = 0x10,
        DNS_QUERY_RESERVED = -16777216,
        DNS_QUERY_RETURN_MESSAGE = 0x200,
        DNS_QUERY_STANDARD = 0,
        DNS_QUERY_TREAT_AS_FQDN = 0x1000,
        DNS_QUERY_USE_TCP_ONLY = 2,
        DNS_QUERY_WIRE_ONLY = 0x100
    }

    private enum QueryTypes
    {
       DNS_TYPE_A = 1
      DNS_TYPE_NS = 2
      DNS_TYPE_CNAME = 5
      DNS_TYPE_SOA = 6
      DNS_TYPE_PTR = 12
      DNS_TYPE_HINFO = 13
       DNS_TYPE_MX = 15
      DNS_TYPE_TXT = 16
      DNS_TYPE_AAAA = 28
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct MXRecord
    {
        public IntPtr pNext;
        public string pName;
        public short wType;
        public short wDataLength;
        public int flags;
        public int dwTtl;
        public int dwReserved;
        public IntPtr pNameExchange;
        public short wPreference;
        public short Pad;
    }
    }

VB

    Imports System
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Runtime.InteropServices

    Public Class DnsMx
    Public Sub New()
    End Sub

    Private Declare Unicode Function DnsQuery Lib "dnsapi" Alias "DnsQuery_W" (<MarshalAs(UnmanagedType.VBByRefStr)> _
    ByRef pszName As String, ByVal wType As QueryTypes, ByVal options As QueryOptions, ByVal aipServers As Integer, ByRef ppQueryResults As IntPtr,     ByVal pReserved As Integer) As Integer

    <DllImport("dnsapi", CharSet := Runtime.InteropServices.CharSet.Auto, SetLastError := True)> _
    Private Shared Sub DnsRecordListFree(ByVal pRecordList As IntPtr, ByVal FreeType As Integer)
    End Sub

    Public Shared Function GetMXRecords(ByVal domain As String) As String()

        Dim ptr1 As IntPtr = IntPtr.Zero
        Dim ptr2 As IntPtr = IntPtr.Zero
        Dim recMx As MXRecord
        If Environment.OSVersion.Platform <> PlatformID.Win32NT Then
            Throw New NotSupportedException()
        End If
        Dim list1 As New ArrayList()
        Dim num1 As Integer = DnsMx.DnsQuery(domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ptr1, 0)
        If num1 <> 0 Then
            Throw New Win32Exception(num1)
        End If
        ptr2 = ptr1
        While Not ptr2.Equals(IntPtr.Zero)
            recMx = DirectCast(Marshal.PtrToStructure(ptr2, GetType(MXRecord)), MXRecord)
            If recMx.wType = 15 Then
                Dim text1 As String = Marshal.PtrToStringAuto(recMx.pNameExchange)
                list1.Add(text1)
            End If
            ptr2 = recMx.pNext
        End While
        DnsMx.DnsRecordListFree(ptr1, 0)
        Return DirectCast(list1.ToArray(GetType(String)), String())
    End Function

    Private Enum QueryOptions
        DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = 1
        DNS_QUERY_BYPASS_CACHE = 8
        DNS_QUERY_DONT_RESET_TTL_VALUES = 1048576
        DNS_QUERY_NO_HOSTS_FILE = 64
        DNS_QUERY_NO_LOCAL_NAME = 32
        DNS_QUERY_NO_NETBT = 128
        DNS_QUERY_NO_RECURSION = 4
        DNS_QUERY_NO_WIRE_QUERY = 16
        DNS_QUERY_RESERVED = -16777216
        DNS_QUERY_RETURN_MESSAGE = 512
        DNS_QUERY_STANDARD = 0
        DNS_QUERY_TREAT_AS_FQDN = 4096
        DNS_QUERY_USE_TCP_ONLY = 2
        DNS_QUERY_WIRE_ONLY = 256
    End Enum

    Private Enum QueryTypes
         DNS_TYPE_A = 1
          DNS_TYPE_NS = 2
          DNS_TYPE_CNAME = 5
          DNS_TYPE_SOA = 6
         DNS_TYPE_PTR = 12
         DNS_TYPE_HINFO = 13
         DNS_TYPE_MX = 15
         DNS_TYPE_TXT = 16
         DNS_TYPE_AAAA = 28
    End Enum

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure MXRecord
        Public pNext As IntPtr
        Public pName As String
        Public wType As Short
        Public wDataLength As Short
        Public flags As Integer
        Public dwTtl As Integer
        Public dwReserved As Integer
        Public pNameExchange As IntPtr
        Public wPreference As Short
        Public Pad As Short
    End Structure
    End Class

Documentation
DnsQuery on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions