开发者

python ctypes throwing error?

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" % nDl开发者_C百科lTest("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.
'''


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜