帮助 ctypes.windll.dnsapi.DnsQuery_A

发布于 2024-08-12 07:16:42 字数 822 浏览 3 评论 0原文

我在使用 [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2024-08-19 07:16:42

它不是一个指向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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文