I am a C # developer trying to learn C ++ 11. I am trying to query DNS using windns.h.
I started with DnsQuery() and read that I need to free the parameter for writing results using DnsRecordListFree() . The C # way might be to use a try-finally block to ensure that I release the resource no matter what.
But I found out that there is no finally block, and that windns.h really needs to get the time and implement an interface compatible with RAII (as I understand a typical tip). Instead of waiting for this to happen, I tried to create an RAII wrapper class whose destructor calls DnsRecordListFree() , and with the operator overload the cast to get the original pointer.
But I was confused about how to properly use this handle or pointer to get the out parameter. And while I was researching what I learned, how unique_ptr , which I already learned a little about, can be used with user deletion.
So here is my simple code. This is most likely wrong, but I suppose I can declare another PDNS_RECORD *presult and use this as an out parameter and then copy or move or otherwise assign its value to unique_ptr , but that sounds like too much work / mess.
It seems to me that the unique_ptr internal pointer should be initialized to NULL , that I must somehow pass the address of the pointer to the out parameter so that DNSQuery original value, and when unique_ptr goes beyond the scope of my function, the call to DnsRecordListFree() will be executed automatically. There are too many, I do not know to determine the right combination for the minimum correct / safe use.
#include <iostream> #include <fstream> #include <memory> #include <Windows.h> #include <WinDNS.h> using namespace std; auto pdnsDeleter = [&](PDNS_RECORD *ptr){ if (ptr) DnsRecordListFree(ptr); }; int main(int argc, char **argv) { cout << "Hello World\n"; std::unique_ptr<PDNS_RECORD*, decltype(pdnsDeleter)> results(0, pdnsDeleter); if (DnsQuery(L"google.com", DNS_TYPE_A, DNS_QUERY_STANDARD, NULL, ??results??, NULL)) { cout << "google.com -> " << ??results??; } cout << "Done\n"; getchar(); return 0; }
Thanks!
c ++ winapi dynamic-memory-allocation
Jason kleban
source share