Python ctypes,dll函数参数

时间:2019-02-21 21:55:13

标签: python dll ctypes

我有一个带有功能的DLL

EXPORT long Util_funct( char *intext, char *outtext, int *outlen )

看起来像期望char * intext,char * outtext,int * outlen。 我试图在python中定义不同的数据类型,以便我可以传递参数,但到目前为止没有成功。

from ctypes import *

string1 = "testrr"
#b_string1 = string1.encode('utf-8')

dll = WinDLL('util.dll')
funct = dll.Util_funct

funct.argtypes = [c_wchar_p,c_char_p, POINTER(c_int)]
funct.restype = c_char_p

p = c_int()
buf = create_string_buffer(1024)
retval = funct(string1, buf, byref(p))

print(retval)

输出为None,但是我看到p中有一些更改。 您能帮我为函数定义合适的数据类型吗?

2 个答案:

答案 0 :(得分:0)

这应该有效:

from ctypes import *

string1 = b'testrr'     # byte string for char*

dll = CDLL('util.dll')  # CDLL unless function declared __stdcall
funct = dll.Util_funct

funct.argtypes = c_char_p,c_char_p,POINTER(c_int) # c_char_p for char*
funct.restype = c_long # return value is long

p = c_int()
buf = create_string_buffer(1024) # assume this is big enough???
retval = funct(string1, buf, byref(p))

print(retval)

答案 1 :(得分:-1)

感谢您的所有回答! 我想我明白了。不是使用最聪明的方法,而只是尝试/试验不同的数据类型。 由于这不是一个普通的图书馆,而且我也没有信息,所以也许对其他人来说,这种解决方案不是很有用,但是无论如何。

该函数一次只能处理一个字符,因为如果我传递一个单词,它只会返回一个编码字符。 所以这里是:

from ctypes import *


buf = create_unicode_buffer(1024)
string1 = "a"
c_s = c_wchar_p(string1)

dll = CDLL('util.dll')
enc = dll.Util_funct

enc.argtypes = c_wchar_p, c_wchar_p, POINTER(c_int)

enc.restype = c_long # i don't think this type matters at all

p = c_int()


enc(c_s, buf, byref(p))


print(p.value)
print(buf.value)

输出为1,simbol ^

再次感谢