c调python_应该是史上最全的python调用C接口

在这段时间里,我需要使用python调用C接口。我在网上搜索了很多,再加上python的官方文件,并组织备份1。从ctypes加载dll导入* dll = () # func1dll = CDLL () # func2print (dll) 2。type3的数据通信。函数调用CDLL1_API int fnDLL1 (void){返回42;}Pythonprint (()) 4。参数传递CDLL1_API int fnDLL2 (int,浮动b,双c, const char *缓冲区,int研发){printf (\时您可以使用byref(临时)Pythontemp = c_int(0)打印((1,c_float (), c_double (),\u2018hell0 .

例如,指针(临时)type_p_int =指针(c_int) temp = type_p_int (c_int(0))打印((1,c_float (), c_double (),\u2018hell0 .encode (gbk)、临时))打印(int *,临时,临时[0])返回值int,浮动,双可以直接接收这些类型,其他类型需要设置接口返回类型CDLL1_API char * fnDLL3 (char * buf){返回缓冲区;}python = c_char_pres = (\u201Chello\u201D.encode (gbk))打印(res,类型(res))如果char *转移,其内容需要更改,需要事先定义好的存储空间CDLL1_API int fnDLL4 (char *缓冲区,size_t buffsize){printf (\结构定义我们使用字段= [(name1,类型1)(\u201Cname2\u201D,类型2)]显示的字节对齐的成员结构。

身体,使用包指定对齐的字节数,定义数组的直接使用* num表明c#编译指示包装数量(1)struct MyStruct {int;双b; char C [32];}; # pragma包()pythonclass MyStruct(结构):_fields_ = [(a, c_int) (b, c_double) (\u201CC\u201D, c_char * 32)] _pack_ = 1比特字段Cstruct MyStruct1 (int: 16;int b: 16;}; pythonclass MyStruct1(结构):_fields_ = [(a c_int 16) (b c_int 16))嵌套结构cstruct MyStruct2 (int, MyStruct S [4];

pythonclass MyStruct2(结构):_fields_ = [(a, c_int)(的struct MyStruct * 4)]通过结构,同前面的参数,指针类型时使用byref或pointercDL L1_API int fnDLL5 (MyStruct & S){printf (\时编码(gbk) (byref (mystruct))(指针(mystruct))返回的结构,和之前一样,您需要指定返回类型cDLL1_API mystruct fnDLL6 () {mystruct * tem =新mystruct;返回* tem;} python = MyStructres =()打印(res)打印(\u201Cmystruct:\u2018,,,) del res的高阶int数组定义my_array[10][10];

contentsprint (res,类型(res))打印(mystruct:) def包(ctype_instance):返回string_at (addressof (ctype_instance), sizeof (ctype_instance)) def解压(ctype buf):断言sizeof (ctype) = = len (buf)装运箱= create_string_buffer (buf)返回(指针(装运箱),指针(ctype))。回调函数内容首先使用CFUNCTYPE定义回调函数类型,第一个参数的参数返回值类型以下参数由回调函数传递的参数类型,然后定义函数在python中,Ctypedef int (* callbakc) (int, int b);

printf (\常用的,比如通过一个字节流,我们需要通过字节流的长度和一个指针指向第一个地址。如果你直接使用参数,c_void_p int类型。和c_char_p字节到最后' \\ 0 ',最后我们在python中使用string_at来获得实际的字节流c调typedef无效(* callbakc) (void * buf, int &buf_size);定义在python def回调(buf、大小):字符串= string_at () CALLBACKFUNC = CFUNCTYPE(没有,c_void_p c_int)调用= CALLBACKFUNC(回调)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值