Python Standard Library >> Generic Operating System Services
>> ctypes
a foreign function library for Python
Reference>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
finding shared libraries<<<<<<<<<<<<<<<<<<<<<<<
>> ctypes.util.find_library(name)
return None|pathname
Linux:find_library('m') => libm.so.*
loading shared libraries<<<<<<<<<<<<<<<<<<<<<<<
>> class ctypes.CDLL(name, mode=DEFAULT_MODE,handle=None,use_errno=False,use_last_error=False)
return int
using standard c calling convention
>> class ctypes.OleDLL(name,mode=DEFAULT_MODE,handle=None,use_errno=False,use_last_error=False)
return HRESULT
Windows only
using stdcall
>> class ctypes.WinDLL(name,mode=DEFAULT_MODE,handle=None,use_errno=False,use_last_error=False)
return int
Windows only
using stdcall
>> class ctypes.PyDLL(name,mode=DEFAULT_MODE,handle=None)
call Python C API functions
>> ctypes.RTLD_GLOBAL
>> ctypes.RTLD_LOCAL
>> ctypes.DEFAULT_MODE
>> PyDLL._handle
>> PyDLL._name
>> class ctypes.LibraryLoader(dlltype)
dlltype=CDLL|PyDLL|WinDLL|OleDLL
>> ctypes.cdll
>> ctypes.windll
>> ctypes.oledll
>> ctypes.pydll
>> ctypes.pythonapi
foreign functions<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>> class ctypes._FuncPtr
base class for c callable foreign functions
>> restype
return int
>> errcheck
callable(result, func, arguments)
>> exception ctypes.ArgumentError
function prototypes<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>> ctypes.CFUNCTYPE(restype,*argtypes,use_errno=False,use_last_error=False)
>> ctypes.WINFUNCTYPE(restype,*argtypes,use_errno=False,use_last_error=False)
>> ctypes.PYFUNCTYPE(restype,*argtypes)
>> prototype(address)
return func at address
>> prototype(callable)
create c callable func
>> prototype(func_spec[,parameters])
return shared lib func
func_type = (name_or_ordinal, library)
>> prototype(vtbl_index, name[, paramflags[, iid]]))
return COM func
utility functions<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>> ctypes.addressof(obj)
return int(obj address in memory)
>> ctypes.alignment(obj_or_type)
return alignment
>> ctypes.byref(obj[, offset])
return light-weight pointer to obj
>> ctypes.case(obj, type)
return new pointer to obj and with type type
>> ctypes.create_string_buffer(init_or_size[,size])
return c_char array
>> ctypes.create_unicode_buffer(init_or_size[,size])
return c_wchar array
>> ctypes.DllCanUnloadNow()
Windows only
>> ctypes.DllGetClassObject()
Windows only
>> ctypes.util.find_library(name)
>> ctypes.find_msvcrt()
>> ctypes.FormatError([code])
>> ctypes.GetLastError()
>> ctypes.get_errno()
>> ctypes.get_last_error()
>> ctypes.memmove(dst, src, count)
>> ctypes.memset(dst, c, count)
>> ctypes.POINTER(type)
>> ctypes.pointer(obj)
>> ctypes.resize(obj, size)
>> ctypes.set_conversion_mode(encoding, errors)
>> ctypes.set_errno(value)
>> ctypes.set_last_error(value)
>> ctypes.sizeof(obj_or_type)
>> ctypes.sizeof(obj_or_type)
>> ctypes.string_at(address[,size])
>> ctypes.WinError(code=None,descr=None)
>> ctypes.wstring_at(address[,size])
data types<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>> class ctypesl._CData
>> from_buffer(source[,offset])
>> from_buffer_copy(source[,offset])
>> from_address(address)
>> from_param(obj)
>> in_dll(library, name)
>> _b_base_
>> _b_needsfree_
>> _objects
fundamental data types<<<<<<<<<<<<<<<<<<<<<<<<<
>> class ctypes._SimpleCData
>> value
>> class ctypes.c_byte
>> class ctypes.c_char
>> class ctypes.c_char_p
>> class ctypes.c_double
>> class ctypes.c_longdouble
>> class ctypes.c_float
>> class ctypes.c_int
>> class ctypes.c_int8
>> class ctypes.c_int16
>> class ctypes.c_int32
>> class ctypes.c_int64
>> class ctypes.c_long
>> class ctypes.c_longlong
>> class ctypes.c_short
>> class ctypes.c_size_t
>> class ctypes.c_ssize_t
>> class ctypes.c_ubyte
>> class ctypes.c_uint
>> class ctypes.c_uint8
>> class ctypes.c_uint16
>> class ctypes.c_uint32
>> class ctypes.c_uint64
>> class ctypes.c_ulong
>> class ctypes.c_ulonglong
>> class ctypes.c_ushort
>> class ctypes.c_void_p
>> class ctypes.c_wchar
>> class ctypes.c_wchar_p
>> class ctypes.c_bool
>> class ctypes.HRESULT
>> class ctypes.py_object
>> class ctypes.wintypes
structured data types<<<<<<<<<<<<<<<<<<<<<<<<<<
>> class ctypes.Union(*args, **kw)
>> class ctypes.BigEndianStructure(*args, **kw)
>> class ctypes.LittleEdianStructure(*args, **kw)
>> class ctypes.Structure(*args, **kw)
>> _fields_
>> _pack_
>> _anonymous_
tutorial>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
loading dynamic link libraries<<<<<<<<<<<<<<<<<
>> cdll, load standard cdecl calling convention
>> windll, Windows only, load stdcall calling convention
>> oledll, Windows only, load stdcall calling convention, return HRESULT
>> from ctypes import *
Win:windll.kernel32, cdll.msvcrt
Lin:cdll.LoadLibrary('libc.so.6'), CDLL('libc.so.6')
accessing functions from loaded dlls<<<<<<<<<<<
>> from ctypes import *
<libc>.printf
windll.kernel32.GetModuleHandleA
cdll.kernel32[1]
calling functions<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>> <libc>.time(None)
>> hex(windll.kernel32.GetModuleHandleA(None))
pythonPkg_ctypes
最新推荐文章于 2023-01-18 14:20:07 发布