當前位置:首頁 » 編程語言 » python調用dll

python調用dll

發布時間: 2022-09-06 09:39:53

1. python調用dll怎麼傳入一個指針接收結果

dk 可能是調用Create_Data前開辟的一個空間,dkLen傳入該空間大小,以方便函數Create_Data寫入結果到指針指向的空間。

2. 請教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# 的動態鏈接庫,兩個動態鏈接庫就在同一個目錄下運行。

3. python調用外部C#庫的dll文件

importclr
importSystem

clr.AddReferenceToFile("SimpleHash.dll")
fromCommonimport*

classHashPy(SimpleHash):
def__init__(self):
pass

defHashCalc(self,arg1,arg2):
#strtobyte[]
arg1=System.Text.Encoding.Default.GetBytes(arg1)
arg2=System.Text.Encoding.Default.GetBytes(arg2)

returnSimpleHash.HashCalc(self,arg1,arg2)

audiobuff='
12345678
12345678
'
key='12345678'

printHashPy().HashCalc(audiobuff,key)

python ctype只能調用c/c++. 你要調用c#的dll 要用IronPython。如上面的例子

4. 如何用python調用.net中的dll

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

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

}

5. python3,64位的,怎麼樣調用32位的DLL最方便

工作流程:1.創建一個進程外COM伺服器(EXE)。2.將32位dll的介面函數封裝為COM伺服器的相關介面。3.注冊COM伺服器*.exe/regserver(注銷*.exe/unregserver)。4.64位進程調用32位COM伺服器介面,成功。從而曲線實現了64位進程調用32位dll。具體步驟:我首先創建了一個簡單的dll工程,只輸出一個函數intc=add(inta,intb);生成lib和dll然後創建一個進程外COM(EXE類型),內部鏈接dll,添加方法Method:Add(long*c){*c=add(1,2);}編譯生成。然後注冊COM,*.exe/regserver最創建一個64位WIN32工程驗證64位環境下方法調用是否正確,經驗證正確!!!結論:以上方法可以解決64位進程調用32位dll的問題32位進程調用64位dll應該也可以通過這種方法解決,原因64位windows系統下安裝了32位和64位兩套COM系統

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

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

7. 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)

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

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

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

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

(8)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) 即完成一次調用。

9. python調用dll怎麼返回多個值

多個返回值需要用list集合來解析。。
舉例參考一下:
import ctypes
# Load DLL into memory.
hllDll = ctypes.WinDLL ("c:\\PComm\\ehlapi32.dll")
# Set up prototype and parameters for the desired function call.
# HLLAPI
hllApiProto = ctypes.WINFUNCTYPE (
ctypes.c_int, # Return type.
ctypes.c_void_p, # Parameters 1 ...
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p) # ... thru 4.
hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),
# Actually map the call ("HLLAPI(...)") to a Python name.
hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)
# This is how you can actually call the DLL function.
# Set up the variables and call the Python name with them.
p1 = ctypes.c_int (1)
p2 = ctypes.c_char_p (sessionVar)
p3 = ctypes.c_int (1)
p4 = ctypes.c_int (0)
hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))

熱點內容
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:170
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:100
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:208
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554
雲伺服器的優點與缺點 發布:2024-09-08 06:30:34 瀏覽:734