python - 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
. 你能帮我为函数定义正确的数据类型吗?
解决方案
这应该有效:
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)
推荐阅读
- python - Numpy:对数组进行排序并找到相应索引的有效方法
- java - 如何仅显示文件更改事件 Java 的一条消息
- python - “是”没有定义?有点迷茫
- google-apps-script - 脚本编辑器接受的内容和 Google 表格接受的内容可能不同?
- r - 在地图中使用starts_with而不是显式命名
- cxf - 在 java-11 中工作的 CXF-SOAP gradle-plugin?
- intellij-idea - 如果intellij中的长参数如何更改换行
- c# - 为每个可能的排列填充 3x3 位立方体
- c - 如何区分 RTOS 和 Linux?
- r - 如何计算地铁站的乘客数量?