1. 程式人生 > >Python調用C的DLL(動態鏈接庫)

Python調用C的DLL(動態鏈接庫)

ctypes href cnblogs .net net 得到 tro turn python

開發環境:mingw64位,python3.6 64位

參考博客:

mingw編譯dll:

https://blog.csdn.net/liyuanbhu/article/details/42612365

python調用dll:

https://www.cnblogs.com/cnpirate/p/5939446.html

編寫 dlltest.c

//dlltest.c  
int Double(int x)  
{  
    return x * 2;  
}  

編譯為dll

gcc dlltest.c -shared -o dlltest.dll -Wl,--out-implib,dlltest.lib

得到lib和dll文件

在python中調用:

from ctypes import *
dll = cdll.LoadLibrary(DLL/dlltest.dll)
a=dll.Double(123)
print(type(a))
print(a)

輸出:

<class int>
246

Python調用C的DLL(動態鏈接庫)