python - Function returns pointer to array with result. How to access this data -
i have function imported dll , defined in ctypes. set restype
c_void_p
. when call function, result returns pointer 32byte array of bytes. how can convert pointer python bytes type?
here function (it's hmac
openssl):
self.hmac = self._lib.hmac self.hmac.restype = ctypes.c_void_p self.hmac.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p]
edit: i've tried replacing c_void_p
c_char_p
. results, c_char_p
represents null terminated string, results cut @ point of first 00h. if hmac
not have zeros in result works perfectly. still not solve problem.
using ctypes.c_void_p
return type wrong thing do.
for non-nul-terminated string, should set array of chars of right size; 32 in case, ctypes.c_char * 32
. restype
pointer this, wrap in ctypes.pointer
.
you can access returned value bytes
object using value
(or raw
; it's same thing in context) attribute of pointer's contents
.
here's practical example using crypt(3)
, returns 13-character string:
>>> import ctypes >>> crypt = ctypes.cdll('libcrypt.so').crypt >>> crypt.argtypes = ctypes.c_char_p, ctypes.c_char_p >>> crypt.restype = ctypes.pointer(ctypes.c_char * 13) >>> crypt('tea', 'ea') <__main__.lp_c_char_array_13 object @ 0x7f7d464f2200> >>> crypt('tea', 'ea').contents <__main__.c_char_array_13 object @ 0x7f7d464f2290> >>> crypt('tea', 'ea').contents.value b'eauwokonzwxw2'
Comments
Post a Comment