[d]python如何获得C函数的返回值

yu332045275 2012-07-25 03:12:31
向各位请教问题:
我有C代码的头文件和已封装的DLL文件。
头文件中有如下的C代码:

/* Declaration of generic filter hndle, which represents a particular filter */
typedef struct fft_Filter_s* fft_FilterHandle;

/*
* create filter
*/
CWRAPPERS_EXPORT fft_FilterHandle FFTAPI fft_Create_Filter();
/*
* check if download is allowed, filter has to be previously configured
*/
CWRAPPERS_EXPORT char FFTAPI fft_Verify_Download_Allowed(fft_FilterHandle pHandle);


我用python加载了DLL文件为inter_object,对这两个函数进行封装:

@classmethod
def py_fft_create_filter(cls):
fft_create_filter = inter_object.fft_Create_Filter
fft_filter_handle = fft_create_filter()
return fft_filter_handle

@classmethod
def py_fft_verify_download_allowed(cls, p_handle):
fft_verify_download_allowed = inter_object.fft_Verify_Download_Allowed
value = fft_verify_download_allowed(p_handle)
return value


在python的代码中,我调用函数py_fft_create_filter获得其返回值fft_filter_handler,使用pycharm调试时,查看他的值是int型45877328,由于我的另一个函数py_fft_verify_download_allowed将直接使用这个参数,所以我并没有在意这个值的value和他的类型。
我调用py_fft_verify_download_allowed并获得其返回值

return_value = py_fft_verify_download_allowed(p_filter_handle)

在C的代码中,这个函数的返回类型是char,返回值是0或者1,相当于bool型的true和false,但是我用python获得的这个return_value这个值却是int型的769,或者768.

我该如何处理,才能让python代码中的return_value获得的值就是C代码的返回值char类型的0或1呢

--------------------------
Double行动:
原帖分数:40
帖子加分:40
...全文
374 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_su 2012-07-26
  • 打赏
  • 举报
回复
默认返回值是整数,不是的话,属性restype自己设置一下,看看文档吧...

15.17.1.8. Return types
By default functions are assumed to return the C int type. Other return types can be specified by setting the restype attribute of the function object.
...
yu332045275 2012-07-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
15.15.1.4. Fundamental data types
ctypes defines a number of primitive C compatible data types :

ctypes type C type Python type
c_char char 1-character string
[/Quote]
谢谢,我貌似发现了什么,有点眉目了
yu332045275 2012-07-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
15.15.1.4. Fundamental data types
ctypes defines a number of primitive C compatible data types :

ctypes type C type Python type
c_char char 1-character string
[/Quote]
还请明示,我确实也使用了ctypes中的类型,但我只是在需要将参数传给C的时候才使用,比如需要将int型数据100作为参数传给C,我会用function(c_int(100)).
但是如何获取到C返回的值并将其转成对应的python数据呢?
C方法fft_Verify_Download_Allowed(fft_FilterHandle pHandle)的返回值应该是char但是我用python获取到的值是int,该如何转换呢?
Gloveing 2012-07-25
  • 打赏
  • 举报
回复
15.15.1.4. Fundamental data types
ctypes defines a number of primitive C compatible data types :

ctypes type C type Python type
c_char char 1-character string

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧