← 返回命令列表

Linux command

ctypes 命令

文本

涉及管道、覆盖或删除,执行前请先确认路径和参数。

常用示例

Load a shared library

python -c "from ctypes import CDLL; lib = CDLL('[libname.so]')"

Call a C function

python -c "from ctypes import CDLL; lib = CDLL('libc.so.6'); print(lib.getpid())"

Pass string argument to C function

python -c "from ctypes import CDLL, c_char_p; lib = CDLL('libc.so.6'); lib.puts(c_char_p(b'Hello'))"

Define function argument and return types

python -c "from ctypes import CDLL, c_int; lib = CDLL('libm.so.6'); lib.abs.argtypes = [c_int]; lib.abs.restype = c_int; print(lib.abs(-42))"

Access Windows DLL

python -c "from ctypes import windll; windll.user32.MessageBoxW(0, 'Hello', 'Title', 0)"

说明

ctypes is a Python standard library module for calling functions in C shared libraries and DLLs. It provides C-compatible data types and allows calling functions with appropriate argument and return type specifications. The module enables Python code to interface with native libraries without writing C extension modules. It handles type conversion between Python and C, pointer management, and structure/union definitions. Common uses include accessing system libraries, using hardware interfaces, integrating legacy C code, and calling platform-specific APIs (Windows DLLs, macOS frameworks).

FAQ

What is the ctypes command used for?

ctypes is a Python standard library module for calling functions in C shared libraries and DLLs. It provides C-compatible data types and allows calling functions with appropriate argument and return type specifications. The module enables Python code to interface with native libraries without writing C extension modules. It handles type conversion between Python and C, pointer management, and structure/union definitions. Common uses include accessing system libraries, using hardware interfaces, integrating legacy C code, and calling platform-specific APIs (Windows DLLs, macOS frameworks).

How do I run a basic ctypes example?

Run `python -c "from ctypes import CDLL; lib = CDLL('[libname.so]')"` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

Where can I find more ctypes examples?

This page includes 5 examples for ctypes, plus related commands for nearby Linux tasks.