理解ctypes模块in Python

    技术2022-07-10  89

    ctypes

    ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

    ctypes tutorial

    loading dynamic link libraries

    ctypes exports the cdll, and on Windows windll and oledll objects, for loading dynamic link libraries.

    Accessing functions from loaded dlls
    Calling functions
    Fundamental data types
    Calling functions, continued
    Calling functions with your own custom data types
    Specifying the required argument types(function prototypes)
    Return types
    Passing pointers
    Structures and unions
    Structure/union alignment and byte order
    Bit fields in structures and unions
    Arrays
    Pointers

    Pointer instances are created by calling the pointer() function on a ctypes type:

    >>> from ctypes import * >>> i = c_int(42) >>> pi = pointer(i)

    Pointer instances have a contents attribute which returns the object to chich the pointer points, the i object above:

    >>> pi.contents c_int(42)

    ctypes does not have OOR(Original Object Return), it constructs a new, equivalent object each time you retrieve an attribute:

    >>> pi.contents is i False >>> id(pi.contents) 140583999808432 >>> id(pi.contents) 140583999809008

    Assigning another c_int instance to the pointer’s contents attribute would cause the pointer to ponit to the memory location where this is stored.

    >>> i = c_int(90) >>> pi.contents = i >>> pi.contents c_int(90) >>> i = c_int(30) >>> pi.contents = i >>> pi.contents c_int(30)

    Pointer instance can also be indexed with integers:

    >>> pi <__main__.LP_c_int object at 0x7fdc435ea440> >>> pi[0] 30 >>> pi[1] 0 >>> pi[2] 0

    Assigning to an integer index changes the pointed to value:

    >>> print(i) c_int(30) >>> pi[0] 30 >>> pi[0] = 20 >>> pi[0] 20 >>>

    It is also possible to use indexes different from 0, but you must know what you’re doing, just as in C: You can access or change arbitrary memory locations. Generally you only use this feature is you receive a pointer from a C funtion, and you know that the pointer actually points to an array instead of a single item.

    Behind the scenes, the pointer() function does more than simpley create pointer instances, it has to create poiter types first. This is done with the POINTER() function, which accepts any ctypes type, and returns a new type:

    >>> PI = POINTER(c_int) >>> PI <class '__main__.LP_c_int'> >>> PI(42) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: expected c_int instead of int >>> PI(i) <__main__.LP_c_int object at 0x7fdc435ea5f0> >>>

    Calling the pointer type without an argument creates a NULL pointer.NUll pointers have a False boolean value:

    >>> null_ptr = POINTER(c_int) >>> bool(null_ptr) True >>> print(bool(null_ptr)) True >>> null_ptr = POINTER(c_int)() >>> print(bool(null_ptr)) False >>>
    Type conversions
    Incomplete Types
    Callback functions
    Accessing values exported from dlls
    Surprises
    Variable-sized data types
    Processed: 0.015, SQL: 9