欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中ctypes使用方法

 更新時(shí)間:2024年04月24日 11:36:49   作者:平時(shí)不搬磚  
這篇文章主要介紹了python中ctypes使用方法,包括 python和c中類型映射,操作結(jié)構(gòu)體和聯(lián)合體,需要定義結(jié)構(gòu)體或聯(lián)合體的類型,然后可以創(chuàng)建實(shí)例、訪問(wèn)其成員等,感興趣的朋友跟隨小編一起看看吧

前段時(shí)間接到了一個(gè)需求是給一個(gè)藍(lán)牙的SDK測(cè)試接口的穩(wěn)定性,將SDK的接口文檔給你了,需要每個(gè)接口都寫一個(gè)對(duì)應(yīng)的測(cè)試用例,SDK 是用c寫的,而我python用的比較熟練些,所有記錄下在ctypes庫(kù)的使用方法。

1 python和c中類型映射

ctypes中數(shù)據(jù)類型

ctypes 類型C 類型Python 數(shù)據(jù)類型
c_bool_Boolbool (1)
c_charchar單字符字節(jié)串對(duì)象
c_wcharwchar_t單字符字符串
c_bytecharint
c_ubyteunsigned charint
POINTER(c_ubyte)uchar*int
c_shortshortint
c_ushortunsigned shortint
c_intintint
c_uintunsigned intint
c_longlongint
c_ulongunsigned longint
c_longlong__int64 或 long longint
c_ulonglongunsigned __int64 或 unsigned long longint
c_size_tsize_tint
c_ssize_tssize_t 或 Py_ssize_tint
c_floatfloatfloat
c_doubledoublefloat
c_longdoublelong doublefloat
c_char_pchar * (NUL terminated)字節(jié)串對(duì)象或 None
c_wchar_pwchar_t * (NUL terminated)字符串或 None
c_void_pvoid *int 或 None

2 加載共享庫(kù)

import ctypes  
# 加載本地的共享庫(kù),路徑根據(jù)實(shí)際情況調(diào)整  
lib = ctypes.CDLL('./libexample.so')  # Linux/macOS平臺(tái)  
# lib = ctypes.WinDLL('example.dll')  # Windows平臺(tái)

3 調(diào)用函數(shù)

# 設(shè)置參數(shù)類型  
lib.add.argtypes = [ctypes.c_int, ctypes.c_int]  
# 設(shè)置返回類型  
lib.add.restype = ctypes.c_int  
# 調(diào)用C函數(shù)  
result = lib.add(2, 3)  
print(result)  # 輸出:5

4 操作指針

在ctypes中,你可以使用pointer和byref來(lái)操作指針。

# 創(chuàng)建一個(gè)整數(shù)數(shù)組  
arr = (ctypes.c_int * 5)(1, 2, 3, 4, 5)  
# 獲取指向數(shù)組首元素的指針  
ptr = ctypes.pointer(arr)  
# 通過(guò)指針訪問(wèn)數(shù)組元素  
print(ptr[0])  # 輸出:1  
# 使用byref創(chuàng)建指向變量的指針  
x = ctypes.c_int(10)  
px = ctypes.byref(x)  
# 通過(guò)指針修改變量的值  
lib.increment(px)  # 假設(shè)有一個(gè)increment函數(shù)用于增加整數(shù)的值  
print(x.value)  # 輸出:11
# 調(diào)用系統(tǒng)的庫(kù)函數(shù)測(cè)試
from ctypes import c_int, c_float, create_string_buffer, CDLL, byref
c_lib = CDLL('/lib/x86_64-linux-gnu/libc.so.6')
i = c_int()
f = c_float()
s = create_string_buffer(b"\000" * 32)
print(i.value, f.value, repr(s.value))
# brref 傳入數(shù)據(jù)類型返回指針的地址, create_string_buffer返回的是指針
c_lib.sscanf(b"1 3.14 Hello", b"%d %f %s", byref(i), byref(f), s)
print(i.value, f.value, repr(s.value))
from ctypes import c_int, POINTER, cdll
i = c_int(42)   # 創(chuàng)建一個(gè)int類型變量
pi = POINTER(c_int)(i)  # 定義一個(gè)指向int類型的指針
print(pi.contents)   # 打印指針指向的內(nèi)存地址
print(pi.contents.value)   # 打印指針指向的值
# 定義C庫(kù)中的函數(shù)原型
sum_func = cdll.LoadLibrary('./test1.so').sums
sum_func.argtypes = [POINTER(c_int), POINTER(c_int)]    # 定義函數(shù)參數(shù)類型為指針類型
sum_func.restype = POINTER(c_int)   # 定義函數(shù)返回值類型為指針類型
pointer = POINTER(c_int)
# 調(diào)用sum()函數(shù)
a = c_int(1)
b = c_int(2)
c = sum_func(pointer(a), pointer(b))   # 將a、b的地址傳遞給sum()函數(shù)
print(c.contents.value)   # 打印返回值

5 操作結(jié)構(gòu)體和聯(lián)合體

你需要定義結(jié)構(gòu)體或聯(lián)合體的類型,然后可以創(chuàng)建實(shí)例、訪問(wèn)其成員等。

# 定義C語(yǔ)言的結(jié)構(gòu)體類型  
class Point(ctypes.Structure):  
    _fields_ = [("x", ctypes.c_int), ("y", ctypes.c_int)]  
# 創(chuàng)建結(jié)構(gòu)體的實(shí)例  
p = Point()  
p.x = 10  
p.y = 20  
# 將結(jié)構(gòu)體的實(shí)例傳遞給C函數(shù)  
lib.print_point(p)  # 假設(shè)有一個(gè)print_point函數(shù)用于打印點(diǎn)的坐標(biāo)

6 處理字符串

字符串通常以字符數(shù)組或字符指針的形式存在。在ctypes中,你可以使用create_string_buffer來(lái)創(chuàng)建C風(fēng)格的字符串,或者使用c_char_p來(lái)操作字符串指針。

# 創(chuàng)建C風(fēng)格的字符串  
c_str = ctypes.create_string_buffer(b"Hello, World!")  
# 將字符串傳遞給C函數(shù)  
lib.print_string(c_str)  # 假設(shè)有一個(gè)print_string函數(shù)用于打印字符串  
# 處理C語(yǔ)言中的字符串指針  
c_char_p_type = ctypes.POINTER(ctypes.c_char)  
c_char_p = c_char_p_type.from_buffer(c_str)  
lib.print_string_ptr(c_char_p)  # 假設(shè)有一個(gè)print_string_ptr函數(shù)接收字符串指針

7 回調(diào)函數(shù)

c 中很多實(shí)現(xiàn)異步的方式通過(guò)回調(diào)函數(shù)事件觸發(fā)的方式,ctype中也能將ctype定義的函數(shù)傳入c中執(zhí)行

# include "stdio.h"
typedef int (*CallbackFunc)(int, int);
int c_sub(int x, int y){
    printf("c callback func\n");
    return x - y;
}
void call_callback(CallbackFunc callback){
    int result = callback(3, 4);
    printf("result from c: %d\n", result);
}
import ctypes
my_lib = ctypes.CDLL("./test2.so")
callback_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int)
def python_sub(a, b):
    print("python callback func")
    return b - a
# 1.調(diào)用c中的函數(shù),回調(diào)函數(shù)從python中傳入
python_callback = callback_type(python_sub)
my_lib.call_callback(python_callback)
# 2.調(diào)用c中的函數(shù),回調(diào)從c中傳入
c_callback = callback_type(my_lib.c_sub)
my_lib.call_callback(c_callback)

到此這篇關(guān)于python中ctypes使用的文章就介紹到這了,更多相關(guān)python ctypes使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論