Python 帮助ctypes.windell.dnsapi.DnsQuery\u A
我在[DnsQuery](API,*ppQueryResultsSet参数方面遇到了问题。有人能给我举个例子,说明如何在python中进行正确的DLL调用吗Python 帮助ctypes.windell.dnsapi.DnsQuery\u A,python,winapi,dns,ctypes,Python,Winapi,Dns,Ctypes,我在[DnsQuery](API,*ppQueryResultsSet参数方面遇到了问题。有人能给我举个例子,说明如何在python中进行正确的DLL调用吗 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, op
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")
它不是指向
DNS\u记录的指针吗?
?这意味着您必须将rr初始化为指针(DNS\u记录)(
),并通过引用传递它:ctypes.byref(rr)
更新:但我认为您看到的问题是传递
服务器arr
:您传递的结构的第一个字段是0x00000001
,而不是引用此结构,因此C代码尝试取消对AddrCount
字段的引用,并给您访问冲突。同样的技术应该用于server_arr
也是。@ChristopheD,我真的很想,但看起来没有人回答。为什么是ctypes.POINTER(DNS_记录)(
不是ctypes.POINTER(DNS_记录())
?看起来仍然存在访问冲突读取0x00000001I C world分配一个指向结构的指针和分配一个结构并获取指向它的指针是不同的。虽然ctypes.pointer(DNS_RECORD())
也应该工作,但我不确定这是正确的方法。