python中嵌入C运行时函数
2017-01-01 00:28
232 查看
详细的文档看这里:https://docs.python.org/3.6/library/ctypes.html?highlight=ctypes#module-ctypes
需要注意的是文档中提到的几点:
cdll 中的C函数的调用约定是cdecl 的,windll 中的C函数的调用约定是stdcall 的,Windows API专用。
1、Note
Accessing the standard C library through
python 标准库中存在msvcrt 库,尽量是用msvct 模块中的接口而不是 ctypes.cdll.msvct中的接口。
2、
tries to protect you from calling functions with the wrong numberof arguments or the wrong calling convention. Unfortunately this only works on Windows.
说明在windows是调用C语言函数还有优势呢。
is passed as a C
or
their value is masked to fitinto the C type.cdll.msvcrt.xxx 函数的参数使用C语言的类型(ctypes.c_cXXX),当然用python的 None、int、btyes、str 类型能对应到C语言的NULL、int、char*, wchar*,实践中发现 wchar_t* ,貌似不是很好用,所以尽量只用python 的 bytes. 其他python的类型都需要用相应的 ctypes.c_xxx 来包一下。
ctypes 中的其他函数当然都是python 函数啦,所以使用时当然都是python 数据类型。
所以这里打印浮点数需要使用 ctypes.c_float().
当然也可以指定参数的类型,这样就能直接使用 3.14 作为参数,而不需要转换了。
3、By default functions are assumed to return the C
C标准库的 strchr 的第二个参数是int 类型,所以下边这里需要使用 ord() 函数。
4、ctypes.byref() 函数用来产生一个C语言数据的指针,提供给cytpes.msvcrt.xxx 函数使用。当然使用ctypes.pointer() 函数也可以。
需要注意的是文档中提到的几点:
cdll 中的C函数的调用约定是cdecl 的,windll 中的C函数的调用约定是stdcall 的,Windows API专用。
1、Note
Accessing the standard C library through
cdll.msvcrtwill use an outdated version of the library that may be incompatible with the onebeing used by Python. Where possible, use native Python functionality,or else import and use the
msvcrtmodule.
python 标准库中存在msvcrt 库,尽量是用msvct 模块中的接口而不是 ctypes.cdll.msvct中的接口。
2、
ctypes
tries to protect you from calling functions with the wrong numberof arguments or the wrong calling convention. Unfortunately this only works on Windows.
说明在windows是调用C语言函数还有优势呢。
None,integers, bytes objects and (unicode) strings are the only native Python objects that can directly be used as parameters in these function calls.
None
is passed as a C
NULLpointer, bytes objects and strings are passed as pointer to the memory block that contains their data (
char*
or
wchar_t*). Pythonintegers are passed as the platforms default C
inttype,
their value is masked to fitinto the C type.cdll.msvcrt.xxx 函数的参数使用C语言的类型(ctypes.c_cXXX),当然用python的 None、int、btyes、str 类型能对应到C语言的NULL、int、char*, wchar*,实践中发现 wchar_t* ,貌似不是很好用,所以尽量只用python 的 bytes. 其他python的类型都需要用相应的 ctypes.c_xxx 来包一下。
ctypes 中的其他函数当然都是python 函数啦,所以使用时当然都是python 数据类型。
ctypes.cdll.msvcrt.printf(b"Anint %d, a double %f\n", 1234, ctypes.c_float(3.14))
所以这里打印浮点数需要使用 ctypes.c_float().
当然也可以指定参数的类型,这样就能直接使用 3.14 作为参数,而不需要转换了。
printf.argtypes = [c_char_p, c_int, c_float]
3、By default functions are assumed to return the C
inttype.
C标准库的 strchr 的第二个参数是int 类型,所以下边这里需要使用 ord() 函数。
strchr.restype = c_char_p # c_char_p is a pointer to a string strchr(b"abcdef", ord("d"))
4、ctypes.byref() 函数用来产生一个C语言数据的指针,提供给cytpes.msvcrt.xxx 函数使用。当然使用ctypes.pointer() 函数也可以。
import ctypes i = ctypes.c_int(0xffffffff) ctypes.cdll.msvcrt.memset(ctypes.byref(i), 0, 4) print(i.value)
相关文章推荐
- [ZT]C++ 扩展和嵌入 Python
- 可爱的 Python:用 Psyco 让 Python 运行得像 C 一样快
- 用Python编写运行Hello World程序
- 如何在LINUX/UNIX上运行PYTHON程序
- c++中嵌入python入门
- C/C++中嵌入Python
- C++ 扩展和嵌入 Python
- Python:定时运行脚本
- C++ 应用程序中 嵌入的python脚本与C++程序的互相调用(访问 通信)
- 可爱的 Python:用 Psyco 让 Python 运行得像 C 一样快
- 扩展和嵌入Python 解释器(Extending and Embedding the Python Interpreter)--第一章
- C++嵌入Python最简例
- C++ 扩展和嵌入 Python
- C++ 扩展和嵌入 Python(1)
- Python嵌入C++(1)
- [ZT]C++ 扩展和嵌入 Python
- 扩展和嵌入Python 解释器(Extending and Embedding the Python Interpreter)--摘要
- c++中嵌入perl,python
- 如何在python下直接运行py程序
- C++ 扩展和嵌入 Python