python中的ctypes库初探

python ctypes

Table of Contents

1 Python中调用C语言库

Python中提供了ctypes标准库,用来调用C语言库中的函数。

  1. 加载dll C语言函数存在不同的调用约定,常见的有stdcall和cdecl,这两种调用约定在Python中的调用函数需要区分:
    • stdcall的加载方式: dll = windll.LoadLibrary("dllpath") dll = WinDLL("dllpath")
    • cdecl的加载方式: dll = cdll.LoadLibrary("dllpath") dll = CDLL("dllpath")
  2. 调用函数,传入参,返回值 C函数通过参数读取值,做处理后通过返回值传回。
    #include <stdlib.h>
    // file add.c
    #include <stdio.h>
    
    int add(const int a, const int b)
    {
        return a+b;
    }
    
    

    把C函数编译为.so库,执行一下命令: gcc -shared -fPIC -o libadd.so add.c

    from ctypes import *
    
    dll = CDLL("./libadd.so")
    
    print("add(1,2) = ", dll.add(1, 2))
    
    

    执行Python命令,查看执行情况: python3 ./add.py add(1,2) = 3

  3. C函数通过参数传出值,即参数是指针的情况 C语言指针概念在ctypes中用pointer对应,如下示例:
    #include <stdlib.h>
    // file add.c
    #include <stdio.h>
    
    int add(const int a, const int b)
    {
        return a+b;
    }
    
    
    int add2(const int a, const int b, int *c)
    {
        if (NULL != c)
            *c = a+b;
    
        return a+b;
    }
    

    add2函数传入指针,并可通过指针输出值。 python调用函数如下:

    from ctypes import *
    
    dll = CDLL("./libadd.so")
    
    print("add(1,2) = ", dll.add(1, 2))
    
    out = c_int(0)
    ptr = pointer(out)
    
    out.value = 100
    print("out.value = ", out.value)
    dll.add2(2,3,ptr)
    print("add2(2, 3) = ", out.value)
    
    

    python3 ./add.py add(1,2) = 3 out.value = 100 add2(2, 3) = 5

  4. 参数是结构体指针 结构体通过继承Structure类实现,各字段通过field定义,示例如下:
    typedef struct
    {
        int x;
        int y;
    } Point;
    
    
    int add3(const Point a, const Point b, Point *c)
    {
        if (NULL == c)
            return 255;
        c->x = a.x + b.x;
        c->y = a.y + b.y;
        return 0;
    }
    
    

    Python中的定义如下:

    class Point(Structure):
        _fields_ = ("x", c_int), ("y", c_int)
        def __repr__(self):
            return "(%d, %d)" % (self.x, self.y)
    
    p1 = Point(1,2)
    p2 = Point(5, 8)
    p3 = Point(0,0)
    ptr_p3 = pointer(p3)
    
    dll.add3(p1,p2,ptr_p3)
    print("add3(p1,p2) = ", p3)
    
    

    其中_repr_是python的内置函数,这个是类的展示函数,相当于java语言中的toString()函数。

  5. 入参char*指针
    int pr(const char *name, char *out)
    {
        printf("Hello, %s\n", name);
        sprintf(out, "Hello, %s\n", name);
        return 0;
    }
    
    
    • 方法1
      sbuf = create_string_buffer(b'robin')
      out  = create_string_buffer(100)
      dll.pr(sbuf, out)
      print("1--", out.value)
      
      
    • 方法2
      sbuf2 = b'\0'*10
      pStr = c_char_p()
      pStr.value = sbuf2
      dll.pr(b'John', pStr)
      print("2--", sbuf2)
      
      
    • 方法3
      sbuf3 = b'\0'*20
      dll.pr(b'Rich', sbuf3)
      print("3--", sbuf3)
      
      
  6. 字节序处理 fields定义的每个参数的类型后面可以定义bit位,示例如下:
    int swap32(const int x)
    {
        return htonl(x);
    }
    
    

    Python中测试字节序如下:

    class Int(Structure):
        _fields_ = [("uint8_0", c_int, 8),
                    ("uint8_1", c_int, 8),
                    ("uint8_2", c_int, 8),
                    ("uint8_3", c_int, 8)]
    
        def __init__(self, i):
            i0 = i & 0xFF
            i1 = (i >> 8) & 0xFF
            i2 = (i >> 16) & 0xFF
            i3 = (i >> 24) & 0xFF
            super().__init__(i0,i1,i2,i3)
    
        def __repr__(self):
            return "0x%08X" % ((self.uint8_3<<24) + \
                (self.uint8_2<<16) + \
                (self.uint8_1<<8) + \
                (self.uint8_0))
    
    
    i = Int(0x12345678)
    i_big = dll.swap32(i)
    print( i, "0x%08X" % i_big)
    
    

    这里使用Int主要是为了测试bit的定义,这个用例用Python内置的int类型ibig=dll.swap32(0x12345678)就可以。

Date: 2013-07-28 17:56:36 中国标准时间

Author:

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值