ctypes type | C type | Python type |
---|---|---|
c_bool | _Bool | bool (1) |
c_char | char | 1-character bytes object |
c_wchar | wchar_t | 1-character string |
c_byte | char | int |
c_ubyte | unsigned char | int |
c_short | short | int |
c_ushort | unsigned short | int |
c_int | int | int |
c_uint | unsigned int | int |
c_long | long | int |
c_ulong | unsigned long | int |
c_longlong | __int64 or long long | int |
c_ulonglong | unsigned __int64 or unsigned long long | int |
c_size_t | size_t | int |
c_ssize_t | ssize_t or Py_ssize_t | int |
c_float | float | float |
c_double | double | float |
c_longdouble | long double | float |
c_char_p | char * (NUL terminated) | bytes object or None |
c_wchar_p | wchar_t * (NUL terminated) | string or None |
c_void_p | void * | int or None |
因爲
python
是無類型的,建立了一個對象並賦值了以後,對這個對象再次賦值將會變成新的類型。 因此一般以類的方式或者容器的方式進行賦值。即經過添加屬性的方式賦值。python
from ctypes import * a = c_int(2) print(type(a)) a = 2 print(type(a)) a = c_int(2) a.value = 3 print(a) """ <class 'ctypes.c_long'> <class 'int'> c_long(3) [Finished in 0.1s] """
若是是非對應類型賦值,會先進行轉換成爲對應類型,類比
C
.數組
ctypes.create_string_buffer(init_or_size, size=None)
建立一個字符串緩衝區,能夠進行修改,類比char buffer[size]={init_or_size}
init_or_size必須是int
類型的(表示建立大小),或者是bytes
類型的用於初始化。ui
from ctypes import * buf = create_string_buffer(10) print(sizeof(buf),buf,type(buf),buf.raw,buf.value) buf = create_string_buffer(b"test",10) print(sizeof(buf),buf,type(buf),buf.raw,buf.value) buf = create_string_buffer(b"test") print(sizeof(buf),buf,type(buf),buf.raw,buf.value) """ 10 <ctypes.c_char_Array_10 object at 0x037A9460> <class 'ctypes.c_char_Array_10'> b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'' 10 <ctypes.c_char_Array_10 object at 0x03835100> <class 'ctypes.c_char_Array_10'> b'test\x00\x00\x00\x00\x00\x00' b'test' 5 <ctypes.c_char_Array_5 object at 0x037A9460> <class 'ctypes.c_char_Array_5'> b'test\x00' b'test' [Finished in 0.1s] """
ctypes.create_unicode_buffer(init_or_size, size=None)
建立一個Unicode類型的字符數組緩衝區,對應Python類型c_wchar "ass"
普通類型字符串,對應C
類型wchar
.編碼
from ctypes import * buf = create_unicode_buffer(10) print(sizeof(buf),buf,type(buf),buf.value) buf = create_unicode_buffer("test",10) print(sizeof(buf),buf,type(buf),buf.value) buf = create_unicode_buffer("test") print(sizeof(buf),buf,type(buf),buf.value) """ 20 <ctypes.c_wchar_Array_10 object at 0x00AB94F0> <class 'ctypes.c_wchar_Array_10'> 20 <ctypes.c_wchar_Array_10 object at 0x02B15100> <class 'ctypes.c_wchar_Array_10'> test 10 <ctypes.c_wchar_Array_5 object at 0x00AB94F0> <class 'ctypes.c_wchar_Array_5'> test [Finished in 0.1s] """
能夠通用的類型。code
有幾種類型是能夠直接像C類型同樣使用的,如
int
,string
,bytes
,這幾種能夠和上面幾種進行類型對照。能匹配的均可以進行直接使用。對象
自定義類型做爲參數。unicode
添加屬性
_as_parameter_
,同時能夠經過property
進行構造。字符串
from ctypes import * class Test: _as_parameter_ = 12 cdll.msvcrt.printf(b"%d\n",Test()) """ 12 [Finished in 0.1s] """
只能是int bytes string
也能夠是經過property
進行修改。get
from ctypes import * class Test: def _getx(self): return 12 _as_parameter_ = property(_getx,None,None,"ok") cdll.msvcrt.printf(b"%d\n",Test()) """ 12 [Finished in 0.1s]