帮助 ctypes.windll.dnsapi.DnsQuery_A
我在使用 [DnsQuery](http:// 时遇到问题msdn.microsoft.com/en-us/library/ms682016(VS.85).aspx) 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, 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")
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")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不是一个指向
DNS_RECORD
的指针吗?这意味着您必须将 rr 初始化为 POINTER(DNS_RECORD)() 并通过引用传递它:ctypes.byref(rr)。更新:但我认为您看到的问题是通过传递
server_arr
:您传递第一个字段为0x00000001
的结构,而不是对此结构的引用,因此 C 代码尝试取消引用AddrCount
字段并导致访问冲突。server_arr
也应该使用相同的技术。Isn't it a pointer to pointer to
DNS_RECORD
? This means you have to initialize rr asPOINTER(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 being0x00000001
instead of reference to this structure, so C code tries to dereferenceAddrCount
field and gives you access violation. The same technique should be used forserver_arr
too.