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]