Jeff from the Windows SDK team here.
I had a customer that wanted to just find the IP address that is registered in DNS, i.e., they wanted to get back the single public IP
address and not any of the other multihomed IP addresses.
Most APIs use the DNS Resolver Cache and return all of the all of the local addresses when you query the local machine name.
DNSQuery() has dozens of flags including DNS_QUERY_WIRE_ONLY, DNS_QUERY_BYPASS_CACHE,
DNS_QUERY_NO_HOSTS_FILE that looked promising. Now I just needed a way to test
it; I couldn’t find a sample that actually looped through the results. There is an old KB article with
sample code that calls DNSQuery() but it doesn’t account for multiple returned addresses.
So I thought I would write a blog with a sample of DNSQuery(); this is that blog.
int wmain(int argc, WCHAR** argv)
PDNS_RECORD ppQueryResultsSet, p;
PIP4_ARRAY pSrvList = NULL;
int iRecord = 0;
if (argc == 1 || argc > 3)
wprintf(L"Usage: %s hostname [IP addr of DNS server] \n\n", argv);
wprintf(L"Querying for host: %s\n", argv);
if (argc == 3) // Get the IP address of the DNS server to query
pSrvList = (PIP4_ARRAY)LocalAlloc(LPTR, sizeof(IP4_ARRAY));
wprintf(L"PIP4_ARRAY allocation failed \n");
sprintf_s(temp,MAX_PATH,"%S", argv); // Convert to ASCII
pSrvList->AddrArray = inet_addr(temp); //DNS (ASCII) to IP address
pSrvList->AddrCount = 1;
wprintf(L"Querying DNS Server: %s\n", argv);
dnsStatus = DnsQuery(argv,
pSrvList, // Documented as reserved, but can take a PIP4_ARRAY for the DNS server
NULL ); // Reserved
wprintf(L"\nDNSQuery failed and returned %d, GLE = %d\n\n", dnsStatus, GetLastError());
p = ppQueryResultsSet;
while (p) // Loop through the returned addresses
wprintf(L"\nRecord #%d\n", iRecord);
ipaddr.S_un.S_addr = (p->Data.A.IpAddress);
wprintf(L"The IP address of %s is %S \n", p->pName, inet_ntoa(ipaddr));
wprintf(L"TTL: %d (secs)\n",p->dwTtl);
p = p->pNext;
if (pSrvList) LocalFree(pSrvList);
It turns out the flag that I needed was DNS_QUERY_WIRE_ONLY, this sent the query on the wire and bypassed the local cache.
Follow us on Twitter, www.twitter.com/WindowsSDK.