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