Jump to content
Sign in to follow this  
j0kky

Currently used DNS

Recommended Posts

Hi folks,

Last morning I needed to know programmatically which were my DNS(s) for the current connection, and I searched for an API which fitted my needs... I just tried dnsqueryconfig, which shows the DNS(s) used, if you have manually selected them in the past. 

The API is little tricky (or maybe I'm little rusty), so I decided to write a small UDF function to avoid a waste of time in the future... here you are.

; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_DnsQueryConfig
; Description ...: Retrieves the currently used DNS servers, if they were selected by user
; Syntax.........: _WinAPI_DnsQueryConfig()
; Return values .: On success it returns an array with the list of currently used DNS servers
;
;                  On failure it returns 0 and sets @error to non zero (these values are useful only for debugging reasons):
;                  |1 - DllCall error
;                  |2 - Generic error, DNS could be generated automatically
; Author ........: j0kky
; Modified ......: 1.0.0 14/11/2018
; Link ..........: https://docs.microsoft.com/en-us/windows/desktop/api/windns/nf-windns-dnsqueryconfig
; ===============================================================================================================================
Func _WinAPI_DnsQueryConfig()
    Local Const $DnsConfigDnsServerList = 6
    Local $aRet = DllCall("Dnsapi.dll", "LONG", "DnsQueryConfig", "int", $DnsConfigDnsServerList, "dword", 0, "ptr", Null, "ptr", 0, "ptr", Null, "dword*", 0)
    If @error Then Return SetError(1, 0, 0)
    if $aRet[6] <= 4 Then Return SetError(2, 0, 0)
    Local $tagBuffer = ""
    For $i = 1 To ($aRet[6] / 4)
        $tagBuffer &= "dword;"
    Next
    Local $tBuffer = DllStructCreate($tagBuffer)
    $aRet = DllCall("Dnsapi.dll", "LONG", "DnsQueryConfig", "int", $DnsConfigDnsServerList, "dword", 0, "ptr", Null, "ptr", 0, "ptr", DllStructGetPtr($tBuffer), "dword*", $aRet[6])
    Local $aDNS[($aRet[6] / 4) - 1]
    For $i = 2 to (UBound($aDNS) + 1)
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "dword", DllStructGetData($tBuffer, $i))
        if @error Then Return SetError(1, 0, 0)
        $aDNS[$i - 2] = $aRet[0]
    Next
    Return SetError(0, 0, $aDNS)
EndFunc

:bye:

Edited by j0kky

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Dwalfware
      Hi Folks
      Can not get my head around this one.
      This function gets the DNS object s
      If you have two DNS entries fine. but if you have only one! Trouble
      $objItem.DNSServerSearchOrder[0] = Primary DNS
      $objItem.DNSServerSearchOrder[1] = Secondry DNS not always there. I need a way to return 0 if not there or the IP if its there.
      Or is there a way to tell how many objects there are in the "[?]"
      I'm terrible at WMI errors, thanks for help in advance.
      Func _GETcurrentNICinfo2() $oErrors = ObjEvent("AutoIt.Error", "Error_Handle") Local $DnsSecondry Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" ) Local $query = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 ) If @error Then Return 0 If NOT IsObj($query) Then Return 0 Else For $objItem In $query Return $objItem.DNSServerSearchOrder[1] next Endif EndFunc  
    • By Skysnake
      Question
      How to detect / determine if requested ODBC driver is not available? Not installed?  Is there a specific function / error code to check for?
      I asked this on the ADO.UDF support page, but I think the issue is not limited to that UDF.  What test can be run to determine if the requested ODBC data driver is available?
      Skysnake
    • By pupitetris
      Hi. I just released this software, we need it to override the DHCP server at work. Our DHCP server does not allow per-mac-address configuration, so I coded this utility to allow our developers to use our area's custom DNS server, regardless of what DHCP provides, and let them have the possibiity to quickly shift to automatic configuration when they take their laptops home.

      https://github.com/pupitetris/DNS-Updater
      An afternoon's worth of coding, and another half day for polish and release. Pretty old-school simplicity; it was a good excercise to finally code some UI using AutoIt.
      Hope it's useful to someone.
×
×
  • Create New...