加载动态连接库。 采用load的方式加载python
from ctypes import * handle = cdll.LoadLibrary("./test.dll") print(handle)
可能出现的错误code
FileNotFoundError: Could not find module './dest.dll'. Try using the full path with constructor syntax. OSError: [WinError 193] %1 不是有效的 Win32 应用程序。
这两个都是版本不兼容的缘由。 只须要修改32 | 64
位编译便可。get
访问方法。it
cpp
代码io
#include<stdio.h> extern "C" int test() { printf("hello test\n"); return 0; }
python
代码编译
from ctypes import * handle = cdll.LoadLibrary("./test.dll") handle.test()
编译执行。class
g++ -shared -fPIC -m32 test.cpp -o test.dll
org++ -shared -fPIC -m64 test.cpp -o test.dll
test
python test.py
import
另外一种访问连接库方法。module
from ctypes import * handle = cdll.LoadLibrary("./test.dll") getattr(handle,"test")()
由于有可能动态连接库的方法是一种不符合python
命名规范进行命名的。如1231asfdasdfsa#!QW
。方法名是以数字开头,并且中间还包含有特殊字符。所以能够经过attr
的方式进行访问。
可能会如下标的方式进行访问。如cdll.kernel32[1]
。