Thread: Strange results using dnsapi and windns

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    Strange results using dnsapi and windns

    hello, I have been reading the thread opened by hiya about 'sending email', and I have been interested on the MX lookup, introduced by anonytmouse. Maybe that's a strange problem, but the first time I compiled the code (DevC++&&MingW) it worked well; ok, I copy it to another pc, try to run and there's no problem; then I shuted down both pc, restart them and there's the strange thing: on the pc where I compiled the code does not work the ms verification, and the second strange thing on the other pc it runs correctly. what's that trick? The only difference between both pc is that in the pc where still runs the program there's installed and running the ms messenger. I thought that may be I should use a function to load the dnsapi (something like the 'InitCommonControls()' for the 'commctrl.h' from the win32 api).
    Then here comes the new strange thing; while my 2-days-old code still doesn't work, I have find a second sample (a bit different from the anonytmouse's post 'http://cboard.cprogramming.com/showthread.php?p=461428'), and now that is runnig well; that's how it looks like:

    Code:
    PDNS_RECORD result;
    DnsQuery("theserver", DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &result, NULL );
    for(PDNS_RECORD i = result; i != NULL; i = i->pNext)
            printf("%s, %i\n", i->Data.MX.pNameExchange, i->Data.MX.wPreference);
    but still a new strange thing, I deleted the PDNS_...etc, and the for loop from that code, write instead the next:

    Code:
    DNS_RECORD* pRecordList=NULL;
    DNS_RECORD* pRecord=NULL;
    DnsQuery_A("megacceso.com",DNS_TYPE_MINFO,DNS_QUERY_STANDARD,NULL,&pRecordList,NULL);
    for (pRecord=pRecordList;pRecord!=NULL;pRecord=pRecord->pNext)
    	{
             printf("Domini:\t\t%s\n",(char*)pRecord->pName);
            }
    and (might be a magic tric that I cann't see), also works well, while the 2-day-old code doesn't work, and is the same as that new one. How can I solve that problem? Should I force the load of a library? The load of a dll?
    But that's not all; I shut down the pc, restart it and: now none of both codes works!!! And the best thing, the 2-day-old code that I copied to a second pc is still working well, no worry about shutdown-restart pc.
    I have been seraching on the msdn for something to init, or load, but I have find anything, there's no 'InitDnsApi()' function. Also I have find a second 'force-to-load' bit of code, that's:

    Code:
    typedef DNS_STATUS (WINAPI *f_DnsQuery)(
    	PCSTR lpstrName,
    	WORD wType,
    	DWORD fOptions,
    	PIP4_ARRAY aipServers,
    	PDNS_RECORD* ppQueryResultsSet,
    	PVOID* pReserved);
    f_DnsQuery m_DnsQuery;
    m_DnsQuery=(f_DnsQuery)GetProcAddress(LoadLibrary("Dnsapi"),"DnsQuery_A");
    but the program does not work. Have someone find in a simmilar strange cases?

    Thank's in advance
    Niara

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Usually when I have these baffling problems of code failing when it originally worked, I find it is because I have introduced a subtle error. This is probably why the program on the other computer continues to work. I don't think you need to initialise the DNS api in any way.

    A couple of comments on the code you posted:
    Code:
    PDNS_RECORD result;
    DnsQuery("theserver", DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &result, NULL );
    for(PDNS_RECORD i = result; i != NULL; i = i->pNext)
            printf("%s, %i\n", i->Data.MX.pNameExchange, i->Data.MX.wPreference);
    The MX member of the Data union structure is only valid when the wType field is DNS_TYPE_MX. If you try to access it for other record types, your program may crash.
    Code:
    DNS_RECORD* pRecordList=NULL;
    DNS_RECORD* pRecord=NULL;
    DnsQuery_A("megacceso.com",DNS_TYPE_MINFO,DNS_QUERY_STANDARD,NULL,&pRecordList,NULL);
    for (pRecord=pRecordList;pRecord!=NULL;pRecord=pRecord  ->pNext)
    	{
             printf("Domini:\t\t%s\n",(char*)pRecord->pName);
            }
    Did you mean to use DNS_TYPE_MINFO for the query type?

    You could try recopying and compiling my original code and seeing if that fails.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello anonytmouse, thank's for your observations; maybe I done a wrong explanation: the program does't crash, but the result of the query is a pRecordList=0x0 (such as pRecord, so theresn't any reason for the 'for' loop). I try it with yahoo.com domain.
    After doing it, I have opened a new project (DevC++), recopy-paste your code on the 'MX lookup' thread 'http://cboard.cprogramming.com/showthread.php?p=461428' (I won't post it because is the same, copied & pasted), link with the MingW adapted lib 'libdnsapi.a', recompiled and now the result of the query is pRecordList=0x244c98. But that's not the strange thing, 3 days ago I have done the same and now (the same code, recompiled, deleted the *.o files, etc) it creates a pRecordList=0x0. I'm afraid that when I close the pc, and restart tomorrow, the project that I have buit now (2 minutes ago with your code copied-pasted) will create the same 0x0 pRecordList, the same thing that ocurred with the first compiled code 3 days ago (that initially worked well but now gives a 0x0 list). And the strangest thing, the program that I first compiled 3 days ago and I copied into another pc (same OS and version) is still runing only in the other pc. Do you want to read a more rare thing? If I copy the program from the other pc to my own pc and try to run it, the list is now 0x0!!! Still another: (I have that second pc (the one where the program runs) beside my own, so I can do those tests easily), if I try to run both programs one after the other (no worry which first), from the other pc the list is != 0x0, but from mine is 0x0 over the same domain name. Is for that reason that I thought I should initialize the api before do the query. The only difference between pc is that in the other pc is running ms messenger (I'm not sure about create a program with a 'readme' file looking like 'msn needed to work properly' )

    I will shut down the pc, and tomorrow I'll see if the 'new' program will return a 0x0 list.
    Thanks for your time and observations.
    Niara

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello again, at this point I'm not sure about nothing. All the previously programs are working now, without making any pc new-settings, nor new installations, no modifications. Hope that tomorrow they'll be still working... There's no 'CloseDNSQuery()' function no?
    For the moment I'll continue with the project.
    Thank's for your time
    Niara

Popular pages Recent additions subscribe to a feed