本文以實例講解了Python調(diào)用C/C++ DLL動態(tài)鏈接庫的方法,具體示例如下:
示例一:
首先,在創(chuàng)建一個DLL工程(本例創(chuàng)建環(huán)境為VS 2005),頭文件:
//hello.h
#ifdef EXPORT_HELLO_DLL
#define HELLO_API __declspec(dllexport)
#else
#define HELLO_API __declspec(dllimport)
#endif
extern "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;
}
這里有兩個注意點:
(1)弄清楚編譯的時候函數(shù)的調(diào)用約定采用的__cdecl還是__stdcall,因為根據(jù)DLL中函數(shù)調(diào)用約定方式,Python將使用相應(yīng)的函數(shù)加載DLL。
(2)如果采用C++的工程,那么導(dǎo)出的接口需要extern "C",這樣python中才能識別導(dǎo)出的函數(shù)。
我的工程中采用__cdecl函數(shù)調(diào)用約定方式進行編譯鏈接產(chǎn)生hello.dll,然后Python中采用ctypes庫對hello.dll進行加載和函數(shù)調(diào)用:
from ctypes import *
dll = cdll.LoadLibrary('hello.dll');
ret = dll.IntAdd(2, 4);
print ret;
至此,第一個小例子已經(jīng)完成了,讀者可以自己動手嘗試一下運行效果。
示例二:
示例一只是一個"hello world"級別的程序,實際運用中更多的需要傳遞數(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 20
struct 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
#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這個函數(shù)通過傳遞一個StructTest類型的指針,然后對對象中的屬性進行賦值,最后返回"just OK".
編寫Python調(diào)用代碼如下, 首先在Python中繼承Structure構(gòu)造一個和C DLL中一致的數(shù)據(jù)結(jié)構(gòu)StructTest,然后設(shè)置函數(shù)GetStructInfo的參數(shù)類型和返回值類型,最后創(chuàng)建一個StructTest對象,并將其轉(zhuǎn)化為指針作為參數(shù),調(diào)用函數(shù)GetStrcutInfo,最后通過輸出數(shù)據(jù)結(jié)構(gòu)的值來檢查是否調(diào)用成功 :
from ctypes import *
ARRAY_NUMBER = 20;
STR_LEN = 20;
#define type
INTARRAY20 = c_int * ARRAY_NUMBER;
CHARARRAY20 = c_char * STR_LEN;
#define struct
class StructTest(Structure):
_fields_ = [
("number", c_int),
("pChar", c_char_p),
("str", CHARARRAY20),
("iArray", INTARRAY20)
]
#load dll and get the function object
dll = cdll.LoadLibrary('hello.dll');
GetStructInfo = dll.GetStructInfo;
#set the return type
GetStructInfo.restype = c_char_p;
#set the argtypes
GetStructInfo.argtypes = [POINTER(StructTest)];
objectStruct = StructTest();
#invoke api GetStructInfo
retStr = GetStructInfo(byref(objectStruct));
#check result
print "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會出錯
2.
以上只是些測試程序,在編寫Python過程中盡可能的使用"try Except"來處理異常
3.
注意在Python與C DLL交互的時候字節(jié)對齊問題
4.
ctypes庫的功能還有待繼續(xù)探索
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

