Hướng dẫn python ctypes uint - python ctypes uint


>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 là một thư viện chức năng nước ngoài cho Python. Nó cung cấp các loại dữ liệu tương thích C và cho phép gọi các hàm trong DLL hoặc thư viện được chia sẻ. Nó có thể được sử dụng để bọc các thư viện này trong Python thuần túy.

Show

Hướng dẫn Ctypes

Lưu ý: Các mẫu mã trong hướng dẫn này sử dụng

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
9 để đảm bảo rằng chúng thực sự hoạt động. Vì một số mẫu mã hoạt động khác nhau trong Linux, Windows hoặc MacOS, chúng có chứa các chỉ thị tài liệu trong các bình luận.

Lưu ý: Một số mẫu mã tham chiếu loại CTYPE

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0. Trên các nền tảng trong đó
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
1 là bí danh của
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
2. Vì vậy, bạn không nên bị nhầm lẫn nếu
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
2 được in nếu bạn mong đợi
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0 - ​​chúng thực sự là cùng một loại.

Tải thư viện liên kết động lor

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 Xuất CDLL và trên các đối tượng Windows Windll và OLEDLL, để tải các thư viện liên kết động.

Bạn tải thư viện bằng cách truy cập chúng dưới dạng thuộc tính của các đối tượng này. CDLL tải các thư viện xuất các chức năng sử dụng quy ước gọi

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
6 tiêu chuẩn, trong khi các thư viện Windll gọi các chức năng bằng cách sử dụng quy ước gọi
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7. OLEDLL cũng sử dụng quy ước gọi
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7 và giả sử các chức năng trả về mã lỗi Windows
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
9. Mã lỗi được sử dụng để tự động tăng ngoại lệ
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
0 khi cuộc gọi hàm không thành công.

Đã thay đổi trong phiên bản 3.3: Các lỗi Windows được sử dụng để tăng

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
1, hiện là bí danh của
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
0.Windows errors used to raise
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
1, which is now an alias of
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
0.

Dưới đây là một số ví dụ cho Windows. Lưu ý rằng

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
3 là thư viện C tiêu chuẩn M chứa hầu hết các chức năng C tiêu chuẩn và sử dụng quy ước gọi CDECL:

>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>

Windows tự động nối thêm phần hậu tố

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
4 thông thường.

Ghi chú

Truy cập thư viện C tiêu chuẩn thông qua

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
5 sẽ sử dụng phiên bản lỗi thời của thư viện có thể không tương thích với phiên bản được Python sử dụng. Nếu có thể, hãy sử dụng chức năng Python gốc, hoặc khác nhập và sử dụng mô -đun
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
3.

Trên Linux, cần phải chỉ định tên tệp bao gồm cả phần mở rộng để tải thư viện, do đó, quyền truy cập thuộc tính không thể được sử dụng để tải thư viện. Nên sử dụng phương thức

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
7 của trình tải DLL hoặc bạn nên tải thư viện bằng cách tạo một phiên bản CDLL bằng cách gọi hàm tạo:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>

Truy cập các chức năng từ DLLS được tải

Các chức năng được truy cập dưới dạng thuộc tính của các đối tượng DLL:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>

Lưu ý rằng các DLL của hệ thống Win32 như

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
8 và
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
9 thường xuất ANSI cũng như các phiên bản Unicode của một hàm. Phiên bản Unicode được xuất với
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
00 được gắn vào tên, trong khi phiên bản ANSI được xuất với
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
01 được gắn vào tên. Hàm Win32
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
02, trả về một tay cầm mô -đun cho một tên mô -đun nhất định, có nguyên mẫu C sau và một macro được sử dụng để lộ một trong số chúng là
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
02 tùy thuộc vào việc unicode có được xác định hay không:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);

Windll không cố gắng chọn một trong số chúng bằng phép thuật, bạn phải truy cập phiên bản bạn cần bằng cách chỉ định rõ ràng

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
04 hoặc
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
05, sau đó gọi nó với các byte hoặc các đối tượng chuỗi tương ứng.

Đôi khi, các chức năng xuất DLLS với các tên có số nhận dạng Python hợp lệ, như

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
06. Trong trường hợp này, bạn phải sử dụng
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
07 để truy xuất chức năng:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>

Trên Windows, một số chức năng xuất DLL không phải theo tên mà theo thứ tự. Các chức năng này có thể được truy cập bằng cách lập chỉ mục đối tượng DLL với số thứ tự:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>

Gọi các chức năng

Bạn có thể gọi các chức năng này như bất kỳ Python nào khác có thể gọi được. Ví dụ này sử dụng hàm

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
08, trả về thời gian hệ thống tính bằng giây kể từ Epoch Unix và hàm
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
09, trả về một tay cầm mô -đun Win32.

Ví dụ này gọi cả hai chức năng với con trỏ

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10 (
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11 nên được sử dụng làm con trỏ
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10):

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
13 được nâng lên khi bạn gọi chức năng
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7 với quy ước gọi
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
6 hoặc ngược lại:

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>

Để tìm ra quy ước gọi chính xác, bạn phải xem xét tệp tiêu đề C hoặc tài liệu cho chức năng bạn muốn gọi.

Trên Windows,

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 sử dụng xử lý ngoại lệ có cấu trúc Win32 để ngăn chặn các sự cố khỏi các lỗi bảo vệ chung khi các chức năng được gọi với các giá trị đối số không hợp lệ:

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>

Tuy nhiên, có đủ cách để đánh sập Python với

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8, vì vậy dù sao bạn cũng nên cẩn thận. Mô -đun
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
18 có thể hữu ích trong các sự cố gỡ lỗi (ví dụ: từ các lỗi phân đoạn được tạo bởi các cuộc gọi thư viện C sai).

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11, số nguyên, các đối tượng byte và chuỗi (unicode) là các đối tượng python gốc duy nhất có thể được sử dụng trực tiếp làm tham số trong các lệnh gọi hàm này.
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11 được truyền dưới dạng con trỏ C
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10, các đối tượng và chuỗi byte được truyền dưới dạng con trỏ đến khối bộ nhớ chứa dữ liệu của chúng (char* hoặc wchar_t*). Các số nguyên python được truyền dưới dạng loại mặc định C int, giá trị của chúng được che dấu để phù hợp với loại C.char* or wchar_t*). Python integers are passed as the platforms default C int type, their value is masked to fit into the C type.

Trước khi chúng tôi chuyển sang gọi các chức năng với các loại tham số khác, chúng tôi phải tìm hiểu thêm về các loại dữ liệu

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8.

Các loại dữ liệu cơ bản

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 Xác định một số loại dữ liệu tương thích C nguyên thủy:

Loại ctypes

Loại c

Loại Python

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
24

_Bool

Bool (1)

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
25

char

Đối tượng byte 1 ký tự

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
26

wchar_t

Chuỗi 1 ký tự

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
27

char

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
28

không dấuchar

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
29

short

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
30

Undignedshortshort

int

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0

int

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
32

không dấuint

int

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
2

long

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
34

UnsignedLonglong

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
35

__int64 hoặc dài or longlong

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
36

Undign__int64 hoặc UndignedLongLong__int64 or unsignedlonglong

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
37

size_t

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
38

ssize_t hoặc py_ssize_t or Py_ssize_t

int

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
39

float

float

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
40

double

float

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
41

Longdoubledouble

float

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
42

char* (nul chấm dứt)* (NUL terminated)

Đối tượng byte hoặc

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
44

wchar_t* (nul chấm dứt)* (NUL terminated)

chuỗi hoặc

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
46

void**

int hoặc

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11

  1. Hàm tạo chấp nhận bất kỳ đối tượng nào có giá trị sự thật.

Tất cả các loại này có thể được tạo bằng cách gọi chúng bằng trình khởi tạo tùy chọn của loại và giá trị chính xác:

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>

Vì các loại này có thể thay đổi, giá trị của chúng cũng có thể được thay đổi sau đó:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
0

Việc gán một giá trị mới cho các trường hợp của các loại con trỏ

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
42,
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
44 và
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
46 Thay đổi vị trí bộ nhớ mà chúng chỉ ra, không phải là nội dung của khối bộ nhớ (tất nhiên là không, bởi vì các đối tượng Byte Python là bất biến):

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
1

Tuy nhiên, bạn nên cẩn thận để không chuyển chúng cho các chức năng mong đợi con trỏ đến bộ nhớ có thể thay đổi. Nếu bạn cần các khối bộ nhớ có thể thay đổi, CTYPE có chức năng

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
51 tạo ra chúng theo nhiều cách khác nhau. Nội dung khối bộ nhớ hiện tại có thể được truy cập (hoặc thay đổi) với thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
52; Nếu bạn muốn truy cập nó dưới dạng chuỗi NUL chấm dứt, hãy sử dụng thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
53:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
2

Hàm

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
51 thay thế hàm
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
55 cũ (vẫn có sẵn dưới dạng bí danh). Để tạo một khối bộ nhớ có thể thay đổi chứa các ký tự unicode của c wchar_t, hãy sử dụng hàm
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
56.wchar_t, use the
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
56 function.

Gọi chức năng, tiếp tục

Lưu ý rằng in in vào kênh đầu ra tiêu chuẩn thực, không phải

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
57, vì vậy các ví dụ này sẽ chỉ hoạt động tại dấu nhắc điều khiển, không phải từ bên trong Idle hoặc Pythonwin:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
3

Như đã được đề cập trước đây, tất cả các loại Python ngoại trừ các số nguyên, chuỗi và các đối tượng byte phải được bọc trong loại

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 tương ứng của chúng, để chúng có thể được chuyển đổi thành loại dữ liệu C yêu cầu:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
4

Gọi chức năng với các loại dữ liệu tùy chỉnh của riêng bạn

Bạn cũng có thể tùy chỉnh chuyển đổi đối số

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 để cho phép các phiên bản của các lớp của riêng bạn được sử dụng làm đối số chức năng.
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 tìm kiếm một thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
61 và sử dụng điều này làm đối số chức năng. Tất nhiên, nó phải là một trong số các số nguyên, chuỗi hoặc byte:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
5

Nếu bạn không muốn lưu trữ dữ liệu của phiên bản trong biến thể hiện

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
61, bạn có thể xác định
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
63 cung cấp thuộc tính có sẵn theo yêu cầu.

Chỉ định các loại đối số bắt buộc (nguyên mẫu chức năng) ¶

Có thể chỉ định các loại đối số yêu cầu của các hàm được xuất từ ​​DLL bằng cách đặt thuộc tính

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64.

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64 phải là một chuỗi các loại dữ liệu C (hàm
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
66 có lẽ không phải là một ví dụ tốt ở đây, bởi vì nó có một số thay đổi và các loại tham số khác nhau tùy thuộc vào chuỗi định dạng, mặt khác, điều này khá tiện dụng để thử nghiệm điều này tính năng):

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
6

Chỉ định một định dạng bảo vệ chống lại các loại đối số không tương thích (chỉ là nguyên mẫu cho hàm C) và cố gắng chuyển đổi các đối số thành các loại hợp lệ:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
7

Nếu bạn đã xác định các lớp của riêng bạn mà bạn chuyển đến các cuộc gọi hoạt động, bạn phải thực hiện phương thức lớp

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
67 để chúng có thể sử dụng chúng trong chuỗi
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64. Phương thức lớp
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
67 nhận đối tượng Python được chuyển đến lệnh gọi hàm, nó nên thực hiện một typecheck hoặc bất cứ điều gì cần thiết để đảm bảo đối tượng này được chấp nhận, sau đó trả về chính đối tượng, thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
61 của nó hoặc bất cứ điều gì bạn muốn C đối số chức năng trong trường hợp này. Một lần nữa, kết quả phải là một số nguyên, chuỗi, byte, một thể hiện
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 hoặc một đối tượng có thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
61.

Các loại trở lại

Theo các chức năng mặc định được giả định để trả về loại C int. Các loại trả về khác có thể được chỉ định bằng cách đặt thuộc tính

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 của đối tượng hàm.int type. Other return types can be specified by setting the
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 attribute of the function object.

Dưới đây là một ví dụ nâng cao hơn, nó sử dụng hàm

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
74, mong đợi một con trỏ chuỗi và char, và trả về một con trỏ cho một chuỗi:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
8

Nếu bạn muốn tránh các cuộc gọi

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
75 ở trên, bạn có thể đặt thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64 và đối số thứ hai sẽ được chuyển đổi từ một đối tượng byte python ký tự duy nhất thành c char c char:

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
9

Bạn cũng có thể sử dụng đối tượng Python có thể gọi được (ví dụ như một hàm hoặc lớp) làm thuộc tính

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73, nếu hàm nước ngoài trả về một số nguyên. Có thể gọi sẽ được gọi với số nguyên, hàm C trả về và kết quả của cuộc gọi này sẽ được sử dụng làm kết quả của cuộc gọi chức năng của bạn. Điều này rất hữu ích để kiểm tra các giá trị trả về lỗi và tự động tăng một ngoại lệ:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
0

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
78 là một hàm sẽ gọi API Windows ____179 để lấy biểu diễn chuỗi của mã lỗi và trả về một ngoại lệ.
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
78 có tham số mã lỗi tùy chọn, nếu không có ai được sử dụng, nó gọi
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
81 để lấy nó.

Xin lưu ý rằng cơ chế kiểm tra lỗi mạnh hơn nhiều có sẵn thông qua thuộc tính

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
82; Xem hướng dẫn tham khảo để biết chi tiết.

Vượt qua con trỏ (hoặc: vượt qua tham số bằng tham chiếu) ¶

Đôi khi một hàm A API mong đợi một con trỏ đến một loại dữ liệu dưới dạng tham số, có thể sẽ ghi vào vị trí tương ứng hoặc nếu dữ liệu quá lớn để được truyền bởi giá trị. Điều này còn được gọi là các tham số truyền bằng cách tham chiếu.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 Xuất hàm
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
84 được sử dụng để truyền tham số bằng tham chiếu. Hiệu ứng tương tự có thể đạt được với chức năng
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
85, mặc dù
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
85 thực hiện nhiều công việc hơn vì nó xây dựng một đối tượng con trỏ thực, do đó, sử dụng
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
84 nhanh hơn nếu bạn không cần đối tượng con trỏ trong chính Python:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
1

Cấu trúc và công đoàn

Các cấu trúc và công đoàn phải xuất phát từ các lớp cơ sở

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
88 và
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
89 được xác định trong mô -đun
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8. Mỗi lớp con phải xác định thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91.
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 phải là danh sách các bộ 2, chứa tên trường và loại trường.

Loại trường phải là loại

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 như
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0 hoặc bất kỳ loại
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 nào khác: cấu trúc, liên kết, mảng, con trỏ.

Dưới đây là một ví dụ đơn giản về cấu trúc điểm, chứa hai số nguyên có tên X và Y, và cũng chỉ ra cách khởi tạo cấu trúc trong hàm tạo:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
2

Tuy nhiên, bạn có thể xây dựng các cấu trúc phức tạp hơn nhiều. Một cấu trúc có thể chứa các cấu trúc khác bằng cách sử dụng một cấu trúc làm loại trường.

Dưới đây là cấu trúc trực tràng chứa hai điểm có tên Upperleft và LowerRight:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
3

Các cấu trúc lồng nhau cũng có thể được khởi tạo trong hàm tạo theo nhiều cách:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
4

Các mô tả trường có thể được lấy từ lớp, chúng rất hữu ích cho việc gỡ lỗi vì chúng có thể cung cấp thông tin hữu ích:descriptors can be retrieved from the class, they are useful for debugging because they can provide useful information:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
5

Cảnh báo

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 không hỗ trợ truyền các công đoàn hoặc cấu trúc với các trường bit cho các chức năng theo giá trị. Mặc dù điều này có thể hoạt động trên 32 bit x86, nhưng nó không được thư viện đảm bảo làm việc trong trường hợp chung. Các công đoàn và cấu trúc với các trường bit phải luôn được truyền đến các chức năng theo con trỏ.

Cấu trúc/Liên minh liên kết và đơn đặt hàng byte

Theo mặc định, các trường cấu trúc và liên minh được căn chỉnh theo cùng một cách trình biên dịch C thực hiện nó. Có thể ghi đè hành vi này bằng cách chỉ định thuộc tính lớp

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
97 trong định nghĩa lớp con. Điều này phải được đặt thành một số nguyên dương và chỉ định sự liên kết tối đa cho các trường. Đây là những gì
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
98 cũng làm trong MSVC.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 sử dụng thứ tự byte gốc cho các cấu trúc và công đoàn. Để xây dựng các cấu trúc với thứ tự byte không bản địa, bạn có thể sử dụng một trong các lớp cơ sở
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
00,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
01,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
02 và
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
03. Các lớp này không thể chứa các trường con trỏ.

Các trường bit trong các cấu trúc và công đoàn

Có thể tạo các cấu trúc và công đoàn chứa các trường bit. Các trường bit chỉ có thể đối với các trường số nguyên, độ rộng bit được chỉ định là mục thứ ba trong các bộ

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
6

Phần

Các mảng là các chuỗi, chứa một số trường hợp cố định cùng loại.

Cách được đề xuất để tạo các loại mảng là nhân loại dữ liệu với số nguyên dương:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
7

Dưới đây là một ví dụ về loại dữ liệu có phần nhân tạo, một cấu trúc chứa 4 điểm trong số các thứ khác:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
8

Các thể hiện được tạo theo cách thông thường, bằng cách gọi lớp:

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
9

Mã trên in một loạt các dòng

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
05, bởi vì các nội dung mảng được khởi tạo thành các số không.

Khởi tạo loại chính xác cũng có thể được chỉ định:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
0

Con trỏ

Các phiên bản con trỏ được tạo bằng cách gọi hàm

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
85 theo loại
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
1

Các phiên bản con trỏ có thuộc tính

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
08 trả về đối tượng mà các điểm con trỏ, đối tượng
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
09 ở trên:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
2

Lưu ý rằng

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 không có oor (trả về đối tượng gốc), nó xây dựng một đối tượng mới, tương đương mỗi khi bạn truy xuất một thuộc tính:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
3

Việc gán một thể hiện

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0 khác cho thuộc tính Nội dung con trỏ của con trỏ sẽ khiến con trỏ trỏ đến vị trí bộ nhớ nơi điều này được lưu trữ:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
4

Các phiên bản con trỏ cũng có thể được lập chỉ mục với số nguyên:

Việc gán cho một chỉ mục số nguyên thay đổi giá trị được trỏ:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
5

Cũng có thể sử dụng các chỉ mục khác với 0, nhưng bạn phải biết bạn đang làm gì, giống như trong C: bạn có thể truy cập hoặc thay đổi vị trí bộ nhớ tùy ý. Nói chung, bạn chỉ sử dụng tính năng này nếu bạn nhận được một con trỏ từ hàm C và bạn biết rằng con trỏ thực sự chỉ vào một mảng thay vì một mục.

Đằng sau hậu trường, hàm

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
85 không chỉ đơn giản là tạo các phiên bản con trỏ, nó phải tạo các loại con trỏ trước. Điều này được thực hiện với chức năng
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
13, chấp nhận bất kỳ loại
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 nào và trả về một loại mới:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
6

Gọi loại con trỏ mà không có đối số tạo ra một con trỏ

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10.
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10 Con trỏ có giá trị boolean
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
17:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
7

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 Kiểm tra
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10 Khi các con trỏ phân hủy (nhưng không hợp lệ không hợp lệ -________ 110 Con trỏ sẽ làm hỏng Python):

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
8

Nhập Chuyển đổi Jo

Thông thường, CTYPE không kiểm tra loại nghiêm ngặt. Điều này có nghĩa là, nếu bạn có

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
21 trong danh sách
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64 của một hàm hoặc là loại trường thành viên trong định nghĩa cấu trúc, chỉ có các trường hợp chính xác cùng loại được chấp nhận. Có một số trường hợp ngoại lệ cho quy tắc này, trong đó CTYPE chấp nhận các đối tượng khác. Ví dụ: bạn có thể vượt qua các phiên bản mảng tương thích thay vì các loại con trỏ. Vì vậy, đối với
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
21, CTYPES chấp nhận một mảng C_INT:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
9

Ngoài ra, nếu một đối số hàm được tuyên bố rõ ràng là loại con trỏ (chẳng hạn như

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
21) trong
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64, một đối tượng của loại nhọn (
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0 trong trường hợp này) có thể được truyền đến hàm. CTYPES sẽ tự động áp dụng chuyển đổi
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
84 trong trường hợp này.

Để đặt trường Loại con trỏ thành

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10, bạn có thể gán
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
0

Đôi khi bạn có các trường hợp không tương thích. Trong C, bạn có thể đúc một loại vào một loại khác.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 cung cấp chức năng
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
31 có thể được sử dụng theo cùng một cách. Cấu trúc
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
32 được xác định ở trên chấp nhận các con trỏ
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
21 hoặc mảng
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0 cho trường
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
35 của nó, nhưng không phải là trường hợp của các loại khác:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
1

Đối với những trường hợp này, hàm

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
31 rất tiện dụng.

Hàm

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
31 có thể được sử dụng để chuyển thể hiện CTYPES vào một con trỏ đến loại dữ liệu CTYPES khác.
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
31 có hai tham số, một đối tượng CTYPES hoặc có thể được chuyển đổi thành một con trỏ nào đó và loại con trỏ CTYPES. Nó trả về một thể hiện của đối số thứ hai, trong đó tham chiếu cùng một khối bộ nhớ với đối số đầu tiên:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
2

Vì vậy,

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
31 có thể được sử dụng để gán cho trường
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
35 của
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
32 Cấu trúc:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
3

Các loại không đầy đủ

Các loại không đầy đủ là các cấu trúc, công đoàn hoặc mảng mà các thành viên chưa được chỉ định. Trong C, chúng được chỉ định bởi các tuyên bố chuyển tiếp, được xác định sau:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
4

Bản dịch đơn giản sang mã CTYPES sẽ là thế này, nhưng nó không hoạt động:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
5

bởi vì

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
42 mới không có sẵn trong bản sao câu lệnh của lớp. Trong
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8, chúng ta có thể xác định lớp
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
44 và đặt thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 sau, sau tuyên bố lớp:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
6

Hãy thử nó. Chúng tôi tạo hai trường hợp

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
44 và để chúng chỉ cho nhau và cuối cùng theo chuỗi con trỏ một vài lần:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
7

Chức năng gọi lại

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 cho phép tạo các con trỏ chức năng có thể gọi C từ các thiết bị gọi Python. Chúng đôi khi được gọi là chức năng gọi lại.

Đầu tiên, bạn phải tạo một lớp cho chức năng gọi lại. Lớp biết quy ước gọi, loại trả về, số và loại đối số mà chức năng này sẽ nhận được.

Hàm nhà máy

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
48 tạo các loại cho các chức năng gọi lại bằng cách sử dụng quy ước gọi
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
6. Trên Windows, chức năng nhà máy
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
50 tạo các loại cho các chức năng gọi lại bằng cách sử dụng quy ước gọi
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7.

Cả hai chức năng của nhà máy này đều được gọi với loại kết quả là đối số đầu tiên và các hàm gọi lại các loại đối số dự kiến ​​là các đối số còn lại.

Tôi sẽ trình bày một ví dụ ở đây sử dụng chức năng thư viện C tiêu chuẩn ____ ____252, được sử dụng để sắp xếp các mục với sự trợ giúp của chức năng gọi lại.

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
52 sẽ được sử dụng để sắp xếp một loạt các số nguyên:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
8

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
52 phải được gọi bằng một con trỏ tới dữ liệu để sắp xếp, số lượng mục trong mảng dữ liệu, kích thước của một mục và con trỏ đến hàm so sánh, gọi lại. Cuộc gọi lại sau đó sẽ được gọi với hai con trỏ cho các mục và nó phải trả về một số nguyên âm nếu mục thứ nhất nhỏ hơn thứ hai, số 0 nếu chúng bằng nhau và số nguyên dương khác.

Vì vậy, chức năng gọi lại của chúng tôi nhận được con trỏ cho các số nguyên và phải trả về một số nguyên. Trước tiên, chúng tôi tạo

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
55 cho chức năng gọi lại:

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
9

Để bắt đầu, đây là một cuộc gọi lại đơn giản cho thấy các giá trị mà nó được thông qua:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
0

Kết quả:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
1

Bây giờ chúng ta thực sự có thể so sánh hai mục và trả về một kết quả hữu ích:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
2

Vì chúng tôi có thể dễ dàng kiểm tra, mảng của chúng tôi đã được sắp xếp ngay bây giờ:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
3

Các nhà máy chức năng có thể được sử dụng làm nhà máy trang trí, vì vậy chúng tôi cũng có thể viết:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
4

Ghi chú

Hãy chắc chắn rằng bạn tiếp tục tham chiếu đến các đối tượng

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
48 miễn là chúng được sử dụng từ mã C.
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 không có, và nếu bạn don, chúng có thể được thu thập rác, làm hỏng chương trình của bạn khi một cuộc gọi lại được thực hiện.

Ngoài ra, lưu ý rằng nếu hàm gọi lại được gọi trong một luồng được tạo bên ngoài điều khiển Python, (ví dụ: bằng mã nước ngoài gọi gọi lại), CTYPES sẽ tạo ra một luồng Python giả mới trên mỗi lần gọi. Hành vi này là chính xác cho hầu hết các mục đích, nhưng điều đó có nghĩa là các giá trị được lưu trữ bằng

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
58 sẽ không tồn tại trên các cuộc gọi lại khác nhau, ngay cả khi các cuộc gọi đó được thực hiện từ cùng một luồng C.

Truy cập các giá trị được xuất từ ​​DLLS¶

Một số thư viện được chia sẻ không chỉ các chức năng xuất, chúng còn xuất các biến. Một ví dụ trong chính thư viện Python là

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
59, số nguyên được đặt thành 0, 1 hoặc 2, tùy thuộc vào cờ
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
60 hoặc
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
61 được đưa ra khi khởi động.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 có thể truy cập các giá trị như thế này với các phương thức lớp
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
63 của loại. Pythonapi là một biểu tượng được xác định trước cho phép truy cập vào API Python C:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
5

Nếu thông dịch viên đã được bắt đầu với

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
60, mẫu sẽ được in
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
65 hoặc
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
66 nếu
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
61 được chỉ định.

Một ví dụ mở rộng cũng cho thấy việc sử dụng các con trỏ truy cập vào con trỏ

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
68 được xuất khẩu bởi Python.

Trích dẫn các tài liệu cho giá trị đó:

Con trỏ này được khởi tạo để trỏ đến một mảng các bản ghi

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
69, được chấm dứt bởi một thành viên có tất cả là
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10 hoặc không. Khi một mô -đun đông lạnh được nhập, nó được tìm kiếm trong bảng này. Mã của bên thứ ba có thể chơi các thủ thuật với điều này để cung cấp một bộ sưu tập các mô-đun đông lạnh được tạo động.

Vì vậy, việc thao túng con trỏ này thậm chí có thể chứng minh hữu ích. Để hạn chế kích thước ví dụ, chúng tôi chỉ hiển thị cách bảng này có thể được đọc với

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
6

Chúng tôi đã xác định kiểu dữ liệu

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
69, vì vậy chúng tôi có thể đưa con trỏ lên bảng:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
7

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
73 là
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
74 đối với mảng của các bản ghi
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
75, chúng tôi có thể lặp lại nó, nhưng chúng tôi chỉ cần đảm bảo rằng vòng lặp của chúng tôi chấm dứt, bởi vì con trỏ không có kích thước. Sớm muộn gì cũng có thể gặp sự cố với vi phạm truy cập hoặc bất cứ điều gì, vì vậy, tốt hơn là thoát ra khỏi vòng lặp khi chúng tôi nhấn mục nhập
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
8

Thực tế là Python tiêu chuẩn có mô -đun đông lạnh và gói đóng băng (được biểu thị bằng thành viên

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77 âm) không được biết đến, nó chỉ được sử dụng để thử nghiệm. Hãy thử nó với
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
78 chẳng hạn.

Bất ngờ bùng

Có một số cạnh trong

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 nơi bạn có thể mong đợi một cái gì đó khác ngoài những gì thực sự xảy ra.

Xem xét ví dụ sau:

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
9

HM. Chúng tôi chắc chắn mong đợi tuyên bố cuối cùng sẽ in

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
80. Chuyện gì đã xảy ra thế? Dưới đây là các bước của dòng
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
81 ở trên:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0

Lưu ý rằng

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
82 và
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
83 là các đối tượng vẫn sử dụng bộ đệm bên trong của đối tượng
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
84 ở trên. Vì vậy, việc thực thi
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
85 sao chép nội dung bộ đệm của
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
82 thành bộ đệm của
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
84. Điều này, đến lượt nó, thay đổi nội dung của
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
83. Vì vậy, nhiệm vụ cuối cùng
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
89, không có hiệu ứng mong đợi.

Hãy nhớ rằng việc truy xuất các đối tượng phụ từ cấu trúc, công đoàn và mảng không sao chép đối tượng phụ, thay vào đó, nó lấy một đối tượng trình bao bọc truy cập vào bộ đệm bên dưới của đối tượng gốc.

Một ví dụ khác có thể hoạt động khác với những gì người ta mong đợi là:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
1

Ghi chú

Hãy chắc chắn rằng bạn tiếp tục tham chiếu đến các đối tượng

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
48 miễn là chúng được sử dụng từ mã C.
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 không có, và nếu bạn don, chúng có thể được thu thập rác, làm hỏng chương trình của bạn khi một cuộc gọi lại được thực hiện.

Ngoài ra, lưu ý rằng nếu hàm gọi lại được gọi trong một luồng được tạo bên ngoài điều khiển Python, (ví dụ: bằng mã nước ngoài gọi gọi lại), CTYPES sẽ tạo ra một luồng Python giả mới trên mỗi lần gọi. Hành vi này là chính xác cho hầu hết các mục đích, nhưng điều đó có nghĩa là các giá trị được lưu trữ bằng

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
58 sẽ không tồn tại trên các cuộc gọi lại khác nhau, ngay cả khi các cuộc gọi đó được thực hiện từ cùng một luồng C.descriptors accessing the contents of the memory. Storing a Python object in the memory block does not store the object itself, instead the
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
08 of the object is stored. Accessing the contents again constructs a new Python object each time!

Truy cập các giá trị được xuất từ ​​DLLS¶

Một số thư viện được chia sẻ không chỉ các chức năng xuất, chúng còn xuất các biến. Một ví dụ trong chính thư viện Python là

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
59, số nguyên được đặt thành 0, 1 hoặc 2, tùy thuộc vào cờ
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
60 hoặc
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
61 được đưa ra khi khởi động.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 có thể truy cập các giá trị như thế này với các phương thức lớp
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
63 của loại. Pythonapi là một biểu tượng được xác định trước cho phép truy cập vào API Python C:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
2

Điều này là tốt và tốt, nhưng làm thế nào người ta sẽ truy cập các yếu tố bổ sung có trong mảng này? Vì loại vẫn chỉ biết về 4 yếu tố, chúng tôi nhận được lỗi truy cập các yếu tố khác:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
3

Một cách khác để sử dụng các loại dữ liệu có kích thước thay đổi với

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 là sử dụng bản chất động của Python và (RE-) xác định loại dữ liệu sau khi kích thước yêu cầu đã được biết, theo từng trường hợp.

Tài liệu tham khảo Ctypes

Tìm thư viện được chia sẻ

Khi lập trình bằng ngôn ngữ được biên dịch, các thư viện được chia sẻ được truy cập khi biên dịch/liên kết một chương trình và khi chương trình được chạy.

Mục đích của chức năng

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
97 là định vị thư viện theo cách tương tự như những gì trình biên dịch hoặc trình tải thời gian chạy (trên các nền tảng có một số phiên bản của thư viện được chia sẻ gần đây nhất sẽ được tải), trong khi bộ tải thư viện CTYPES hoạt động như Chương trình được chạy và gọi Trình tải thời gian chạy trực tiếp.

Mô -đun

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
98 cung cấp một chức năng có thể giúp xác định thư viện để tải.

ctypes.util.find_library(name)find_library(name)

Cố gắng tìm một thư viện và trả về một tên đường dẫn. Tên là tên thư viện không có tiền tố như LIB, hậu tố như

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
99,
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
00 hoặc số phiên bản (đây là biểu mẫu được sử dụng cho tùy chọn Trình liên kết POSIX
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
01). Nếu không có thư viện nào có thể được tìm thấy, trả về
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11.

Chức năng chính xác phụ thuộc vào hệ thống.

Trên Linux,

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
97 cố gắng chạy các chương trình bên ngoài (
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
04,
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
05,
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
06 và
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
07) để tìm tệp thư viện. Nó trả về tên tệp của tệp thư viện.

Đã thay đổi trong phiên bản 3.6: Trên Linux, giá trị của biến môi trường

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
08 được sử dụng khi tìm kiếm các thư viện, nếu không thể tìm thấy thư viện bằng bất kỳ phương tiện nào khác.On Linux, the value of the environment variable
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
08 is used when searching for libraries, if a library cannot be found by any other means.

Dưới đây là một số ví dụ:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
4

Trên các macOS,

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
97 thử một số sơ đồ và đường dẫn đặt tên được xác định trước để xác định vị trí thư viện và trả về một tên đường dẫn đầy đủ nếu thành công:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
5

Trên Windows,

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
97 tìm kiếm dọc theo đường dẫn tìm kiếm hệ thống và trả về toàn bộ tên đường dẫn, nhưng vì không có sơ đồ đặt tên được xác định trước, một cuộc gọi như
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
11 sẽ thất bại và trả về
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11.

Nếu gói một thư viện được chia sẻ với

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8, có thể tốt hơn là xác định tên thư viện được chia sẻ tại thời điểm phát triển và mã cứng vào mô -đun trình bao bọc thay vì sử dụng
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
97 để xác định vị trí thư viện khi chạy.

Tải các thư viện được chia sẻ

Có một số cách để tải các thư viện chia sẻ vào quy trình Python. Một cách là khởi tạo một trong các lớp sau:

classCtyPes.cdll (name, mode = default_mode, tay cầm = khôngctypes.CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)

Các trường hợp của lớp này đại diện cho các thư viện chia sẻ được tải. Các chức năng trong các thư viện này sử dụng quy ước gọi C tiêu chuẩn và được giả định để trả về int.int.

Trên Windows, việc tạo một thể hiện

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
15 có thể thất bại ngay cả khi tên DLL tồn tại. Khi không tìm thấy một DLL phụ thuộc của DLL được tải, lỗi
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
0 được nêu ra với thông báo [[WinError 126], không thể tìm thấy mô -đun được chỉ định. Thông báo lỗi này không chứa tên của DLL bị thiếu vì API Windows không trả về thông tin này khiến lỗi này khó chẩn đoán. Để giải quyết lỗi này và xác định DLL nào không được tìm thấy, bạn cần tìm danh sách các DLL phụ thuộc và xác định cái nào không được tìm thấy bằng cách sử dụng các công cụ gỡ lỗi và truy tìm Windows.

classCtypes.oledll (name, mode = default_mode, xử lýctypes.OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)

Chỉ Windows: Các phiên bản của lớp này thể hiện các thư viện được chia sẻ, các chức năng trong các thư viện này sử dụng quy ước gọi

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7 và được giả định để trả lại mã
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
9 cụ thể của Windows.
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
9 Các giá trị chứa thông tin chỉ định xem cuộc gọi chức năng không thành công hay thành công, cùng với mã lỗi bổ sung. Nếu giá trị trả về báo hiệu lỗi,
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
0 sẽ tự động tăng lên.

Đã thay đổi trong phiên bản 3.3:

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
1 đã từng được nâng lên.
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
1 used to be raised.

ClassCtyPes.Windll (name, mode = default_mode, xử lý = none, use_errno = false, use_last_error = falsectypes.WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False, winmode=None)

Chỉ Windows: Các phiên bản của lớp này thể hiện các thư viện được chia sẻ, các chức năng trong các thư viện này sử dụng quy ước gọi

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7 và được giả định để trả về int theo mặc định.int by default.

Khóa phiên dịch Python Global được phát hành trước khi gọi bất kỳ chức năng nào được xuất bởi các thư viện này và sau đó được phản ứng lại.global interpreter lock is released before calling any function exported by these libraries, and reacquired afterwards.

classCtyPes.pydll (name, mode = default_mode, xử lý = none) ¶ctypes.PyDLL(name, mode=DEFAULT_MODE, handle=None)

Các trường hợp của lớp này hoạt động như các trường hợp

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
15, ngoại trừ việc Python Gil không được phát hành trong cuộc gọi chức năng và sau khi thực thi chức năng, cờ lỗi Python được kiểm tra. Nếu cờ lỗi được đặt, một ngoại lệ python sẽ được nâng lên.

Do đó, điều này chỉ hữu ích để gọi các hàm API Python C trực tiếp.

Tất cả các lớp này có thể được khởi tạo bằng cách gọi chúng với ít nhất một đối số, tên đường dẫn của thư viện được chia sẻ. Nếu bạn có một tay cầm hiện có cho một thư viện được chia sẻ đã được tải, nó có thể được truyền dưới dạng tham số

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
24, nếu không các nền tảng cơ bản
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
25 hoặc
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
26 được sử dụng để tải thư viện vào quy trình và để xử lý nó.

Tham số chế độ có thể được sử dụng để chỉ định cách tải thư viện. Để biết chi tiết, hãy tham khảo bộ truyện Dlopen (3). Trên Windows, chế độ bị bỏ qua. Trên các hệ thống POSIX, RTLD_NOW luôn được thêm vào và không có thể định cấu hình.

Tham số USE_ERRNO, khi được đặt thành TRUE, cho phép cơ chế CTYPES cho phép truy cập số lỗi

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27 của hệ thống một cách an toàn.
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 duy trì một bản sao địa lý của các hệ thống
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27; Nếu bạn gọi các chức năng nước ngoài được tạo bằng
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
30 thì giá trị
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27 trước khi gọi hàm được hoán đổi với bản sao riêng của CTYPES, điều tương tự cũng xảy ra ngay sau khi gọi hàm.

Hàm

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
32 trả về giá trị của bản sao riêng của CTYPES và hàm
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
33 thay đổi bản sao riêng của CTYPES thành một giá trị mới và trả lại giá trị cũ.

Tham số use_last_error, khi được đặt thành TRUE, cho phép cùng một cơ chế cho mã lỗi Windows được quản lý bởi các hàm API Windows và

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
35 Windows;
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
36 và
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
37 được sử dụng để yêu cầu và thay đổi bản sao riêng của CTYPES của mã lỗi Windows.

Tham số WinMode được sử dụng trên Windows để chỉ định cách tải thư viện (vì chế độ bị bỏ qua). Nó lấy bất kỳ giá trị nào hợp lệ cho tham số cờ API ____I

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
38 Win32. Khi bị bỏ qua, mặc định là sử dụng các cờ dẫn đến tải DLL an toàn nhất để tránh các vấn đề như chiếm quyền điều khiển DLL. Chuyển đường dẫn đầy đủ đến DLL là cách an toàn nhất để đảm bảo thư viện và phụ thuộc chính xác được tải.

Đã thay đổi trong phiên bản 3.8: Đã thêm tham số WinMode.Added winmode parameter.

ctypes.RTLD_GLOBALRTLD_GLOBAL

Cờ để sử dụng làm tham số chế độ. Trên các nền tảng không có cờ này, nó được định nghĩa là số nguyên số không.

ctypes.RTLD_LOCALRTLD_LOCAL

Cờ để sử dụng làm tham số chế độ. Trên các nền tảng không có sẵn, nó giống như RTLD_Global.

ctypes.DEFAULT_MODEDEFAULT_MODE

Chế độ mặc định được sử dụng để tải các thư viện chia sẻ. Trên OSX 10.3, đây là RTLD_Global, nếu không thì nó giống như RTLD_LOCAL.

Các trường hợp của các lớp này không có phương pháp công khai. Các chức năng được xuất bởi thư viện được chia sẻ có thể được truy cập dưới dạng thuộc tính hoặc bằng chỉ mục. Xin lưu ý rằng việc truy cập hàm thông qua một thuộc tính lưu trữ kết quả và do đó truy cập nó liên tục trả về cùng một đối tượng mỗi lần. Mặt khác, truy cập nó thông qua một chỉ mục trả về một đối tượng mới mỗi lần:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
6

Các thuộc tính công khai sau đây có sẵn, tên của chúng bắt đầu bằng dấu gạch dưới để không đụng độ với tên chức năng được xuất:

Pydll._handle¶_handle

Tay cầm hệ thống được sử dụng để truy cập thư viện.

Pydll._name¶_name

Tên của thư viện được truyền trong hàm tạo.

Các thư viện được chia sẻ cũng có thể được tải bằng cách sử dụng một trong các đối tượng đúc sẵn, là trường hợp của lớp

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
39, bằng cách gọi phương thức
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
7 hoặc bằng cách truy xuất thư viện làm thuộc tính của phiên bản Trình tải.

ClassCtyPes.L LibraryLoader (DLLTYPE) ¶ ctypes.LibraryLoader(dlltype)

Lớp tải thư viện chia sẻ. DLLTyPE nên là một trong các loại

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
15,
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
42,
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
43 hoặc
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
44.

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
45 có hành vi đặc biệt: Nó cho phép tải thư viện chia sẻ bằng cách truy cập nó dưới dạng thuộc tính của thể hiện trình tải thư viện. Kết quả được lưu trong bộ nhớ cache, do đó, các truy cập thuộc tính lặp lại trả về cùng một thư viện mỗi lần.

Loadl Library (tên) ¶(name)

Tải một thư viện được chia sẻ vào quy trình và trả lại. Phương thức này luôn trả về một thể hiện mới của thư viện.

Các trình tải thư viện đúc sẵn này có sẵn:

ctypes.cdllcdll

Tạo các trường hợp

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
15.

ctypes.windllwindll

Chỉ Windows: Tạo các phiên bản

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
43.

ctypes.oledlloledll

Chỉ Windows: Tạo các phiên bản

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
44.

ctypes.pydllpydll

Tạo các trường hợp

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
42.

Để truy cập trực tiếp vào API Python C, một đối tượng thư viện chia sẻ Python sẵn sàng sử dụng có sẵn:

ctypes.pythonapipythonapi

Một ví dụ

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
42 phơi bày các hàm API Python C dưới dạng thuộc tính. Lưu ý rằng tất cả các chức năng này được giả định để trả lại C int, tất nhiên không phải lúc nào cũng là sự thật, vì vậy bạn phải gán thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 chính xác để sử dụng các chức năng này.int, which is of course not always the truth, so you have to assign the correct
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 attribute to use these functions.

Đang tải thư viện thông qua bất kỳ đối tượng nào trong số này làm tăng sự kiện kiểm toán

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
52 với đối số chuỗi
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
53, tên được sử dụng để tải thư viện.auditing event
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
52 with string argument
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
53, the name used to load the library.

Truy cập một chức năng trên thư viện được tải làm tăng sự kiện kiểm toán

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
54 với các đối số
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
55 (đối tượng thư viện) và
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
53 (tên biểu tượng tên là chuỗi hoặc số nguyên).

Trong trường hợp chỉ có khả năng xử lý thư viện thay vì đối tượng, việc truy cập chức năng sẽ tăng sự kiện kiểm toán

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
57 với các đối số
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
24 (tay cầm thư viện thô) và
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
53.

Chức năng nước ngoài Jo

Như đã giải thích trong phần trước, các chức năng nước ngoài có thể được truy cập dưới dạng thuộc tính của các thư viện được tải. Các đối tượng chức năng được tạo theo cách này theo mặc định chấp nhận bất kỳ số lượng đối số nào, chấp nhận bất kỳ phiên bản dữ liệu CTYPES nào làm đối số và trả về loại kết quả mặc định được chỉ định bởi Trình tải thư viện. Chúng là trường hợp của một lớp tư nhân:

ClassCtyPes._FunCptr¶ctypes._FuncPtr

Lớp cơ sở cho các chức năng nước ngoài có thể gọi C.

Các trường hợp của các chức năng nước ngoài cũng là các loại dữ liệu tương thích C; Chúng đại diện cho con trỏ chức năng C.

Hành vi này có thể được tùy chỉnh bằng cách gán cho các thuộc tính đặc biệt của đối tượng hàm nước ngoài.

restype¶

Gán một loại CTYPES để chỉ định loại kết quả của hàm nước ngoài. Sử dụng

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11 cho void, một chức năng không trả lại bất cứ điều gì.void, a function not returning anything.

Có thể gán một đối tượng Python có thể gọi được không phải là loại CTYPES, trong trường hợp này, hàm được giả định để trả về C int và có thể gọi sẽ được gọi với số nguyên này, cho phép xử lý thêm hoặc kiểm tra lỗi. Sử dụng điều này được không dùng nữa, để xử lý bài đăng hoặc kiểm tra lỗi linh hoạt hơn Sử dụng kiểu dữ liệu CTYPES là

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 và gán một cuộc gọi cho thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
82.int, and the callable will be called with this integer, allowing further processing or error checking. Using this is deprecated, for more flexible post processing or error checking use a ctypes data type as
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 and assign a callable to the
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
82 attribute.

Argtypes¶

Chỉ định một tuple của các loại ctypes để chỉ định các loại đối số mà hàm chấp nhận. Các chức năng bằng cách sử dụng quy ước gọi

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7 chỉ có thể được gọi với cùng số lượng đối số với độ dài của tuple này; Các chức năng bằng cách sử dụng quy ước gọi C cũng chấp nhận các đối số bổ sung, không xác định.

Khi một hàm nước ngoài được gọi, mỗi đối số thực tế được chuyển đến phương thức lớp

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
67 của các mục trong bộ
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64, phương pháp này cho phép điều chỉnh đối số thực tế với một đối tượng mà hàm nước ngoài chấp nhận. Ví dụ: một mục
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
42 trong bộ
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64 sẽ chuyển đổi một chuỗi được truyền làm đối số thành đối tượng byte bằng các quy tắc chuyển đổi CTYPES.

MỚI: Bây giờ có thể đặt các mục vào argtypes không phải là loại CTYPES, nhưng mỗi mục phải có phương thức

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
67 trả về giá trị có thể sử dụng làm đối số (số nguyên, chuỗi, cá thể ctypes). Điều này cho phép xác định các bộ điều hợp có thể điều chỉnh các đối tượng tùy chỉnh dưới dạng tham số chức năng.

errcheck¶

Gán hàm Python hoặc một gọi khác có thể gọi cho thuộc tính này. Người có thể gọi sẽ được gọi với ba hoặc nhiều đối số:

có thể gọi (kết quả, func, đối số)(result, func, arguments)

Kết quả là những gì hàm nước ngoài trả về, theo quy định của thuộc tính

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73.

Func là chính đối tượng chức năng nước ngoài, điều này cho phép tái sử dụng cùng một đối tượng có thể gọi để kiểm tra hoặc đăng quá trình kết quả của một số chức năng.

Các đối số là một bộ chứa các tham số ban đầu được chuyển đến lệnh gọi hàm, điều này cho phép chuyên về hành vi trên các đối số được sử dụng.

Đối tượng mà hàm này trả về sẽ được trả về từ cuộc gọi chức năng nước ngoài, nhưng nó cũng có thể kiểm tra giá trị kết quả và tăng một ngoại lệ nếu cuộc gọi hàm nước ngoài không thành công.

ExceptionCtypes.ArgumentError¶ctypes.ArgumentError

Ngoại lệ này được nêu ra khi một cuộc gọi chức năng nước ngoài không thể chuyển đổi một trong các đối số được thông qua.

Trên Windows, khi một cuộc gọi chức năng nước ngoài làm tăng một ngoại lệ hệ thống (ví dụ, do vi phạm truy cập), nó sẽ bị bắt và thay thế bằng một ngoại lệ Python phù hợp. Hơn nữa, một sự kiện kiểm toán

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
70 với đối số
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
71 sẽ được nêu ra, cho phép một móc kiểm toán thay thế ngoại lệ bằng riêng của nó.

Một số cách để gọi các cuộc gọi chức năng nước ngoài có thể nâng cao một sự kiện kiểm toán

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
72 với các đối số
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
73 và
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
74.

Chức năng nguyên mẫu

Các chức năng nước ngoài cũng có thể được tạo ra bằng cách khởi tạo các nguyên mẫu chức năng. Các nguyên mẫu chức năng tương tự như các nguyên mẫu chức năng trong C; Họ mô tả một hàm (loại trả về, loại đối số, quy ước gọi) mà không xác định việc thực hiện. Các chức năng của nhà máy phải được gọi với loại kết quả mong muốn và các loại đối số của hàm và có thể được sử dụng làm nhà máy trang trí, và do đó, được áp dụng cho các chức năng thông qua cú pháp

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
75. Xem các chức năng gọi lại cho các ví dụ.Callback functions for examples.

ctypes.cfunctype (restype, *argtypes, uses_errno = false, use_last_error = false)CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

Nguyên mẫu chức năng được trả về tạo ra các hàm sử dụng quy ước gọi C tiêu chuẩn. Hàm sẽ phát hành Gil trong cuộc gọi. Nếu use_errno được đặt thành true, bản sao riêng của CTYPES của biến

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27 được trao đổi với giá trị
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27 thực sự trước và sau cuộc gọi; use_last_error làm tương tự cho mã lỗi Windows.

ctypes.winfunctype (restype, *argtypes, uses_errno = false, use_last_error = false)WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

Chỉ Windows: Nguyên mẫu chức năng được trả về tạo các hàm sử dụng quy ước gọi

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
7. Hàm sẽ phát hành Gil trong cuộc gọi. use_errno và use_last_error có cùng ý nghĩa như trên.

ctypes.pyfunctype (restype, *argtypes) ¶PYFUNCTYPE(restype, *argtypes)

Nguyên mẫu chức năng được trả về tạo ra các hàm sử dụng quy ước gọi Python. Hàm sẽ không phát hành GIL trong cuộc gọi.

Các nguyên mẫu chức năng được tạo bởi các chức năng nhà máy này có thể được khởi tạo theo các cách khác nhau, tùy thuộc vào loại và số lượng của các tham số trong cuộc gọi:

prototype(address)(address)

Trả về một hàm nước ngoài tại địa chỉ được chỉ định phải là một số nguyên.

prototype(callable)(callable)

Tạo hàm C có thể gọi (hàm gọi lại) từ Python có thể gọi được.

Nguyên mẫu (func_spec [, paramflags])(func_spec[, paramflags])

Trả về một chức năng nước ngoài được xuất bởi một thư viện chia sẻ. func_spec phải là 2-Tuple

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
79. Mục đầu tiên là tên của hàm được xuất dưới dạng chuỗi hoặc thứ tự của hàm được xuất dưới dạng số nguyên nhỏ. Mục thứ hai là thể hiện thư viện được chia sẻ.

Nguyên mẫu (vtbl_index, name [, paramflags [, iid]]))(vtbl_index, name[, paramflags[, iid]])

Trả về một chức năng nước ngoài sẽ gọi phương thức COM. VTBL_INDEX là chỉ mục vào bảng hàm ảo, một số nguyên không âm nhỏ. Tên là tên của phương thức COM. IID là một con trỏ tùy chọn đến định danh giao diện được sử dụng trong báo cáo lỗi mở rộng.

Các phương thức COM Sử dụng quy ước gọi đặc biệt: Chúng yêu cầu một con trỏ tới giao diện COM làm đối số đầu tiên, ngoài các tham số được chỉ định trong bộ

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64.

Tham số Paramflags tùy chọn tạo ra các trình bao bọc chức năng nước ngoài có nhiều chức năng hơn nhiều so với các tính năng được mô tả ở trên.

Paramflags phải là một tuple có cùng độ dài với

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
64.

Mỗi mục trong tuple này chứa thêm thông tin về một tham số, nó phải là một bộ chứa một, hai hoặc ba mục.

Mục đầu tiên là một số nguyên chứa kết hợp các cờ hướng cho tham số:

1

Chỉ định một tham số đầu vào cho hàm.

2

Tham số đầu ra. Hàm nước ngoài điền vào một giá trị.

4

Tham số đầu vào mặc định cho số nguyên không.

Mục thứ hai tùy chọn là tên tham số là chuỗi. Nếu điều này được chỉ định, hàm nước ngoài có thể được gọi với các tham số được đặt tên.

Mục thứ ba tùy chọn là giá trị mặc định cho tham số này.

Ví dụ này trình bày cách bọc hàm Windows

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
82 để nó hỗ trợ các tham số mặc định và các đối số được đặt tên. Tuyên bố C từ tệp tiêu đề Windows là:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
7

Đây là gói với

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
8

Chức năng nước ngoài

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
84 hiện có thể được gọi theo những cách này:

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
9

Một ví dụ thứ hai thể hiện các tham số đầu ra. Hàm Win32

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
85 lấy kích thước của một cửa sổ được chỉ định bằng cách sao chép chúng vào cấu trúc
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
86 mà người gọi phải cung cấp. Đây là Tuyên bố C:

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
0

Đây là gói với

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8:

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
1

Chức năng nước ngoài

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
84 hiện có thể được gọi theo những cách này:

Một ví dụ thứ hai thể hiện các tham số đầu ra. Hàm Win32

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
85 lấy kích thước của một cửa sổ được chỉ định bằng cách sao chép chúng vào cấu trúc
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
86 mà người gọi phải cung cấp. Đây là Tuyên bố C:

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
2

Các chức năng với các tham số đầu ra sẽ tự động trả về giá trị tham số đầu ra nếu có một hoặc một tuple chứa các giá trị tham số đầu ra khi có nhiều hơn một, do đó, hàm getWindowRect hiện trả về một phiên bản trực tràng, khi được gọi.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
3

Các tham số đầu ra có thể được kết hợp với giao thức >>> cdll.LoadLibrary("libc.so.6") >>> libc = CDLL("libc.so.6") >>> libc >>> 82 để thực hiện xử lý đầu ra và kiểm tra lỗi tiếp theo. Hàm API Win32 /* ANSI version */ HMODULE GetModuleHandleA(LPCSTR lpModuleName); /* UNICODE version */ HMODULE GetModuleHandleW(LPCWSTR lpModuleName); 85 trả về /* ANSI version */ HMODULE GetModuleHandleA(LPCSTR lpModuleName); /* UNICODE version */ HMODULE GetModuleHandleW(LPCWSTR lpModuleName); 90 để báo hiệu thành công hoặc thất bại, do đó, chức năng này có thể thực hiện kiểm tra lỗi và tăng ngoại lệ khi cuộc gọi API không thành công:

Nếu hàm
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
82 trả về đối số, nó không thay đổi,
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 tiếp tục xử lý thông thường mà nó thực hiện trên các tham số đầu ra. Nếu bạn muốn trả về một bộ điều tọa độ cửa sổ thay vì ví dụ
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
86, bạn có thể truy xuất các trường trong hàm và trả lại chúng, việc xử lý thông thường sẽ không còn diễn ra nữa:
addressof(obj)

Các chức năng tiện ích¶

ctypes.addressof (obj) ¶auditing event

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
94 with argument
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
95.

Trả về địa chỉ của bộ đệm bộ nhớ dưới dạng số nguyên. OBJ phải là một ví dụ của loại CTYPES.alignment(obj_or_type)

Tăng một sự kiện kiểm toán

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
94 với đối số
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
95.

ctypes.alignment (obj_or_type) ¶byref(obj[, offset])

Trả về các yêu cầu căn chỉnh của loại CTYPES. obj_or_type phải là loại CTYPES hoặc thể hiện.

ctypes.byref (obj [, offset]) ¶

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
4

Đối tượng được trả về chỉ có thể được sử dụng như một tham số cuộc gọi chức năng nước ngoài. Nó hoạt động tương tự như

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
97, nhưng việc xây dựng nhanh hơn rất nhiều.

ctypes.cast (obj, gõ) ¶cast(obj, type)

Hàm này tương tự như toán tử đúc trong C. Nó trả về một thể hiện mới của loại trỏ đến cùng một khối bộ nhớ với OBJ. Loại phải là loại con trỏ và OBJ phải là một đối tượng có thể được hiểu là một con trỏ.

ctypes.create_string_buffer (init_or_size, size = none) ¶create_string_buffer(init_or_size, size=None)

Hàm này tạo ra một bộ đệm ký tự có thể thay đổi. Đối tượng được trả về là một mảng CTYPE của

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
25.

init_or_size phải là một số nguyên chỉ định kích thước của mảng hoặc đối tượng byte sẽ được sử dụng để khởi tạo các mục mảng.

Nếu một đối tượng byte được chỉ định là đối số đầu tiên, bộ đệm được tạo một mục lớn hơn độ dài của nó để phần tử cuối cùng trong mảng là ký tự kết thúc NUL. Một số nguyên có thể được truyền dưới dạng đối số thứ hai cho phép chỉ định kích thước của mảng nếu không nên sử dụng chiều dài của byte.

Tăng một sự kiện kiểm toán

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
99 với các đối số
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
00,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.auditing event
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
99 with arguments
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
00,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.

ctypes.create_unicode_buffer (init_or_size, size = none) ¶create_unicode_buffer(init_or_size, size=None)

Hàm này tạo ra một bộ đệm ký tự unicode có thể thay đổi. Đối tượng được trả về là một mảng CTYPE của

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
26.

init_or_size phải là một số nguyên chỉ định kích thước của mảng hoặc một chuỗi sẽ được sử dụng để khởi tạo các mục mảng.

Nếu một chuỗi được chỉ định là đối số đầu tiên, bộ đệm được tạo một mục lớn hơn độ dài của chuỗi sao cho phần tử cuối cùng trong mảng là ký tự kết thúc NUL. Một số nguyên có thể được truyền dưới dạng đối số thứ hai cho phép chỉ định kích thước của mảng nếu không nên sử dụng độ dài của chuỗi.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
03 với các đối số
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
00,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
03 with arguments
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
00,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.

ctypes.dllcanunloadnow () ¶DllCanUnloadNow()

Chỉ Windows: Hàm này là một móc cho phép triển khai các máy chủ COM trong quá trình với CTYPE. Nó được gọi từ chức năng dllcanununnow mà _ctypes mở rộng DLL xuất.

ctypes.dllgetClassObject () ¶DllGetClassObject()

Chỉ Windows: Hàm này là một móc cho phép triển khai các máy chủ COM trong quá trình với CTYPE. Nó được gọi từ chức năng dllgetClassObject mà

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
06 xuất xuất DLL.

ctypes.util.find_l Library (tên) ¶find_library(name)

Cố gắng tìm một thư viện và trả về một tên đường dẫn. Tên là tên thư viện không có tiền tố như

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
07, hậu tố như
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
99,
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
00 hoặc số phiên bản (đây là biểu mẫu được sử dụng cho tùy chọn liên kết POSIX
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
01). Nếu không có thư viện nào có thể được tìm thấy, trả về
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11.

Chức năng chính xác phụ thuộc vào hệ thống.

ctypes.util.find_msvcrt () ¶find_msvcrt()

Chỉ Windows: Trả lại tên tệp của thư viện thời gian chạy VC được sử dụng bởi Python và bởi các mô -đun mở rộng. Nếu tên của thư viện không thể được xác định,

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11 sẽ được trả về.

Ví dụ, nếu bạn cần miễn phí bộ nhớ, được phân bổ bởi một mô -đun mở rộng với một cuộc gọi đến

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
13, điều quan trọng là bạn phải sử dụng chức năng trong cùng một thư viện phân bổ bộ nhớ.

ctypes.formaterror ([mã]) ¶FormatError([code])

Chỉ Windows: Trả về mô tả văn bản của mã lỗi. Nếu không có mã lỗi nào được chỉ định, mã lỗi cuối cùng được sử dụng bằng cách gọi hàm API Windows GetLasterRor.

ctypes.getlasterror () ¶GetLastError()

Chỉ Windows: Trả về mã lỗi cuối cùng được đặt bởi Windows trong luồng gọi. Hàm này gọi trực tiếp chức năng Windows

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
81, nó không trả về bản sao CTYPES-Private của mã lỗi.

ctypes.get_errno ()get_errno()

Trả về giá trị hiện tại của bản sao CTYPES-Private của biến

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27 trong luồng gọi.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
16 không có lập luận.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
16 with no arguments.

ctypes.get_last_error () ¶get_last_error()

Chỉ Windows: Trả về giá trị hiện tại của bản sao CTYPES-Private của biến

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
17 trong luồng gọi.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
18 không có lập luận.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
18 with no arguments.

ctypes.memmove (DST, SRC, đếm) ¶memmove(dst, src, count)

Giống như hàm thư viện C tiêu chuẩn C tiêu chuẩn: Bản sao đếm byte từ SRC đến DST. DST và SRC phải là các số nguyên hoặc các trường hợp CTYPES có thể được chuyển đổi thành con trỏ.

ctypes.memset (dst, c, đếm) ¶memset(dst, c, count)

Giống như chức năng thư viện Memset C tiêu chuẩn: điền vào khối bộ nhớ tại địa chỉ DST với số byte của giá trị c. DST phải là một số nguyên chỉ định một địa chỉ hoặc một thể hiện CTYPES.

ctypes.pulum (gõ) ¶POINTER(type)

Chức năng nhà máy này tạo ra và trả về một loại con trỏ CTYPES mới. Các loại con trỏ được lưu trữ và tái sử dụng nội bộ, vì vậy gọi chức năng này nhiều lần là rẻ. Loại phải là loại CTYPES.

ctypes.pulum (obj) ¶pointer(obj)

Hàm này tạo ra một thể hiện con trỏ mới, chỉ vào OBJ. Đối tượng trả về thuộc loại

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
19.

Lưu ý: Nếu bạn chỉ muốn chuyển một con trỏ đến một đối tượng đến một cuộc gọi chức năng nước ngoài, bạn nên sử dụng

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
20 nhanh hơn nhiều.

ctypes.resize (obj, kích thước) ¶resize(obj, size)

Hàm này thay đổi kích thước bộ đệm bộ nhớ trong của OBJ, phải là một ví dụ của loại CTYPES. Không thể làm cho bộ đệm nhỏ hơn kích thước gốc của loại đối tượng, như được cho bởi

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
21, nhưng có thể phóng to bộ đệm.

ctypes.set_errno (giá trị) ¶set_errno(value)

Đặt giá trị hiện tại của bản sao CTYPES-Private của biến

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27 trong luồng gọi cho giá trị và trả về giá trị trước đó.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
23 với đối số
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
23 with argument
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
27.

ctypes.set_last_error (giá trị) ¶set_last_error(value)

Chỉ Windows: Đặt giá trị hiện tại của bản sao CTYPES-Private của biến

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
17 trong luồng gọi cho giá trị và trả về giá trị trước đó.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
26 với đối số
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
27.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
26 with argument
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
27.

ctypes.sizeof (obj_or_type) ¶sizeof(obj_or_type)

Trả về kích thước tính bằng byte thuộc loại CTYPES hoặc bộ nhớ bộ nhớ thể hiện. Thực hiện giống như toán tử c

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
28.

ctypes.string_at (địa chỉ, size = -1) ¶string_at(address, size=- 1)

Hàm này trả về chuỗi C Bắt đầu tại địa chỉ bộ nhớ dưới dạng đối tượng byte. Nếu kích thước được chỉ định, nó được sử dụng làm kích thước, nếu không chuỗi được coi là không kết thúc.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
29 với các đối số
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
30,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
29 with arguments
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
30,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.

ctypes.winerror (code = none, descr = none) ¶WinError(code=None, descr=None)

Chỉ có Windows: Chức năng này có lẽ là thứ được đặt tên tồi tệ nhất trong CTYPES. Nó tạo ra một ví dụ của Oserror. Nếu mã không được chỉ định,

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
32 được gọi để xác định mã lỗi. Nếu DESCR không được chỉ định,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
33 được gọi để nhận mô tả văn bản về lỗi.

Đã thay đổi trong phiên bản 3.3: Một thể hiện của

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
1 được tạo để tạo.An instance of
>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>
1 used to be created.

ctypes.wstring_at (địa chỉ, size = -1) ¶wstring_at(address, size=- 1)

Hàm này trả về chuỗi ký tự rộng bắt đầu tại địa chỉ bộ nhớ dưới dạng chuỗi. Nếu kích thước được chỉ định, nó được sử dụng làm số ký tự của chuỗi, nếu không chuỗi được giả sử là không kết thúc.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
35 với các đối số
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
30,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
35 with arguments
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
30,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77.

Loại dữ liệu¶

ClassCtyPes._CData¶ ctypes._CData

Lớp không công khai này là lớp cơ sở chung của tất cả các loại dữ liệu CTYPES. Trong số những thứ khác, tất cả các phiên bản loại CTYPES đều chứa một khối bộ nhớ chứa dữ liệu tương thích C; Địa chỉ của khối bộ nhớ được trả về bởi hàm trợ giúp

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
38. Một biến thể hiện khác được hiển thị là
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
39; Điều này chứa các đối tượng Python khác cần được giữ sống trong trường hợp khối bộ nhớ chứa con trỏ.

Các phương pháp phổ biến của các loại dữ liệu CTYPES, đây là tất cả các phương pháp lớp (chính xác là, chúng là phương pháp của metaclass):metaclass):

from_buffer (nguồn [, offset]) ¶(source[, offset])

Phương thức này trả về một thể hiện CTYPES chia sẻ bộ đệm của đối tượng nguồn. Đối tượng nguồn phải hỗ trợ giao diện bộ đệm có thể ghi. Tham số Offset tùy chọn chỉ định độ lệch vào bộ đệm nguồn trong byte; Mặc định bằng không. Nếu bộ đệm nguồn không đủ lớn,

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
13 sẽ được nâng lên.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
41 với các đối số
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
74,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
44.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
41 with arguments
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
74,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
44.

From_buffer_copy (nguồn [, offset]) ¶(source[, offset])

Phương pháp này tạo ra một thể hiện CTYPES, sao chép bộ đệm từ bộ đệm đối tượng nguồn phải đọc được. Tham số Offset tùy chọn chỉ định độ lệch vào bộ đệm nguồn trong byte; Mặc định bằng không. Nếu bộ đệm nguồn không đủ lớn,

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
13 sẽ được nâng lên.

Tăng một sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
41 với các đối số
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
74,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
44.auditing event
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
41 with arguments
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
74,
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
77,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
44.

From_buffer_copy (nguồn [, offset]) ¶(address)

Phương pháp này tạo ra một thể hiện CTYPES, sao chép bộ đệm từ bộ đệm đối tượng nguồn phải đọc được. Tham số Offset tùy chọn chỉ định độ lệch vào bộ đệm nguồn trong byte; Mặc định bằng không. Nếu bộ đệm nguồn không đủ lớn,

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
13 sẽ được nâng lên.

From_address (địa chỉ) ¶auditing event

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
50 with argument
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
30.

Phương thức này trả về một thể hiện loại CTYPES bằng cách sử dụng bộ nhớ được chỉ định bởi địa chỉ phải là một số nguyên.(obj)

Phương pháp này và các phương pháp khác gọi phương pháp này một cách gián tiếp, tăng sự kiện kiểm toán

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
50 với đối số
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
30.

From_param (obj) ¶

Phương pháp này điều chỉnh OBJ theo loại CTYPES. Nó được gọi với đối tượng thực tế được sử dụng trong một cuộc gọi chức năng nước ngoài khi loại có trong hàm nước ngoài, ____ ____164 Tuple; Nó phải trả về một đối tượng có thể được sử dụng làm tham số cuộc gọi hàm.(library, name)

Tất cả các loại dữ liệu CTYPES đều có một triển khai mặc định của ClassMethod này thường trả về OBJ nếu đó là một thể hiện của loại. Một số loại chấp nhận các đối tượng khác là tốt.

in_dll (thư viện, tên) ¶

Phương thức này trả về một thể hiện loại CTYPES được xuất bởi một thư viện được chia sẻ. Tên là tên của biểu tượng xuất dữ liệu, thư viện là thư viện chia sẻ được tải.

Các biến thể hiện phổ biến của các loại dữ liệu CTYPES:

_b_needsfree_¶

Biến chỉ đọc này là đúng khi phiên bản dữ liệu CTYPES đã tự phân bổ khối bộ nhớ, sai.

_các đối tượng¶

Thành viên này là

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
11 hoặc từ điển có chứa các đối tượng Python cần được giữ sống để nội dung khối bộ nhớ được giữ hợp lệ. Đối tượng này chỉ được tiếp xúc để gỡ lỗi; Không bao giờ sửa đổi nội dung của từ điển này.

Các loại dữ liệu cơ bản

ClassCtyPes._SimpleCdata¶ ctypes._SimpleCData

Lớp không công khai này là lớp cơ sở của tất cả các loại dữ liệu CTYPES cơ bản. Nó được đề cập ở đây bởi vì nó chứa các thuộc tính phổ biến của các loại dữ liệu CTYPES cơ bản.

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
55 là một lớp con của
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
56, vì vậy nó kế thừa các phương thức và thuộc tính của chúng. Các loại dữ liệu CTYPES không và không chứa con trỏ bây giờ có thể được ngâm.

Các phiên bản có một thuộc tính duy nhất:

giá trị¶

Thuộc tính này chứa giá trị thực của trường hợp. Đối với các loại số nguyên và con trỏ, nó là một số nguyên, cho các loại ký tự, nó là một đối tượng hoặc chuỗi byte ký tự, cho các loại con trỏ ký tự, nó là một đối tượng hoặc chuỗi byte python.

Khi thuộc tính

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
53 được lấy từ một thể hiện CTYPES, thường là một đối tượng mới được trả về mỗi lần.
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 không thực hiện trả về đối tượng gốc, luôn luôn là một đối tượng mới được xây dựng. Điều tương tự cũng đúng với tất cả các phiên bản đối tượng CTYPES khác.

Các loại dữ liệu cơ bản, khi được trả lại dưới dạng kết quả gọi hàm nước ngoài, hoặc, ví dụ, bằng cách truy xuất các thành viên trường cấu trúc hoặc các mục mảng, được chuyển đổi một cách minh bạch thành các loại python gốc. Nói cách khác, nếu một hàm nước ngoài có

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 là
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
42, bạn sẽ luôn nhận được đối tượng Byte Python, không phải là một ví dụ
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
42.

Các lớp con của các loại dữ liệu cơ bản không kế thừa hành vi này. Vì vậy, nếu một chức năng nước ngoài

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
73 là một lớp con của
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
46, bạn sẽ nhận được một thể hiện của lớp con này từ cuộc gọi chức năng. Tất nhiên, bạn có thể nhận được giá trị của con trỏ bằng cách truy cập thuộc tính
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
53.

Đây là các loại dữ liệu CTYPES cơ bản:

classCtypes.c_byte¶ ctypes.c_byte

Đại diện cho kiểu dữ liệu C đã ký và diễn giải giá trị là số nguyên nhỏ. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.signedchar datatype, and interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.c_char¶ctypes.c_char

Đại diện cho kiểu dữ liệu c char và diễn giải giá trị như một ký tự. Trình xây dựng chấp nhận một trình khởi tạo chuỗi tùy chọn, độ dài của chuỗi phải chính xác là một ký tự.char datatype, and interprets the value as a single character. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

ClassCtypes.c_char_p¶ctypes.c_char_p

Đại diện cho kiểu dữ liệu c char* khi nó trỏ đến chuỗi kết thúc bằng không. Đối với một con trỏ ký tự chung cũng có thể chỉ vào dữ liệu nhị phân,

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
65 phải được sử dụng. Hàm tạo chấp nhận một địa chỉ số nguyên hoặc đối tượng byte.char* datatype when it points to a zero-terminated string. For a general character pointer that may also point to binary data,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
65 must be used. The constructor accepts an integer address, or a bytes object.

ClassCtypes.c_double¶ctypes.c_double

Đại diện cho kiểu dữ liệu c kép. Chất xây dựng chấp nhận một bộ khởi tạo float tùy chọn.double datatype. The constructor accepts an optional float initializer.

ClassCtypes.c_longdouble¶ctypes.c_longdouble

Đại diện cho kiểu dữ liệu C LongDouble. Chất xây dựng chấp nhận một bộ khởi tạo float tùy chọn. Trên các nền tảng trong đó

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
66 là bí danh của
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
40.longdouble datatype. The constructor accepts an optional float initializer. On platforms where
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
66 it is an alias to
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
40.

ClassCtypes.c_float¶ctypes.c_float

Đại diện cho kiểu dữ liệu c float. Chất xây dựng chấp nhận một bộ khởi tạo float tùy chọn.float datatype. The constructor accepts an optional float initializer.

ClassCtypes.c_int¶ctypes.c_int

Đại diện cho kiểu dữ liệu C đã ký. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện. Trên các nền tảng trong đó

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
68 là bí danh của
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
2.signedint datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
68 it is an alias to
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
2.

classCtypes.c_int8¶ctypes.c_int8

Đại diện cho kiểu dữ liệu chữ C 8 bit. Thường là bí danh cho

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
27.signed int datatype. Usually an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
27.

ClassCtypes.c_int16¶ctypes.c_int16

Đại diện cho kiểu dữ liệu chữ C 16 bit. Thường là bí danh cho

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
29.signedint datatype. Usually an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
29.

classCtypes.c_int32¶ctypes.c_int32

Đại diện cho kiểu dữ liệu có chữ ký C 32 bit. Thường là bí danh cho

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0.signedint datatype. Usually an alias for
>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0.

classCtypes.c_int64¶ctypes.c_int64

Đại diện cho kiểu dữ liệu ký tên C 64 bit. Thường là bí danh cho

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
35.signedint datatype. Usually an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
35.

ClassCtypes.c_long¶ ctypes.c_long

Đại diện cho kiểu dữ liệu C đã ký. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.signedlong datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.C_LongLong¶ ctypes.c_longlong

Đại diện cho kiểu dữ liệu C SignedLongLong. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.signedlonglong datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.c_short¶ctypes.c_short

Đại diện cho kiểu dữ liệu C đã ký. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.signedshort datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.c_size_t¶ ctypes.c_size_t

Đại diện cho kiểu dữ liệu C

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
74.

ClassCtyPes.c_ssize_t¶ ctypes.c_ssize_t

Đại diện cho kiểu dữ liệu C

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
75.

Mới trong phiên bản 3.2.

ClassCtypes.c_ubyte¶ ctypes.c_ubyte

Đại diện cho kiểu dữ liệu c unsignchar, nó diễn giải giá trị dưới dạng số nguyên nhỏ. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.unsignedchar datatype, it interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.c_uint¶ctypes.c_uint

Đại diện cho kiểu dữ liệu c không dấu C. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện. Trên các nền tảng trong đó

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
68 là bí danh cho
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
34.unsignedint datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
68 it is an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
34.

ClassCtypes.c_uint8¶ctypes.c_uint8

Đại diện cho kiểu dữ liệu không dấu C 8-bit. Thường là bí danh cho

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
28.unsignedint datatype. Usually an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
28.

ClassCtypes.c_uint16¶ctypes.c_uint16

Đại diện cho kiểu dữ liệu không dấu C 16-bit. Thường là bí danh cho

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
30.unsignedint datatype. Usually an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
30.

ClassCtypes.c_uint32¶ ctypes.c_uint32

Đại diện cho kiểu dữ liệu không dấu C 32 bit. Thường là bí danh cho

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
32.unsignedint datatype. Usually an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
32.

ClassCtypes.c_uint64¶ctypes.c_uint64

Đại diện cho kiểu dữ liệu không dấu C 64 bit. Thường là bí danh cho

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
36.unsignedint datatype. Usually an alias for
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
36.

classctypes.c_ulong¶ctypes.c_ulong

Đại diện cho kiểu dữ liệu c unsignedLong. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.unsigned long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.c_ulonglong¶ctypes.c_ulonglong

Đại diện cho kiểu dữ liệu c unsignedLongLong. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.unsigned longlong datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.c_ushort¶ctypes.c_ushort

Đại diện cho kiểu dữ liệu c unsignshort. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn; Không có kiểm tra tràn được thực hiện.unsignedshort datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

ClassCtypes.c_void_p¶ctypes.c_void_p

Đại diện cho loại C VOID*. Giá trị được biểu diễn dưới dạng số nguyên. Hàm tạo chấp nhận một bộ khởi tạo số nguyên tùy chọn.void* type. The value is represented as integer. The constructor accepts an optional integer initializer.

ClassCtypes.c_wchar¶ctypes.c_wchar

Đại diện cho kiểu dữ liệu C wchar_t và diễn giải giá trị dưới dạng chuỗi unicode ký tự duy nhất. Trình xây dựng chấp nhận một trình khởi tạo chuỗi tùy chọn, độ dài của chuỗi phải chính xác là một ký tự.wchar_t datatype, and interprets the value as a single character unicode string. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

ClassCtypes.c_wchar_p¶ctypes.c_wchar_p

Đại diện cho kiểu dữ liệu c wchar_t*, phải là một con trỏ tới chuỗi ký tự rộng kết thúc bằng không. Hàm tạo chấp nhận một địa chỉ số nguyên hoặc một chuỗi.wchar_t* datatype, which must be a pointer to a zero-terminated wide character string. The constructor accepts an integer address, or a string.

ClassCtypes.c_bool¶ctypes.c_bool

Đại diện cho kiểu dữ liệu c bool (chính xác hơn, _bool từ C99). Giá trị của nó có thể là

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
82 hoặc
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
17 và hàm tạo chấp nhận bất kỳ đối tượng nào có giá trị sự thật.bool datatype (more accurately, _Bool from C99). Its value can be
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
82 or
>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
17, and the constructor accepts any object that has a truth value.

ClassCtypes.Hresult¶ctypes.HRESULT

Chỉ Windows: đại diện cho giá trị

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
9, chứa thông tin thành công hoặc lỗi cho một cuộc gọi chức năng hoặc phương thức.

ClassCtyPes.py_Object¶ctypes.py_object

Đại diện cho C PyObject* Kiểu dữ liệu. Gọi điều này mà không có một đối số tạo ra con trỏ pyobject*

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10.PyObject* datatype. Calling this without an argument creates a
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
10 PyObject* pointer.

Mô -đun

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
86 cung cấp khá nhiều loại dữ liệu cụ thể Windows khác, ví dụ
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
87,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
88 hoặc
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
89. Một số cấu trúc hữu ích như
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
90 hoặc
/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
86 cũng được xác định.

Các loại dữ liệu có cấu trúc

classctypes.union (*args, ** kw) ¶ ctypes.Union(*args, **kw)

Lớp cơ sở trừu tượng cho các công đoàn theo thứ tự byte bản địa.

ClassCtyPes.BigendianUnion (*args, ** kw) ¶ctypes.BigEndianUnion(*args, **kw)

Lớp cơ sở trừu tượng cho các công đoàn theo thứ tự byte endian lớn.

Mới trong phiên bản 3.11.

ClassCtypes.littleendianunion (*args, ** kw) ¶ctypes.LittleEndianUnion(*args, **kw)

Lớp cơ sở trừu tượng cho các công đoàn theo thứ tự byte endian nhỏ.

Mới trong phiên bản 3.11.

ClassCtypes.littleendianunion (*args, ** kw) ¶ctypes.BigEndianStructure(*args, **kw)

Lớp cơ sở trừu tượng cho các công đoàn theo thứ tự byte endian nhỏ.

ClassCtyPes.BigendianStrure (*args, ** kw) ¶ ctypes.LittleEndianStructure(*args, **kw)

Lớp cơ sở trừu tượng cho các cấu trúc theo thứ tự byte endian lớn.

ClassCtyPes.LittleendianStrure (*args, ** kw) ¶

Lớp cơ sở trừu tượng cho các cấu trúc theo thứ tự byte endian nhỏ.ctypes.Structure(*args, **kw)

Các cấu trúc và công đoàn có thứ tự byte không phải là người bản địa không thể chứa các trường loại con trỏ hoặc bất kỳ loại dữ liệu nào khác có chứa các trường loại con trỏ.

classctypes.structure (*args, ** kw) ¶descriptors which allow reading and writing the fields by direct attribute accesses. These are the

Lớp cơ sở trừu tượng cho các cấu trúc theo thứ tự byte bản địa.

Cấu trúc cụ thể và các loại liên kết phải được tạo bằng cách phân lớp một trong các loại này và ít nhất là xác định biến lớp

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91.
>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 sẽ tạo các mô tả cho phép đọc và viết các trường bằng cách truy cập thuộc tính trực tiếp. Đây là những

_lĩnh vực_¶

Một chuỗi xác định các trường cấu trúc. Các mặt hàng phải là 2-n-n-nế hoặc 3-n-nế. Mục đầu tiên là tên của trường, mục thứ hai chỉ định loại trường; Nó có thể là bất kỳ loại dữ liệu CTYPES.

Đối với các trường loại số nguyên như

>>> windll.kernel32.GetModuleHandleA(32)  
Traceback (most recent call last):
  File "", line 1, in 
OSError: exception: access violation reading 0x00000020
>>>
0, một mục tùy chọn thứ ba có thể được đưa ra. Nó phải là một số nguyên dương nhỏ xác định độ rộng bit của trường.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
5

Tên trường phải là duy nhất trong một cấu trúc hoặc liên minh. Điều này không được kiểm tra, chỉ có thể truy cập một trường khi tên được lặp lại.

Có thể xác định biến lớp

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 Sau câu lệnh Lớp xác định lớp con cấu trúc, điều này cho phép tạo các loại dữ liệu trực tiếp hoặc gián tiếp tham chiếu:

Tuy nhiên, biến lớp
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 phải được xác định trước khi loại được sử dụng lần đầu tiên (một thể hiện được tạo,
>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")  
<_FuncPtr object at 0x...>
>>>
97 được gọi trên nó, v.v.). Các bài tập sau này cho biến lớp
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 sẽ nâng cao thuộc tính.

Có thể xác định các lớp phụ của các loại cấu trúc, chúng kế thừa các trường của lớp cơ sở cộng với

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 được xác định trong lớp phụ, nếu có.

_đóng gói_¶

Một số nguyên nhỏ tùy chọn cho phép ghi đè sự liên kết của các trường cấu trúc trong trường hợp.

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
97 phải được xác định khi
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 được chỉ định, nếu không nó sẽ không có hiệu lực.

Các trường được liệt kê trong biến này phải là các trường cấu trúc hoặc loại liên kết.

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 sẽ tạo các mô tả trong loại cấu trúc cho phép truy cập trực tiếp vào các trường lồng nhau, mà không cần phải tạo cấu trúc hoặc trường liên kết.

Dưới đây là một loại ví dụ (Windows):

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
6

Cấu trúc

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
05 mô tả kiểu dữ liệu COM, trường
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
06 chỉ định một trong các trường liên minh là hợp lệ. Do trường
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
07 được định nghĩa là trường ẩn danh, giờ đây có thể truy cập các thành viên trực tiếp ra khỏi phiên bản đánh máy.
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
08 và
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
09 là tương đương, nhưng trước đây nhanh hơn vì nó không cần phải tạo một ví dụ liên minh tạm thời:

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
7

Có thể xác định các lớp phụ của các cấu trúc, chúng kế thừa các trường của lớp cơ sở. Nếu định nghĩa lớp con có biến

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 riêng biệt, các trường được chỉ định trong này được nối vào các trường của lớp cơ sở.

Cấu trúc và nhà xây dựng công đoàn chấp nhận cả đối số vị trí và từ khóa. Các đối số vị trí được sử dụng để khởi tạo các trường thành viên theo cùng thứ tự như chúng xuất hiện trong

>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91. Các đối số từ khóa trong hàm tạo được hiểu là bài tập thuộc tính, vì vậy chúng sẽ khởi tạo
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91 với cùng tên hoặc tạo các thuộc tính mới cho các tên không có trong
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
91.

Mảng và Con trỏ

classCtypes.Array (*args) ¶ctypes.Array(*args)

Lớp cơ sở trừu tượng cho các mảng.

Cách được đề xuất để tạo các loại mảng cụ thể là nhân bất kỳ loại dữ liệu

>>> cdll.kernel32.GetModuleHandleA(None)  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")  
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>
8 nào với số nguyên không âm. Ngoài ra, bạn có thể phân lớp loại này và xác định các biến lớp
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
15 và
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
16. Các phần tử mảng có thể được đọc và viết bằng cách sử dụng quyền truy cập chỉ số và cắt lát tiêu chuẩn; Đối với các lát đọc, đối tượng kết quả không phải là
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
17.

_chiều dài_¶

Một số nguyên dương Chỉ định số lượng phần tử trong mảng. Các chỉ số ngoài phạm vi dẫn đến một

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
18. Sẽ được trả lại bởi
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
19.

_loại hình_¶

Chỉ định loại của mỗi phần tử trong mảng.

Các hàm tạo lớp con mảng chấp nhận các đối số vị trí, được sử dụng để khởi tạo các phần tử theo thứ tự.

ClassCtyPes._Pulum¶ctypes._Pointer

Riêng, lớp cơ sở trừu tượng cho con trỏ.

Các loại con trỏ bê tông được tạo bằng cách gọi

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)  
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)     
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>
13 với loại sẽ được chỉ ra; Điều này được thực hiện tự động bởi
>>> cdll.LoadLibrary("libc.so.6")  

>>> libc = CDLL("libc.so.6")       
>>> libc                           

>>>
85.

Nếu một con trỏ trỏ đến một mảng, các phần tử của nó có thể được đọc và viết bằng cách sử dụng chỉ số tiêu chuẩn và truy cập lát cắt. Các đối tượng con trỏ không có kích thước, vì vậy

>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
19 sẽ tăng
>>> cdll.kernel32[1]  
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]  
Traceback (most recent call last):
  File "", line 1, in 
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>
23. Các chỉ số tiêu cực sẽ đọc từ bộ nhớ trước con trỏ (như trong C) và các chỉ số ngoài phạm vi có thể sẽ gặp sự cố với vi phạm truy cập (nếu bạn may mắn).

_loại hình_¶

Chỉ định loại của mỗi phần tử trong mảng.

Các hàm tạo lớp con mảng chấp nhận các đối số vị trí, được sử dụng để khởi tạo các phần tử theo thứ tự.

ClassCtyPes._Pulum¶