python ctypes 抛出错误?

发布于 2024-10-21 00:49:32 字数 742 浏览 1 评论 0原文

到目前为止,我已经得到了一个不适合 python 使用的 DLL,并且类型返回:我只是无法传递它的参数,因为我做错了,而且我不太明白有关如何操作的文档我应该做一些事情。基本上,我正在测试的 DLL 中的函数名为“iptouint”。它接受一个 c_char_p 并返回一个 c_double。

这是我的代码:

nDll = ctypes.WinDLL('ndll.dll')

nDllProto = ctypes.WINFUNCTYPE(ctypes.c_double)
nDllInit = nDllProto(("dllInit", nDll))

nDllTestProto = ctypes.WINFUNCTYPE(ctypes.c_double,ctypes.c_char_p)
nDllTest = nDllTestProto(("iptouint",nDll),((1, "p1",0),(1, "p2",0)))

#This is the line that throws the error:
print("IP: %s" % nDllTest("12.345.67.890"))

'''
It gives me the error:
ValueError: paramflags must have the same length as argtypes
Im not sure what to do; Ive certainly played around with it to no avail.
Help is much appreciated.
Thanks.
'''

so far I've gotten a DLL that wasn't meant for python to work with it, and types return: I just can't pass it arguments because I'm doing it wrong and I don't quite understand the documentation on how I'm supposed to do things. Basically, the function in the DLL I'm testing is named "iptouint". It takes a c_char_p and returns a c_double.

Here is my code:

nDll = ctypes.WinDLL('ndll.dll')

nDllProto = ctypes.WINFUNCTYPE(ctypes.c_double)
nDllInit = nDllProto(("dllInit", nDll))

nDllTestProto = ctypes.WINFUNCTYPE(ctypes.c_double,ctypes.c_char_p)
nDllTest = nDllTestProto(("iptouint",nDll),((1, "p1",0),(1, "p2",0)))

#This is the line that throws the error:
print("IP: %s" % nDllTest("12.345.67.890"))

'''
It gives me the error:
ValueError: paramflags must have the same length as argtypes
Im not sure what to do; Ive certainly played around with it to no avail.
Help is much appreciated.
Thanks.
'''

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

两个我 2024-10-28 00:49:32

尝试简单地指示 ctypes 它所采用的 argtypes 以及它返回的 argtypes:

nDll = ctypes.WinDLL('ndll.dll')
nDll.restype = ctypes.c_double
nDll.argtypes = [ctypes.c_char_p]

result = nDll.iptouint("12.345.67.890").value

尽管如此,请考虑以下几点:

1) 如果如名称所示,这会将字符串中的 IPv4 值 s 转换为 Unsigned Int,则返回类型 os 不是“double”正如你所说 - 它将是 ctypes.c_uint32

2)你的示例值不是有效的 IPv4 地址,并且无法转换为 32 位整数(它作为“双精度”也没有意义 - 即 64 位浮点数) - 它根本无效

3) 如果您只是想在 Python 中为 ipv4 地址获取无符号 32 位值,则不需要它。有很多非常易读、更简单且跨平台的方法可以使用纯 python 来做到这一点。例如:

def iptoint(ip):
   value = 0
   for component in ip.split("."):
       value <<= 8  #shifts previous value 8 bits to the left, 
                    #leaving space for the next byte
       value |= int(component)  # sets the bits for the new byte in the address
   return value

更新
在 Python 3.x 中有 ipaddress 模块 -
https://docs.python.org/3/library/ipaddress.html - 也可以作为 Python 2.x 的 pip 安装使用 - 它总是可以以正确的方式处理这个问题,并且也可以与 IPv6 一起正常工作。

try simply indicating ctypes the argtypes it takes and the ones it returns:

nDll = ctypes.WinDLL('ndll.dll')
nDll.restype = ctypes.c_double
nDll.argtypes = [ctypes.c_char_p]

result = nDll.iptouint("12.345.67.890").value

Although, consider these points:

1) if, as the name indicates, this converts an IPv4 value s ina string to an Unsigned Int, the return type os not "double" as you say - it would be ctypes.c_uint32

2) Your example value is not a valit IPv4 address, and cannot be converted to a 32 bit integer (neither does it makes sense as a "double" - i.e. a 64 bit floating point number) - it is simply invalid

3) You don't need that if you are just trying to have an unsigned 32bit value for an ipv4 address in Python. There are quite a few, very readable, easier, and multiplatform ways to do that with pure python. For example:

def iptoint(ip):
   value = 0
   for component in ip.split("."):
       value <<= 8  #shifts previous value 8 bits to the left, 
                    #leaving space for the next byte
       value |= int(component)  # sets the bits for the new byte in the address
   return value

update:
In Python 3.x there is the ipaddress module -
https://docs.python.org/3/library/ipaddress.html - which is also available as a pip install for Python 2.x - it can handle this always in the correct way, and works fine with IPv6 as well.

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