views:

116

answers:

1

Hi,

I have trouble with DnsQuery API, the *ppQueryResultsSet parameter troubles me. Can anyone show me an example of how to make correct DLL calls in python?

import ctypes
from ctypes import wintypes
from windns_types import DNS_RECORD, IP4_ARRAY #declared here http://pastebin.com/f39d8b997


def DnsQuery(host, type, server, opt=0):
    server_arr = IP4_ARRAY()
    rr = DNS_RECORD()
    server_arr.AddrCount=1
    server_arr.AddrArray[0] = ctypes.windll.Ws2_32.inet_addr(server)
    ctypes.windll.dnsapi.DnsQuery_A(host, type, opt, server_arr, rr, 0)
    # WindowsError: exception: access violation reading 0x00000001

    return rr

print DnsQuery("www.google.com", 1, "208.67.222.222")
+2  A: 

Isn't it a pointer to pointer to DNS_RECORD? This means you have to initialize rr as POINTER(DNS_RECORD)() and pass it by reference: ctypes.byref(rr).

Update: But I think the problem you see is from passing server_arr: you pass a structure with first field being 0x00000001 instead of reference to this structure, so C code tries to dereference AddrCount field and gives you access violation. The same technique should be used for server_arr too.

Denis Otkidach
Why it's `ctypes.POINTER(DNS_RECORD)()` not `ctypes.pointer(DNS_RECORD())` ? And look like still access violation reading 0x00000001
est
I the C world allocating a pointer to structure and allocation a structure and getting a pointer to it is not the same. While `ctypes.pointer(DNS_RECORD())` should work too, I'm not sure it's correct way.
Denis Otkidach
@Denis Otkidach,It works. Thank you very much!
est