gpt4 book ai didi

python - C函数执行在哪里停止

转载 作者:行者123 更新时间:2023-11-30 19:44:24 25 4
gpt4 key购买 nike

在我的项目文件夹中:

libtest.so
test.h

我已经在我的Python代码中导入了ctypes(不确定是否有必要),并且加载了.so文件,如下所示:

lib = cdll.LoadLibrary('./libtest.so')

在“test.h”C 头文件中,我有:

long TEST_API test( 
___OUT_ char text[41],
___OUT_ char* pcReceiptSignature,

);

我从 python 代码中调用“test”,如下所示:

class StructA(Structure):
_fields_ = [("text", c_char*41),
("pcReceiptSignature",c_char*1),
]



sampleStruct = StructA("","")

def test2():

res = lib.test(sampleStruct.text, sampleStruct.pcReceiptSignature)
return res

但执行在某处失败,我从未到达“return res”行。

对于在结构化中定义的 c_chars 指定的长度,我也尝试不给它们长度(没有变化)。

我在这里做错了什么。有没有办法查看执行失败的地方?

最佳答案

您确实应该在调试器下运行代码,使用 -g 等编译 .so

但是原始代码中的一个明显错误是 char* pcReceiptSignature 被声明为 out 参数?因此我猜你想为其保留一些空间,由函数填充,但没有可用的空间(你正在使用 c_char_p 而不是数组!)。 pcRpcReceiptSignature 是一个指向常量字符串 "" 的指针,因此覆盖它并不是很安全。

关于python - C函数执行在哪里停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263602/

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