使用ctypes调c动态库传参,c库解析乱码
如题,使用ctypes调用so库中的接口,两个接口的参数相同,都是字符串一个接口返回值为int,参数utf-8编码后传给so库,so库解析参数正确,接口正常调用;
另一个调用返回值为指针类型,参数utf-8编码后传给so库,so库解析参数却是乱码。
真是奇怪
附示例源码:
接口1print的信息正确,接口2print的却是乱码
so库封装源码示例:
int c_dev_unbind(char *dev_id)
{
printf("接口1的参数:%s",dev_id);
return dev_unbind(dev_id);
}
DEV_DESC_IF_S *dev_desc_get(char *dev_id)
{
printf("接口2的参数:%s",dev_id);
DEV_DESC_IF_S *dev_ifs = dev_desc_get(dev_id);
return dev_ifs;
}
python调用
def py_dev_unbind(dev_id):
dev_id = dev_id.encode() if isinstance(dev_id, str) else dev_id
method = sdk.c_dev_unbind
method.argtypes =
method.restype = c_int
res = method(dev_id)
return res
def py_dev_desc_get(dev_id):
dev_id = dev_id.encode() if isinstance(dev_id, str) else dev_id
method = sdk.c_dev_desc_get
method.argtypes =
method.restype =
res = method(dev_id)
return res
已找到原因
页:
[1]