Help ctypes.windll.dnsapi.DnsQuery_A
Question
I have trouble with [DnsQuery](http://msdn.microsoft.com/en-us/library/ms682016(VS.85).aspx) 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")
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow