[英]How can I use a C# DLL from Python Script?
我需要使用C#編寫的dll在Python(32)中使用一個函數。 我使用ctypes,但收到錯誤消息:“找不到z函數”。 我需要的功能名稱是“ z”,庫名稱是“ ledscrcons.dll”。 我已經從另一個應用程序(C#)中檢查了它,並且該庫運行良好,但是python沒有看到它。 我不知道是什么問題?? 這是PScript的代碼:
import sys
import string
from time import gmtime, strftime
from array import *
from ctypes import *
import ctypes
import clr
def SendStr(s, port):
mydll = clr.AddReference('ledscrcons.dll')
from ledscrcons import Class1
send = mydll.z
mydll.z.argtypes = [ctypes.c_char_p, ctypes.c_int]
mydll.z.restype = c_int
st = s.encode('cp1251')
i=2
count = 0
critcnt = 1
while i!=0 and count<critcnt:
i=send(c_char_p(st), c_int(port))
if i==2 :
print(str(i) + "dd")
if i==1 :
print(str(i) + 'dd')
if i==0 :
print('t')
count = count + 1
if count==critcnt:
if i==1:
print('kk')
if i==2:
print('uu')
return i
請,任何幫助將是有用的。
我猜您使用的庫不是Com Visible。 您可以通過設置屬性使其可見:
[ComVisible(true)]
您也可以嘗試IronPython,它是Python的.net實現。 在IronPython中,只需執行
import clr
clr.AddReference('YourAssemblyName')
from YourNameSpace import YourClassName
C#-DLL不是本機可執行文件,但需要.NET-Runtime庫。 因此,您不能將它們與本機Python可執行文件一起使用。 您必須切換到IronPython,這是C#中的python實現。
您不能使用ctypes訪問.NET程序集。 我建議使用IronPython或Python .NET
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.