首页 > 解决方案 > Python ctypes,dll函数参数

问题描述

我有一个带函数的 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)

输出为无,但我看到p. 你能帮我为函数定义正确的数据类型吗?

标签: pythondllctypes

解决方案


这应该有效:

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)

推荐阅读