gpt4 book ai didi

python - ctypes 字符串指针类型错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:53 24 4
gpt4 key购买 nike

这是一个调用 DLL 的 ctypes 包装器。它将简单的文本文件读取为字节数组(utf-8 编码),创建相同长度的字符串缓冲区,并将两者传递给 DLL。使用下面的代码,我收到错误:预期 LP_c_char_p 实例而不是 c_char_Array_8049。

我还尝试将两个数组作为 ctypes.c_byte 传递,但这也不起作用。

file_name = r"C:\Projects\--Data_Files\Strings\Sample_Text.txt"
f = io.open(file_name, mode="r", encoding="utf-8")
CA_my_str = f.read()

CA_no_punct = ctypes.create_string_buffer(len(CA_my_str))

Input_Length_Array = []
Input_Length_Array.append(len(CA_no_punct))
Input_Length_Array.append(len(CA_my_str))

length_array_out = (ctypes.c_double * len(Input_Length_Array))(*Input_Length_Array)

hDLL = ctypes.WinDLL("C:/NASM_Test_Projects/String_Processing/String_Processing.dll")
CallName = hDLL.Main_Entry_fn
CallName.argtypes = [ctypes.POINTER(ctypes.c_char_p),ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_double)]
CallName.restype = ctypes.POINTER(ctypes.c_double)

ret_ptr = CallName(CA_no_punct, CA_my_str, length_array_out)

所以问题是:两个数组 CA_my_str 和 CA_no_punct 的正确 ctypes argtype 是什么?

编辑:根据下面的请求,这是 NASM 中的入口点:

Main_Entry_fn:
push rdi
push rbp
mov [no_punct_ptr],rcx
mov [my_str_ptr],rdx
mov [data_master_ptr],r8
; Now assign lengths
lea rdi,[data_master_ptr]
mov rbp,[rdi]
xor rcx,rcx
movsd xmm0,qword[rbp+rcx]
cvttsd2si rax,xmm0
mov [no_punct_length],rax
add rcx,8
movsd xmm0,qword[rbp+rcx]
cvttsd2si rax,xmm0
mov [my_str_length],rax
add rcx,8
call String_Processing_fn
exit_label_for_Main_Entry_fn:
pop rbp
pop rdi
ret

最佳答案

c_char_p 对于两个数组参数都是正确的,相当于 C 的 char*:

CallName.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(ctypes.c_double)]

使用CA_no_punct.raw查看输出缓冲区的完整内容。

关于python - ctypes 字符串指针类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49181097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com