Hướng dẫn python ctypes read memory address - python ctypes đọc địa chỉ bộ nhớ

Tôi tình cờ làm việc về vấn đề tương tự. Tải tập lệnh Python của tôi. Có thư viện để nhận địa chỉ bộ đệm hình ảnh từ C ++ .so. Sau khi tôi nhận được địa chỉ bộ đệm, tôi cần có khả năng đọc từng byte trong bộ đệm. Tôi đã sử dụng "From_address" để tạo đối tượng danh sách:

imageBytes = list(c_ubyte * dataSize).from_address(pointer)

Sau đây cho thấy chi tiết cách lấy địa chỉ bộ nhớ được chuyển từ C ++ sang Pyth và cách truy cập dữ liệu bộ nhớ ở phía Python. Trong C ++ Code FrameProvider.cpp:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}

Python của tôi:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])


>>> 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 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.

Hướng dẫn Ctypes

Lưu ý: Các mẫu mã trong hướng dẫn này sử dụ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
>>>
2 để đả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 CTYPES

>>> 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. Trên các nền tảng trong đó
>>> 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 là bí danh của
>>> 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. Vì vậy, bạn không nên bị nhầm lẫn nếu
>>> 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 được in nếu bạn mong đợ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
>>>
3 - 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[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 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

>>> 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 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
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0. OLEDLL cũng sử dụng quy ước gọi
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0 và giả sử các chức năng trả về mã lỗi Windows
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
2. Mã lỗi được sử dụng để tự động tăng ngoại lệ
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
3 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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
4, hiện là bí danh của
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
3.Windows errors used to raise
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
4, which is now an alias of
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
3.

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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
6 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ố

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
7 thông thường.

Ghi chú

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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
8 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
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
6.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
00 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ư

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
01 và
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
02 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
03 được gắn vào tên, trong khi phiên bản ANSI được xuất với
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
04 được gắn vào tên. Hàm Win32
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
05, 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à
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
05 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
07 hoặc
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
08, 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ư

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
09. Trong trường hợp này, bạn phải sử dụng
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
10 để 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
11, trả về thời gian hệ thống tính bằng giây kể từ Epoch Unix và hàm
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
12, 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ỏ

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13 (
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14 nên được sử dụng làm con trỏ
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13):

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
16 được nâng lên khi bạn gọi chức năng
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0 với quy ước gọ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
>>>
9 hoặc ngược lại:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
0

Để 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[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 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ệ:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
1

Tuy nhiên, có đủ cách để đánh sập Python vớ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
>>>
1, vì vậy dù sao bạn cũng nên cẩn thận. Mô -đun
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
21 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).

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14, 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.
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14 được truyền dưới dạng con trỏ C
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13, 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[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.

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

>>> 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 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
27

_Bool

Bool (1)

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
28

char

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
29

wchar_t

Chuỗi 1 ký tự

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
30

char

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
31

không dấuchar

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
32

short

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
33

Undignedshortshort

int

>>> 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

int

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
35

không dấuint

int

>>> 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

long

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
37

UnsignedLonglong

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
38

__int64 hoặc dài or longlong

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
39

Undign__int64 hoặc UndignedLongLong__int64 or unsignedlonglong

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
40

size_t

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
41

ssize_t hoặc py_ssize_t or Py_ssize_t

int

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
42

float

float

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
43

double

float

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
44

Longdoubledouble

float

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
45

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

Đối tượng byte hoặc

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
47

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

chuỗi hoặc

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
49

void**

int hoặc

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14

  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:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
2

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 đó:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
3

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ỏ

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
45,
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
47 và
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
49 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):

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
4

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
54 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
55; 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
56:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
5

Hàm

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
54 thay thế hàm
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
58 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
59.wchar_t, use the
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
59 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
60, vì vậy các ví dụ này sẽ chỉ hoạt động tại dấu nhắc giao diện điều khiển, không phải từ bên trong Idle hoặc Pythonwin:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
6

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[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 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:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
7

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[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 để 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[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 tìm kiếm một thuộc tính
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
64 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:

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
8

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
64, bạn có thể xác định
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
66 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67.

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67 phải là một chuỗi các loại dữ liệu C (hàm
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
69 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):

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
9

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ệ:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
0

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
70 để chúng có thể sử dụng chúng trong chuỗi
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67. Phương thức lớp
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
70 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
64 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[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 hoặc một đối tượng có thuộc tính
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
64.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 của đối tượng hàm.int type. Other return types can be specified by setting the
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 attribute of the function object.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
77, mong đợi một con trỏ chuỗi và char, và trả về một con trỏ cho một chuỗi:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
1

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
78 ở trên, bạn có thể đặt thuộc tính
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67 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:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
2

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76, 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ệ:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
3

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
81 là một hàm sẽ gọi API Windows ____182 để lấy biểu diễn chuỗi của mã lỗi và trả về một ngoại lệ.
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
81 có tham số mã lỗi tùy chọn, nếu không có ai được sử dụng, nó gọi
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
84 để truy xuất nó.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
85; 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[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 Xuất hàm
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
87 đượ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 hàm
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
88, mặc dù
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
88 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
87 nhanh hơn nếu bạn không cần đối tượng con trỏ trong chính Python:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
4

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ở

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
91 và
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
92 được xác định trong mô -đun
>>> 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. Mỗi lớp con phải xác định thuộc tính
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94.
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 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[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 như
>>> 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 hoặc bất kỳ loạ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
>>>
1 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:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
5

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:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
6

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:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
7

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:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
8

Cảnh báo

>>> 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 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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
00 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ì
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
01 cũng làm trong MSVC.

>>> 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 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ở
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
03,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
04,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
05 và
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
06. 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ộ

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94:

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
9

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 *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
0

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 *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
1

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 *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
2

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
08, 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:

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
3

Con trỏ

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
88 theo loạ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
>>>
1:

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
4

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
11 trả về đối tượng mà các điểm con trỏ, đối tượng
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
12 ở trên:

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
5

Lưu ý rằ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
>>>
1 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:

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
6

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

>>> 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 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ữ:

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
7

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ỏ:

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
8

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, chức năng

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
88 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
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
16, chấp nhận bất kỳ loạ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
>>>
1 nào và trả về một loại mới:

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
9

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13.
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13 Con trỏ có giá trị boolean
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
20:

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

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

>>>
0

>>> 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 kiểm tra
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13 khi các con trỏ phá hủy (nhưng không hợp lệ không hợp lệ -________ 113 Con trỏ sẽ làm hỏng Python):

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

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

>>>
1

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ó

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
24 trong danh sách
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67 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
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
24, CTYPES chấp nhận một mảng C_INT:

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

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

>>>
2

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ư

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
24) trong
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67, một đối tượng của loại nhọn (
>>> 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 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
87 trong trường hợp này.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13, bạn có thể gán
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14:

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

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

>>>
3

Đô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[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 cung cấp chức năng
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
34 có thể được sử dụng theo cùng một cách. Cấu trúc
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
35 được xác định ở trên chấp nhận
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
24 Con trỏ hoặc mả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
>>>
3 cho trường
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
38 của nó, nhưng không phải là trường hợp của các loại khác:

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

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

>>>
4

Đối với những trường hợp này, chức năng

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
34 là tiện dụng.

Hàm

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
34 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.
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
34 có hai tham số, một đối tượng CTYPES hoặc có thể được chuyển đổi thành một con trỏ của một loại 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:

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

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

>>>
5

Vì vậy,

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
34 có thể được sử dụng để gán cho trường
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
38 của
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
35 Cấu trúc:

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

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

>>>
6

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:

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

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

>>>
7

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

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

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

>>>
8

bởi vì

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
45 mới không có sẵn trong bản sao câu. Trong
>>> 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, chúng ta có thể xác định lớp
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
47 và đặt thuộc tính
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 sau, sau tuyên bố lớp:

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

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

>>>
9

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
47 và để chúng chỉ cho nhau và cuối cùng theo chuỗi con trỏ một vài lầ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
>>>
0

Chức năng gọi lạ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
>>>
1 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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
51 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
>>> 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. Trên Windows, chức năng nhà máy
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
53 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
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0.

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 ____ ____255, đượ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.

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
55 sẽ được sử dụng để sắp xếp một loạt các số nguyê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
>>>
1

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
55 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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
58 cho 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
>>>
2

Để 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:

>>> 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

Kết quả:

>>> 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

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:

>>> 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

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ờ:

>>> 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

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:

>>> 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

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
51 miễn là chúng được sử dụng từ mã 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
>>>
1 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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
61 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à

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
62, số nguyên được đặt thành 0, 1 hoặc 2, tùy thuộc vào cờ
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
63 hoặc
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
64 được đưa ra khi khởi độ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
>>>
1 có thể truy cập các giá trị như thế này với các phương thức lớp
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
66 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:

>>> 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

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
63, mẫu sẽ được in
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
68 hoặc
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
69 nếu
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
64 sẽ đượ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ỏ

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
71 đượ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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
72, được chấm dứt bởi một thành viên có tất cả là
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13 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[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:

>>> 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

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
72, vì vậy chúng tôi có thể đưa con trỏ lên bảng:

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
76 là
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
77 đối với mảng của các bản ghi
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
78, 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 vào mục
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13:

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

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80 âm) không được biết đến, nó chỉ được sử dụng để thử nghiệm. Hãy thử nó với
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
81 chẳng hạn.

Bất ngờ bùng

Có một số cạnh trong

>>> 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 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:

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

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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
83. Chuyện gì đã xảy ra thế? Dưới đây là các bước của dòng
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
84 ở trên:

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

Lưu ý rằng

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
85 và
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
86 là các đối tượng vẫn sử dụng bộ đệm bên trong của đối tượng
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
87 ở trên. Vì vậy, việc thực thi
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
88 sao chép nội dung bộ đệm của
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
85 thành bộ đệm của
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
87. Điều này, đến lượt nó, thay đổi nội dung của
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
86. Vì vậy, nhiệm vụ cuối cùng
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
92, 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à:

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);
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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
51 miễn là chúng được sử dụng từ mã 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
>>>
1 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.

Tại sao nó in

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
20? Các phiên bản CTYPES là các đối tượng chứa một khối bộ nhớ cộng với một số mô tả truy cập vào nội dung của bộ nhớ. Lưu trữ một đối tượng Python trong khối bộ nhớ không lưu trữ đối tượng, thay vào đó,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
11 của đối tượng được lưu trữ. Truy cập nội dung một lần nữa xây dựng một đối tượng Python mới mỗi lần!descriptors accessing the contents of the memory. Storing a Python object in the memory block does not store the object itself, instead the
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
11 of the object is stored. Accessing the contents again constructs a new Python object each time!

Kiểu dữ liệu có kích thước thay đổ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
>>>
1 cung cấp một số hỗ trợ cho các mảng và cấu trúc có kích thước thay đổi.

Hàm

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
97 có thể được sử dụng để thay đổi kích thước bộ đệm bộ nhớ của đối tượng CTYPES hiện có. Hàm lấy đối tượng làm đối số đầu tiên và kích thước được yêu cầu trong byte làm đối số thứ hai. Khối bộ nhớ không thể được tạo nhỏ hơn khối bộ nhớ tự nhiên được chỉ định bởi loại đối tượng,
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
16 được nâng lên nếu điều này được thử:

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

Đ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:

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

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[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 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 hàm

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
00 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 *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
01 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 *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
02,
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
03 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
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
04). Nếu không có thư viện nào có thể được tìm thấy, trả về
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14.

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

Trên Linux,

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
00 cố gắng chạy các chương trình bên ngoài (
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
07,
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
08,
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
09 và
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
10) để 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
11 đượ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
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
11 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ụ:

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

Trên macOS,

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
00 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:

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

Trên Windows,

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
00 tìm kiếm dọc theo đường dẫn tìm kiếm hệ thống và trả về toàn bộ đườ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ư
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
14 sẽ thất bại và trả về
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14.

Nếu gói thư viện chia sẻ vớ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
>>>
1, 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 *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
00 để định vị 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
18 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
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
3 đượ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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0 và được giả định để trả về mã
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
2 cụ thể của Windows.
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
2 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,
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
3 sẽ tự động tăng lên.

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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
4 đã từng được nâng lên.
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
4 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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
18, 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ố

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
27, nếu không các nền tảng cơ bản
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
28 hoặc
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
29 đượ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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30 hệ thống một cách an toàn.
>>> 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 duy trì một bản sao địa phương của biến
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30; Nếu bạn gọi các chức năng nước ngoài được tạo bằng
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
33 thì giá trị
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
35 trả về giá trị của bản sao riêng của CTYPES và hàm
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
36 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à

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
38 Windows;
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
39 và
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
40 đượ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 Win32

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
41. 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:

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

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
42, bằng cách gọi phương thức
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
00 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
18,
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
45,
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
46 hoặc
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
47.

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
48 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
18.

ctypes.windllwindll

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
46.

ctypes.oledlloledll

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
47.

ctypes.pydllpydll

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
45.

Để 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ụ của

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
45 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
55 với đối số chuỗi
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
56, tên được sử dụng để tải thư viện.auditing event
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
55 with string argument
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
56, 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
57 với các đối số
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
58 (đối tượng thư viện) và
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
56 (tên biểu tượng tên dưới dạng 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
60 với các đối số
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
27 (tay cầm thư viện RAW) và
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
56.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14 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à

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 và gán một cuộc gọi có thể gọi cho thuộc tính
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
85.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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 and assign a callable to the
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
85 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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0 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 sang phương thức lớp

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
70 của các mục trong bộ
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67, 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
45 trong bộ
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
70 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76.

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
73 với đối số
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
74 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
75 với các đối số
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
76 và
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
77.

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
78. 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30 được trao đổi với giá trị
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30 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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
0. 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
82. 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ộ

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
67.

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
85 để 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à:

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

Đây là gói vớ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
>>>
1:

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

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
87 hiện có thể được gọi theo những cách này:

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

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
88 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
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
89 mà người gọi phải cung cấp. Đây là Tuyên bố C:

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

Đây là gói vớ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
>>>
1:

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

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
87 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
88 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
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
89 mà người gọi phải cung cấp. Đây là Tuyên bố C:

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

Nếu hàm

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
85 trả về đối số, nó không thay đổ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
>>>
1 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ụ
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
89, 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:

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

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

ctypes.addressof (obj) ¶addressof(obj)

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.

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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
97 với đối số
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
98.auditing event
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
97 with argument
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
98.

ctypes.alignment (obj_or_type) ¶alignment(obj_or_type)

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]) ¶byref(obj[, offset])

Trả về một con trỏ trọng lượng nhẹ cho OBJ, đó phải là một ví dụ của loại CTYPES. Offset mặc định về 0 và phải là một số nguyên sẽ được thêm vào giá trị con trỏ bên trong.

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
99 tương ứng với mã C này:

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

Đố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ư

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

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

>>>
00, 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 CTYPES là

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
28.

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

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

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

>>>
02 với các đối số
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
03,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
02 with arguments
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
03,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.

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 CTYPES là

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
29.

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

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

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

>>>
06 với các đối số
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
03,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
06 with arguments
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
03,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.

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à

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

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

>>>
09 exports 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ư

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

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

>>>
10, hậu tố như
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
02,
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
03 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
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
04). Nếu không có thư viện nào có thể được tìm thấy, trả về
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14.

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,

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14 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

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

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

>>>
16, đ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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
84, 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30 trong luồng gọi.

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

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

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

>>>
19 không có lập luận.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
19 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

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

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

>>>
20 trong luồng gọi.

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

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

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

>>>
21 không có lập luận.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
21 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

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

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

>>>
22.

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

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

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

>>>
23 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

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

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

>>>
24, 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

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

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30 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

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

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

>>>
26 với đối số
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
26 with argument
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
30.

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

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

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

>>>
20 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

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

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

>>>
29 với đối số
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
30.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
29 with argument
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
30.

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

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

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

>>>
31.

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

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

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

>>>
32 với các đối số
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
33,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
32 with arguments
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
33,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.

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,

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

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

>>>
35 được gọi để xác định mã lỗi. Nếu DESCR không được chỉ định,
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
36 đượ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

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
4 được tạo để tạo.An instance of
>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
4 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

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

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

>>>
38 với các đối số
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
33,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
38 with arguments
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
33,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80.

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

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

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

>>>
41. Một biến thể hiện khác được hiển thị là
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
42; Đ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,

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
16 sẽ được nâng lên.

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

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

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

>>>
44 với các đối số
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
77,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80,
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
47.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
44 with arguments
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
77,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80,
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
47.

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,

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
16 sẽ được nâng lên.

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

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

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

>>>
44 với các đối số
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
77,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80,
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
47.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
44 with arguments
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
77,
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
80,
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
47.

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,

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
16 sẽ được nâng lên.

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, nêu ra một sự kiện kiểm toán

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

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

>>>
53 với đối số
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
33.auditing event
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
53 with argument
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
33.

From_param (obj) ¶(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, ____ ____167 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.

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) ¶(library, name)

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_base_¶

Đôi khi các phiên bản dữ liệu CTYPES không sở hữu khối bộ nhớ mà chúng chứa, thay vào đó chúng chia sẻ một phần của khối bộ nhớ của một đối tượng cơ sở. Thành viên chỉ đọc

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

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

>>>
56 là đối tượng CTYPES gốc sở hữu khối bộ nhớ.

_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à

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
14 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.

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

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

>>>
58 là một lớp con của
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
59, 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
56 đượ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[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 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ó

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 của
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
45, bạn sẽ luôn nhận được một đối tượng Byte Python, chứ không phải là một ví dụ
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
45.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
76 là một lớp con của
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
49, 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
56.

Đâ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, phải sử dụng

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

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

>>>
68. 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,
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
68 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 đó

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

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

>>>
69 là bí danh của
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
43.longdouble datatype. The constructor accepts an optional float initializer. On platforms where
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
69 it is an alias to
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
43.

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 đó

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

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

>>>
71 là bí danh của
>>> 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.signedint datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
71 it is an alias to
>>> 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.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
30.signed int datatype. Usually an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
30.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
32.signedint datatype. Usually an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
32.

classCtypes.c_int32¶ctypes.c_int32

Đại diện cho kiểu dữ liệu có chữ ký C 32 bit. Thường là một bí danh cho

>>> 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.signedint datatype. Usually an alias for
>>> 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.

classCtypes.c_int64¶ctypes.c_int64

Đại diện cho kiểu dữ liệu ký tên C 64 bit. Thường là một bí danh cho

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
38.signedint datatype. Usually an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
38.

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

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

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

>>>
77.

ClassCtyPes.c_ssize_t¶ ctypes.c_ssize_t

Đại diện cho kiểu dữ liệu C

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

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

>>>
78.

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 đó

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

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

>>>
71 là bí danh cho
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
37.unsignedint datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
71 it is an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
37.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
31.unsignedint datatype. Usually an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
31.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
33.unsignedint datatype. Usually an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
33.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
35.unsignedint datatype. Usually an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
35.

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
39.unsignedint datatype. Usually an alias for
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
39.

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à

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

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

>>>
85 hoặc
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
20 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
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
85 or
import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
20, and the constructor accepts any object that has a truth value.

ClassCtypes.Hresult¶ctypes.HRESULT

Chỉ Windows: Thể hiện giá trị

>>> print(libc.time(None))  
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))  
0x1d000000
>>>
2, 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*

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13.PyObject* datatype. Calling this without an argument creates a
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
13 PyObject* pointer.

Mô -đun

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

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

>>>
89 cung cấp khá nhiều loại dữ liệu cụ thể Windows khác, ví dụ
>>> cdll.LoadLibrary("libc.so.6")  

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

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

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

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

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

>>>
92. Một số cấu trúc hữu ích như
>>> cdll.LoadLibrary("libc.so.6")  

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

>>>
93 hoặc
>>> from ctypes import *
>>> print(windll.kernel32)  

>>> print(cdll.msvcrt)      

>>> libc = cdll.msvcrt      
>>>
89 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94.
>>> 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 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_¶

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 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:

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

Tuy nhiên, biến lớp

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 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,
>>> 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 được gọi trên nó, v.v.). Các bài tập sau này cho biến lớp
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 đượ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.

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
00 phải được xác định khi
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 được chỉ định, nếu không nó sẽ không có hiệu lực.

_vô danh_¶

Một chuỗi tùy chọn liệt kê tên của các trường không tên (ẩn danh).

>>> 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 phải được xác định khi
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 đượ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[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 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):

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

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
>>>
08 mô tả kiểu dữ liệu COM, 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
>>>
09 chỉ định một trong các trường liên minh là hợp lệ. Do 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
>>>
10 đượ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.
>>> 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
>>>
11 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
>>>
12 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[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

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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 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

dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94. 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94 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
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
94.

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[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 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
>>> 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
>>>
18 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
>>>
19. 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à
>>> 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
>>>
20.

_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

>>> 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. Sẽ được trả lại bở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
>>>
22.

_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

import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)

class dataPack(ctypes.Structure):
    _fields_ = [("width",ctypes.c_int),
                ("height",ctypes.c_int),
                ("buffersize",ctypes.c_int),
                ("bufferAddress", ctypes.c_void_p)]

lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
 from_address(data.contents.bufferAddress))        

print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])
16 với loại sẽ được chỉ ra; Điều này được thực hiện tự động bởi
dataPackPtr = new DataPack(); 

DataPack * getFrame(){
    uint32_t width = 1920;
    uint32_t height = 1208;
    const size_t buffersize = width * height * 4;//rgba, each color is one byte
    unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);
    memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.
    dataPackPtr->width = width;
    dataPackPtr->height = height;
    dataPackPtr->buffersize = buffersize;
    dataPackPtr->bufferPtr = rgbaImage;
    return dataPackPtr;
}

extern "C" {
    DataPack* getFrame_wrapper(){
        return getFrame();
    }
}
88.

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

>>> 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
>>>
22 sẽ 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
>>>
26. 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¶