1. 程式人生 > >利用DNSQuery 進行DNS查詢

利用DNSQuery 進行DNS查詢

#include <WinSock2.h>
#include <WinDNS.h>
#pragma comment (lib, "Dnsapi.lib")
#pragma comment (lib, "ws2_32.lib")


int _tmain(int argc, _TCHAR* argv[])
{
	PDNS_RECORD pResult = NULL;
	DNS_STATUS dnsStatus = 0;
	//DNS_QUERY_STANDARD 會首從快取中查詢,如果沒有查到才發起dns查詢,
	dnsStatus = DnsQuery_A("www.baidu.com", DNS_TYPE_A, DNS_QUERY_BYPASS_CACHE/*DNS_QUERY_STANDARD*/, NULL, &pResult, NULL);
	PDNS_RECORD temp = pResult;
	while (temp)
	{
		IN_ADDR ipaddr;
		ipaddr.S_un.S_addr = temp->Data.A.IpAddress;
		char *pAddr = inet_ntoa(ipaddr);
		printf("Addr:%s \n", pAddr);
		temp = temp->pNext;
	}
	
	DnsRecordListFree(pResult, DnsFreeRecordListDeep);

}