Skip to content

Instantly share code, notes, and snippets.

@valenting
Last active January 31, 2025 23:13
Show Gist options
  • Save valenting/64ff50a3d86557ea0289d15cb55c8bef to your computer and use it in GitHub Desktop.
Save valenting/64ff50a3d86557ea0289d15cb55c8bef to your computer and use it in GitHub Desktop.
DnsQuery_A not working on Windows 10

Build with

cl /EHsc /MD dns_query.cpp /link dnsapi.lib

or

clang++.exe -std=c++17 -Wall -Wextra -g main.cpp -o main.exe -ldnsapi

Output on Windows 10

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment