ctypes使得python能夠直接呼叫c語言開發的動態鏈結庫,非常強大。
為了使用ctypes,你必須依次完成以下步驟:
* 編寫動態連線庫程式
* 載入動態連線庫
* 將python的物件轉換為ctypes所能識別的引數
* 使用ctypes的引數呼叫動態連線庫中的函式
一、windows下使用python的ctypes呼叫dll動態鏈結庫
編寫dll檔案 開啟vs2008,新建乙個vc工程,選擇win32型別,win32專案,用程式型別選擇dll………
呼叫方式見linux呼叫方式。
二、linux下使用python的ctypes呼叫so動態鏈結庫
編寫so檔案
1
2
#include "stdio.h"
3
4
void
test();
5
float
add(
float
,
float
);
01
02
#include "test.h"
03
04
void
test()
05
08
09
float
add(
float
a,
float
b)
10
1
gcc -fpic -shared
test
.c -o libtest.so
2
3
#-fpic 編譯成位置無關**,必須 不然你的程式在別的地方肯可能執行不了
4
#-shared 當然是說要編譯成共享庫了
python呼叫so動態鏈結庫
01
#!/usr/bin/env python
02
# -*-coding:utf-8-*-
03
04
print
"sss"
05
06
from
ctypes
import
*
07
08
test
=
cdll.loadlibrary(
"./libtest.so"
)
09
10
test.test()
11
12
add
=
test.add
13
add.argtypes
=
[c_float, c_float]
# 引數型別,兩個float(c_float內ctypes型別)
14
add.restype
=
c_float
15
16
print
add(
1.2
,
19.2
)
發發的related posts:
flash/python socket
python websocket server
python 手冊
linux中c/c++標頭檔案說明
及時阻止ssh暴力破解入侵者方法
**:
python動態呼叫函式
舉例 檔案 windows params 包含兩個方法,兩個引數 a 1 b 2 def count print count def get print get 然後來呼叫啦 import windows params as lp def invoke method method value eva...
python 動態呼叫函式
def do foo print foo class print object def do foo self print dynamic,foo staticmethod def static foo print static foo def main obj print func name do...
python呼叫dll動態庫
python呼叫動態庫有兩種型別,主要看dll的匯出函式的呼叫約定 stdll和 cdecl 對應的動態庫的呼叫方式為 ctypes.cdll.loadlibrary test.dll 對應 cdecl呼叫方式 ctypes.windll.loadlibrary test.dll 對應 stdll呼...