cl /EHsc /MD dns_query.cpp /link dnsapi.lib
or
clang++.exe -std=c++17 -Wall -Wextra -g main.cpp -o main.exe -ldnsapi
DnsQuery_A failed with error: 9501
DNS query failed.
result 0000000000000000
#include <windows.h> | |
#include <windns.h> | |
#include <iostream> | |
#include <string> | |
#define LOG(fmt, ...) std::printf(fmt "\n", __VA_ARGS__) | |
// Function to query the HTTPS (Type 65) DNS record | |
bool query_dns_https_record(const std::string& host) { | |
PDNS_RECORD result = nullptr; | |
DNS_STATUS status = DnsQuery_A( | |
host.c_str(), | |
65, // Type 65 corresponds to HTTPS record | |
DNS_QUERY_STANDARD, | |
nullptr, | |
&result, | |
nullptr | |
); | |
if (status != ERROR_SUCCESS) { | |
LOG("DnsQuery_A failed with error: %ld", status); | |
return false; | |
} | |
// This is NULL on Windows 10 | |
LOG("result %p", result); | |
// Free the DNS query result 🤷♂️ | |
if (result) { | |
DnsRecordListFree(result, DnsFreeRecordListDeep); | |
} | |
return true; | |
} | |
int main() { | |
std::string host = "example.com"; | |
if (!query_dns_https_record(host)) { | |
std::cerr << "DNS query failed." << std::endl; | |
} | |
std::string host2 = "cloudflare-dns.com"; | |
if (!query_dns_https_record(host2)) { | |
std::cerr << "DNS query failed." << std::endl; | |
} | |
return 0; | |
} |