当前位置: 代码迷 >> 综合 >> ctypes 函数指针作为参数
  详细解决方案

ctypes 函数指针作为参数

热度:1   发布时间:2023-12-22 02:44:57.0
  1. 函数指针作为参数赋值

    • 案例

      #include<stdio.h>
      void show() {
                printf("%s\n",__FUNCTION__);
      }void cool(void(**s)()){
                *s = &show;
      }
      ~
      • 赋值的是show

    • 文件名编译

      • gcc -fPIC --shared test.c -o test.so

  2. python

    • 代码

      import ctypes
      VOID_T = ctypes.CFUNCTYPE(None)
      t = VOID_T()handle=ctypes.CDLL("./test.so")
      handle.cool.argtypes=[ctypes.POINTER(VOID_T)]
      handle.cool(ctypes.pointer(t))
      t()
    • 执行

      • 输出函数名show.