日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

python什么模块动态调用链接库_Python调用C/C++动态链接库的方法详解

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python什么模块动态调用链接库_Python调用C/C++动态链接库的方法详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文以實例講解了Python調用C/C++ DLL動態鏈接庫的方法,具體示例如下:

示例一:

首先,在創建一個DLL工程(本例創建環境為VS 2005),頭文件:

1

2

3

4

5

6

7

8

9

10

//hello.h

#ifdef EXPORT_HELLO_DLL

#define HELLO_API __declspec(dllexport)

#else

#define HELLO_API __declspec(dllimport)

#endif

extern "C"

{

HELLO_APIint IntAdd(int ,int);

}

CPP文件:

1

2

3

4

5

6

7

//hello.cpp

#define EXPORT_HELLO_DLL

#include "hello.h"

HELLO_APIint IntAdd(int a,int b)

{

return a + b;

}

這里有兩個注意點:

(1)弄清楚編譯的時候函數的調用約定采用的__cdecl還是__stdcall,因為根據DLL中函數調用約定方式,Python將使用相應的函數加載DLL。

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

我的工程中采用__cdecl函數調用約定方式進行編譯鏈接產生hello.dll,然后Python中采用ctypes庫對hello.dll進行加載和函數調用:

1

2

3

4

from ctypes import *

dll = cdll.LoadLibrary('hello.dll');

ret = dll.IntAdd(2, 4);

print ret;

至此,第一個小例子已經完成了,讀者可以自己動手嘗試一下運行效果。

示例二:

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//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_APIchar* GetStructInfo(struct StructTest* pStruct);

}

CPP文件如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

//hello.cpp

#include

#define EXPORT_HELLO_DLL

#include "hello.h"

HELLO_APIchar* 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這個函數通過傳遞一個StructTest類型的指針,然后對對象中的屬性進行賦值,最后返回"just OK".

編寫Python調用代碼如下,首先在Python中繼承Structure構造一個和C DLL中一致的數據結構StructTest,然后設置函數GetStructInfo的參數類型和返回值類型,最后創建一個StructTest對象,并將其轉化為指針作為參數,調用函數GetStrcutInfo,最后通過輸出數據結構的值來檢查是否調用成功:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

from ctypesimport *

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,valin enumerate(objectStruct.iArray):

print 'Array[i]: ', val;

print retStr;

總結:

1. 用64位的Python去加載32位的DLL會出錯

2. 以上只是些測試程序,在編寫Python過程中盡可能的使用"try Except"來處理異常

3. 注意在Python與C DLL交互的時候字節對齊問題

4. ctypes庫的功能還有待繼續探索

總結

以上是生活随笔為你收集整理的python什么模块动态调用链接库_Python调用C/C++动态链接库的方法详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。