Python ctypes,dll函数参数

问题描述 投票:0回答:2

我有一个带功能的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有一些变化。你能帮我定一下这个功能的正确数据类型吗?

python dll ctypes
2个回答
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
投票

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

看起来函数一次只处理一个字符,因为如果我传递一个字,它只返回一个编码字符。所以这里是:

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 ^

再次感谢

© www.soinside.com 2019 - 2024. All rights reserved.