基本介紹
- 中文名:C類型
- 外文名:ctypes
載入動態程式庫
>>> from ctypes import *
>>> print(windll.kernel32)
<WinDLL 'kernel32', handle ... at ...>
>>> print(cdll.msvcrt)
<CDLL 'msvcrt', handle ... at ...>
>>> libc = cdll.msvcrt
>>> cdll.LoadLibrary("libc.so.6")
<CDLL 'libc.so.6', handle ... at ...>
>>> libc = CDLL("libc.so.6")
>>> libc
<CDLL 'libc.so.6', handle ... at ...>
函式
>>> from ctypes import *
>>> libc = cdll.msvcrt # Windows
>>> libc = CDLL("libc.so.6") # Linux
>>> libc.printf<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ctypes.py", line 239, in __getattr__
func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")
<_FuncPtr object at 0x...>
>>>
>>> cdll.kernel32[1]
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ctypes.py", line 310, in __getitem__
func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
調用函式
>>> print(libc.time(None))
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))
0x1d000000
>>> cdll.kernel32.GetModuleHandleA(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>> windll.msvcrt.printf(b"spam")
Traceback (most recent call last):
... ...
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
數據類型
ctypes 類型 | C 類型 | Python 類型 |
---|---|---|
c_bool | _Bool | bool (1) |
c_char | char | 單字元位元組對象 |
c_wchar | wchar_t | 單字元字元串 |
c_byte | char | 整型 |
c_ubyte | unsignedchar | 整型 |
c_short | short | 整型 |
c_ushort | unsignedshort | 整型 |
c_int | int | 整型 |
c_uint | unsignedint | 整型 |
c_long | long | 整型 |
c_ulong | unsignedlong | 整型 |
c_longlong | __int64或longlong | 整型 |
c_ulonglong | unsigned__int64或unsignedlonglong | 整型 |
c_size_t | size_t | 整型 |
c_ssize_t | ssize_t或Py_ssize_t | 整型 |
c_float | float | 浮點數 |
c_double | double | 浮點數 |
c_longdouble | longdouble | 浮點數 |
c_char_p | char*(NUL terminated) | 位元組串對象或None |
c_wchar_p | wchar_t*(NUL terminated) | 字元串或None |
c_void_p | void* | int 或None |
- 構造函式接受任何具有真值的對象。
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>> i = c_int(42)
>>> print(i)
c_long(42)
>>> print(i.value)
42
>>> i.value = -99
>>> print(i.value)
-99
>>> s = "Hello, World"
>>> c_s = c_wchar_p(s)
>>> print(c_s)
c_wchar_p(139966785747344)
>>> print(c_s.value)
Hello World
>>> c_s.value = "Hi, there"
>>> print(c_s) # the memory location has changed
c_wchar_p(139966783348904)
>>> print(c_s.value)
Hi, there
>>> print(s) # first object is unchanged
Hello, World
>>> from ctypes import *
>>> p = create_string_buffer(3) # create a 3 byte buffer, initialized to NUL bytes
>>> print(sizeof(p), repr(p.raw))
3 b'\x00\x00\x00'
>>> p = create_string_buffer(b"Hello") # create a buffer containing a NUL terminated string
>>> print(sizeof(p), repr(p.raw))
6 b'Hello\x00'
>>> print(repr(p.value))
b'Hello'
>>> p = create_string_buffer(b"Hello", 10) # create a 10 byte buffer
>>> print(sizeof(p), repr(p.raw))10 b'Hello\x00\x00\x00\x00\x00'
>>> p.value = b"Hi"
>>> print(sizeof(p), repr(p.raw))
10 b'Hi\x00lo\x00\x00\x00\x00\x00'