#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;
}
windows win32 c/c++获取系统dns
最新推荐文章于 2025-12-03 11:12:35 发布
本文详细描述了一个使用C++编写的函数DnsGetSystemAddr,它通过`GetNetworkParams`从Windows系统中获取DNS服务器列表。通过两次内存分配调用,函数打印出系统的DNS服务器地址。
1万+

被折叠的 条评论
为什么被折叠?



