導航:首頁 > 編程語言 > python能調用dll嗎

python能調用dll嗎

發布時間:2022-08-17 12:57:32

1. 如何用python調用應用程序的.dll文件

python引用DLL文件的方法具體分析如下:
在python中調用dll文件中的介面比較簡單,如我們有一個test.dll文件,內部定義如下:
extern "C"
{
int __stdcall test( void* p, int len)
{
return len;
}
}
在python中我們可以用以下兩種方式載入
1.
import ctypes
dll = ctypes.windll.LoadLibrary( 'test.dll' )
2.
import ctypes
dll = ctypes.WinDll( 'test.dll' )
其中ctypes.windll為ctypes.WinDll類的一個對象,已經在ctypes模塊中定義好的。在test.dll中有test介面,可直接用dll調用即可
nRst = dll.test( )
print nRst
由於在test這個介面中需要傳遞兩個參數,一個是void類型的指針,它指向一個緩沖區。一個是該緩沖區的長度。因此我們要獲取到python中的字元串的指針和長度
#方法一:
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb'
pStr = ctypes.c_char_p( )
pStr.value = sBuf
pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
nRst = dll.test( pVoid, len( pStr.value) )
#方法二:
test = dll.test
test.argtypes = [ctypes.c_char_p, ctypes.c_int]
test.restypes = ctypes.c_int
nRst = test(sBuf, len(sBuf))
如果修改test.dll中介面的定義如下:
extern "C"
{
int __cdecl test( void* p, int len)
{
return len;
}
}
由於介面中定義的是cdecl格式的調用,所以在python中也需要用相應的類型
1.
import ctypes
dll = ctypes.cdll.LoadLibrary( 'test.dll' )
##註:一般在linux下為test.o文件,同樣可以使用如下的方法:
##dll =ctypes.cdll.LoadLibrary('test.o')
2.
import ctypes
dll = ctypes.CDll( 'test.dll' )

2. 有誰知道python怎麼調用c#的dll

1、首選運行工具 makepy.py。

3. 請教python調用dll動態庫的傳參問題

第一步,我先從簡單的調用出發,定義了一個簡單的函數,該函數僅僅實現一個整數加法求和: LIBEXPORT_API int mySum(int a,int b){ return a+b;} C# 導入定義: public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] public static extern int mySum (int a,int b); } 在C#中調用測試: int iSum = RefComm.mySum(,); 運行查看結果iSum為5,調用正確。第一步試驗完成,說明在C#中能夠調用自定義的動態鏈接庫函數。 第二步,我定義了字元串操作的函數(簡單起見,還是採用前面的函數名),返回結果為字元串: LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a); return a;} C# 導入定義: public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, string b); } 在C#中調用測試: string strDest=""; string strTmp= RefComm.mySum("45", strDest); 運行查看結果 strTmp 為"45",但是strDest為空。我修改動態鏈接庫實現,返回結果為串b: LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a) return b;} 修改 C# 導入定義,將串b修改為ref方式: public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, ref string b); } 在C#中再調用測試: string strDest=""; string strTmp= RefComm.mySum("45", ref strDest); 運行查看結果 strTmp 和 strDest 均不對,含不可見字元。再修改 C# 導入定義,將CharSet從Auto修改為Ansi: public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, string b); } 在C#中再調用測試: string strDest=""; string strTmp= RefComm. mySum("45", ref strDest); 運行查看結果 strTmp 為"45",但是串 strDest 沒有賦值。第二步實現函數返回串,但是在函數出口參數中沒能進行輸出。再次修改 C# 導入定義,將串b修改為引用(ref): public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, ref string b); } 運行時調用失敗,不能繼續執行。 第三步,修改動態鏈接庫實現,將b修改為雙重指針: LIBEXPORT_API char *mySum(char *a,char **b){sprintf((*b),"%s",a); return *b;} C#導入定義: public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern string mySum (string a, ref string b); } 在C#中調用測試: string strDest=""; string strTmp= RefComm. mySum("45", ref strDest); 運行查看結果 strTmp 和 strDest 均為"45",調用正確。第三步實現了函數出口參數正確輸出結果。 第四步,修改動態鏈接庫實現,實現整數參數的輸出: LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;} C#導入的定義: public class RefComm { [DllImport("LibEncrypt.dll", EntryPoint=" mySum ", CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)] public static extern int mySum (int a, int b,ref int c); } 在C#中調用測試: int c=0; int iSum= RefComm. mySum(,, ref c); 運行查看結果iSum 和c均為5,調用正確。 經過以上幾個步驟的試驗,基本掌握了如何定義動態庫函數以及如何在 C# 定義導入,有此基礎,很快我實現了變長加密函數在 C# 中的調用,至此目標實現。 三、結論 在 C# 中調用 C++ 編寫的動態鏈接庫函數,如果需要出口參數輸出,則需要使用指針,對於字元串,則需要使用雙重指針,對於 C# 的導入定義,則需要使用引用(ref)定義。 對於函數返回值,C# 導入定義和 C++ 動態庫函數聲明定義需要保持一致,否則會出現函數調用失敗。定義導入時,一定注意 CharSet 和 CallingConvention 參數,否則導致調用失敗或結果異常。運行時,動態鏈接庫放在 C# 程序的目錄下即可,我這里是一個 C# 的動態鏈接庫,兩個動態鏈接庫就在同一個目錄下運行。

4. python 調用DLL的問題

那就是有內存泄露。內存沒有釋放。原因可能是多種。一種是你的DLL或者是AntiVC出了錯。另外一種是ctypes出錯。其中返回的內容沒有釋放內存。也許只有10個位元組,但是長期積累還是很可觀。

如果解決這個問題,就是穩定的程序,怎麼用都不會壞。

還有一個保守的辦法。如果你調用DLL的頻率不是特別快。比如每秒不超過2000次。那麼你可以將這個功能封裝在一個服務進程里。線程再通過介面去訪問。該 服務進程即使內存溢出崩潰了。也會自動重新啟動。這樣你的20個線程基本上只需要重試幾次,等服務進程重新啟動完成後,就可以獲得結果。基本不影響使用。操作系統也很安全。因為進程退出後,所有的遺留錯誤都會清空。

5. 我現在想把自己寫的python模塊源代碼封裝成dll,然後在別的python腳本里調用,可以嗎

可以的,只要把python模塊轉換成dll模塊,利用Python自帶的ctypes模塊載入調用就行。

ctypes 是Python的外部函數庫。它提供了與 C語言兼容的數據類型,並允許調用 DLL 或共享庫中的函數。可使用該模塊以純 Python 形式對這些庫進行封裝。

ctypes導出了cdll對象,在 Windows 系統中還導出了windll和oledll對象用於載入動態鏈接庫。通過操作這些對象的屬性,你可以載入外部的動態鏈接庫。cdll載入按標準的cdecl調用協議導出的函數,而windll導入的庫按stdcall調用協議調用其中的函數。

(5)python能調用dll嗎擴展閱讀:

載入調用DLL的相關方法:

1、載入DLL

載入的時候要根據你將要調用的函數是符合什麼調用約定的。

stdcall調用約定:兩種載入方式

Objdll = ctypes.windll.LoadLibrary("dllpath")

Objdll = ctypes.WinDLL("dllpath")

cdecl調用約定:也有兩種載入方式

Objdll = ctypes.cdll.LoadLibrary("dllpath")

Objdll = ctypes.CDLL("dllpath")

其實windll和cdll分別是WinDLL類和CDll類的對象。

2、調用dll中的方法

載入dll的時候會返回一個DLL對象(假設名字叫Objdll),利用該對象就可以調用dll中的方法。 e.g.如果dll中有個方法名字叫Add(注意如果經過stdcall聲明的方法,如果不是用def文件聲明的導出函數或者extern 「C」 聲明的話,編譯器會對函數名進行修改,這個要注意。)

調用:nRet = Objdll.Add(12, 15) 即完成一次調用。

6. python可以調用易語言dll嗎

完全可以 dll 都可以調用 通用的

7. python 調用 C++ 編譯的 dll

困難啊。通常調用C編寫的DLL就比較難。如果C++更難。如果說在linux還過得去。那麼在linux下還得去。在windows就更難了。標准不一樣。

建議你直接使用cython,輕松就搞定了。ctypes只是偶爾才用一下。

往往調用失敗與dll的封裝格式,編譯的方法,參數,版本都有關系。c++的函數命名方式也不同於C,似乎前面要加下劃線。

8. python怎麼調用dll共享庫

可以的,python中一般有兩種方法調用DLL中的函數。1.直接使用函數名,函數名可以用dependencywalker等工具查看。(這個工具在vc或者vs的工具包中)[python]viewplainimportctypesdll=CTYPES.CDLL("test.dll")res=test(3,4)2.使用Ordinal,Ordinal可以用dependencywalker等工具查看。[python]viewplainimportctypesdll=CTYPES.CDLL("test.dll")res=dll[1](3,4)

9. python2.7可以直接import dll庫嗎

在python中調用dll文件中的介面比較簡單,實例代碼如下:
如我們有一個test.dll文件,內部定義如下:
extern "C"
{

int __stdcall test( void* p, int len)
{
return len;
}

}

在python中我們可以用以下兩種方式載入
1.
import ctypes
dll = ctypes.windll.LoadLibrary( 'test.dll' )

2.
import ctypes
dll = ctypes.WinDll( 'test.dll' )
其中ctypes.windll為ctypes.WinDll類的一個對象,已經在ctypes模塊中定義好的。在test.dll中有test介面,可直接用dll調用即可
nRst = dll.test( )
print nRst
由於在test這個介面中需要傳遞兩個參數,一個是void類型的指針,它指向一個緩沖區。一個是該緩沖區的長度。因此我們要獲取到python中的字元串的指針和長度
#方法一:
sBuf = 'aaaaaaaaaabbbbbbbbbbbbbb'
pStr = ctypes.c_char_p( )
pStr.value = sBuf
pVoid = ctypes.cast( pStr, ctypes.c_void_p ).value
nRst = dll.test( pVoid, len( pStr.value) )

#方法二:
test = dll.test
test.argtypes = [ctypes.c_char_p, ctypes.c_int]
test.restypes = ctypes.c_int
nRst = test(sBuf, len(sBuf))
如果修改test.dll中介面的定義如下:
extern "C"
{
int __cdecl test( void* p, int len)
{
return len;
}
}

由於介面中定義的是cdecl格式的調用,所以在python中也需要用相應的類型
1.
import ctypes
dll = ctypes.cdll.LoadLibrary( 'test.dll' )
##註:一般在linux下為test.o文件,同樣可以使用如下的方法:
## dll = ctypes.cdll.LoadLibrary('test.o')

2.
import ctypes
dll = ctypes.CDll( 'test.dll' )

閱讀全文

與python能調用dll嗎相關的資料

熱點內容
pythonclass使用方法 瀏覽:221
移動加密軟體去哪下載 瀏覽:281
php彈出alert 瀏覽:207
吉林文檔課件加密費用 瀏覽:131
感測器pdf下載 瀏覽:284
隨車拍app綁定什麼設備 瀏覽:896
方維團購系統源碼 瀏覽:991
linux反彈shell 瀏覽:159
列印機介面加密狗還能用嗎 瀏覽:300
二板股票源碼 瀏覽:448
度人經pdf 瀏覽:902
怎麼配置android遠程伺服器地址 瀏覽:960
java程序員看哪些書 瀏覽:943
什麼app可以免費和外國人聊天 瀏覽:797
pdf手寫筆 瀏覽:182
別永遠傷在童年pdf 瀏覽:990
愛上北斗星男友在哪個app上看 瀏覽:421
主力散戶派發源碼 瀏覽:671
linux如何修復伺服器時間 瀏覽:61
榮縣優途網約車app叫什麼 瀏覽:479