Getting an IP address from a hostname is normally done by calling getaddrinfo (which is really just an alias for WspiapiGetAddrInfo).
We see cases where this fails. At that same time as the failure:
ping {hostname} is able to resolve the hostname just fine.
ping -4 {hostname} also fails to resolve the hostname, just like getaddrinfo.
The solution to fix the getaddrinfo and the ping -4 failure situation is to run
ipconfig /flushdns
The above seems to indicate that ping -4 and getaddrinfo are resolving the address using an (apparently bad) entry in an internal DNS cache.
Is there a way to programmatically clear that cache (doing whatever ipconfig /flushdns does), or better, have getaddrinfo not use the cache?
Is there a way to programmatically clear that cache (doing whatever ipconfig /flushdns does)
ipconfig /flushdns does this:
typedef BOOL(WINAPI *DFRC)();
DFRC DnsFlushResolverCache;
HMODULE hDll = LoadLibrary(L"DnsApi.dll");
DnsFlushResolverCache = (DFRC)GetProcAddress(hDll, "DnsFlushResolverCache");
BOOL bRet = DnsFlushResolverCache();
// code...
FreeLibrary(hDll);