国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python調(diào)用C/C++動(dòng)態(tài)鏈接庫(kù)的方法詳解

2019-11-25 18:18:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文以實(shí)例講解了Python調(diào)用C/C++ DLL動(dòng)態(tài)鏈接庫(kù)的方法,具體示例如下:

示例一:

首先,在創(chuàng)建一個(gè)DLL工程(本例創(chuàng)建環(huán)境為VS 2005),頭文件:

//hello.h#ifdef EXPORT_HELLO_DLL#define HELLO_API __declspec(dllexport)#else#define HELLO_API __declspec(dllimport)#endifextern "C"{ HELLO_API int IntAdd(int , int);}

CPP文件:

//hello.cpp#define EXPORT_HELLO_DLL#include "hello.h"HELLO_API int IntAdd(int a, int b){ return a + b;}

這里有兩個(gè)注意點(diǎn):

(1)弄清楚編譯的時(shí)候函數(shù)的調(diào)用約定采用的__cdecl還是__stdcall,因?yàn)楦鶕?jù)DLL中函數(shù)調(diào)用約定方式,Python將使用相應(yīng)的函數(shù)加載DLL。

(2)如果采用C++的工程,那么導(dǎo)出的接口需要extern "C",這樣python中才能識(shí)別導(dǎo)出的函數(shù)。

我的工程中采用__cdecl函數(shù)調(diào)用約定方式進(jìn)行編譯鏈接產(chǎn)生hello.dll,然后Python中采用ctypes庫(kù)對(duì)hello.dll進(jìn)行加載和函數(shù)調(diào)用:

from ctypes import *dll = cdll.LoadLibrary('hello.dll');ret = dll.IntAdd(2, 4);print ret;

至此,第一個(gè)小例子已經(jīng)完成了,讀者可以自己動(dòng)手嘗試一下運(yùn)行效果。

示例二:

示例一只是一個(gè)"hello world"級(jí)別的程序,實(shí)際運(yùn)用中更多的需要傳遞數(shù)據(jù)結(jié)構(gòu)、字符串等,才能滿足我們的需求。那么本示例將展示,如何傳遞數(shù)據(jù)結(jié)構(gòu)參數(shù),以及如何通過數(shù)據(jù)結(jié)構(gòu)獲取返回值。

首先編寫DLL工程中的頭文件:

//hello.h#ifdef EXPORT_HELLO_DLL#define HELLO_API __declspec(dllexport)#else#define HELLO_API __declspec(dllimport)#endif#define ARRAY_NUMBER 20#define STR_LEN 20struct StructTest{ int number; char* pChar; char str[STR_LEN]; int iArray[ARRAY_NUMBER];};extern "C"{ //HELLO_API int IntAdd(int , int); HELLO_API char* GetStructInfo(struct StructTest* pStruct);}

CPP文件如下:

//hello.cpp#include <string.h>#define EXPORT_HELLO_DLL#include "hello.h"HELLO_API char* GetStructInfo(struct StructTest* pStruct){ for (int i = 0; i < ARRAY_NUMBER; i++) pStruct->iArray[i] = i; pStruct->pChar = "hello python!"; strcpy (pStruct->str, "hello world!"); pStruct->number = 100; return "just OK";}

GetStructInfo這個(gè)函數(shù)通過傳遞一個(gè)StructTest類型的指針,然后對(duì)對(duì)象中的屬性進(jìn)行賦值,最后返回"just OK".

編寫Python調(diào)用代碼如下,首先在Python中繼承Structure構(gòu)造一個(gè)和C DLL中一致的數(shù)據(jù)結(jié)構(gòu)StructTest,然后設(shè)置函數(shù)GetStructInfo的參數(shù)類型和返回值類型,最后創(chuàng)建一個(gè)StructTest對(duì)象,并將其轉(zhuǎn)化為指針作為參數(shù),調(diào)用函數(shù)GetStrcutInfo,最后通過輸出數(shù)據(jù)結(jié)構(gòu)的值來(lái)檢查是否調(diào)用成功

from ctypes import *ARRAY_NUMBER = 20;STR_LEN = 20;#define typeINTARRAY20 = c_int * ARRAY_NUMBER;CHARARRAY20 = c_char * STR_LEN;#define structclass StructTest(Structure):  _fields_ = [    ("number", c_int),    ("pChar", c_char_p),    ("str", CHARARRAY20),    ("iArray", INTARRAY20)        ]#load dll and get the function objectdll = cdll.LoadLibrary('hello.dll');GetStructInfo = dll.GetStructInfo;#set the return typeGetStructInfo.restype = c_char_p;#set the argtypesGetStructInfo.argtypes = [POINTER(StructTest)];objectStruct = StructTest();#invoke api GetStructInforetStr = GetStructInfo(byref(objectStruct));#check resultprint "number: ", objectStruct.number;print "pChar: ", objectStruct.pChar;print "str: ", objectStruct.str;for i,val in enumerate(objectStruct.iArray):  print 'Array[i]: ', val;print retStr;

總結(jié):

1. 用64位的Python去加載32位的DLL會(huì)出錯(cuò)
2. 以上只是些測(cè)試程序,在編寫Python過程中盡可能的使用"try Except"來(lái)處理異常
3. 注意在Python與C DLL交互的時(shí)候字節(jié)對(duì)齊問題
4. ctypes庫(kù)的功能還有待繼續(xù)探索

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鲁山县| 邵武市| 万荣县| 金昌市| 宁明县| 双柏县| 博爱县| 名山县| 景东| 云阳县| 西乌珠穆沁旗| 吴川市| 涟源市| 应城市| 新余市| 安丘市| 绥化市| 丹凤县| 博兴县| 临夏县| 永吉县| 波密县| 泰和县| 兴文县| 犍为县| 库尔勒市| 松滋市| 铜梁县| 南乐县| 岳阳市| 肥城市| 多伦县| 寻甸| 偃师市| 邓州市| 涿州市| 绥宁县| 庆云县| 五大连池市| 台山市| 瑞安市|