#include <Windows.h>
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
int DnsGetSystemAddr()
{
FIXED_INFO* pFixedInfo;
ULONG ulOutBufLen;
DWORD dwRetVal;
pFixedInfo = (FIXED_INFO*)malloc(sizeof(FIXED_INFO));
ulOutBufLen = sizeof(FIXED_INFO);
if (pFixedInfo == NULL)
{
printf("Error allocating memory needed to call GetNetworkParams\n");
return IS_ERROR;
}
// Make an initial call to GetNetworkParams to get the necessary size into the ulOutBufLen variable
if (GetNetworkParams(pFixedInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
free(pFixedInfo);
pFixedInfo = (FIXED_INFO*)malloc(ulOutBufLen);
if (pFixedInfo == NULL)
{
printf("Error allocating memory needed to call GetNetworkParams\n");
return IS_ERROR;
}
}
// Make a second call to GetNetworkParams using the allocated memory
if (dwRetVal = GetNetworkParams(pFixedInfo, &ulOutBufLen) == NO_ERROR)
{
m_VecSystemDns.clear();
IP_ADDR_STRING* pDnsServer = &pFixedInfo->DnsServerList;
while (pDnsServer)
{
printf("DNS Server: %s\n", pDnsServer->IpAddress.String);
pDnsServer = pDnsServer->Next;
}
}
else
{
printf("GetNetworkParams failed with error: %d\n", dwRetVal);
return IS_ERROR;
}
if (pFixedInfo != NULL)
{
free(pFixedInfo);
}
return IS_OK;
}