如何使用Python脚本中的C#DLL?

如何使用Python脚本中的C#DLL?,c#,python,C#,Python,我需要使用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

我需要使用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
导入系统 导入字符串 从时间导入gmtime,strftime 从数组导入* 从ctypes导入* 导入ctypes 导入clr def SendStr(s,端口): mydll=clr.AddReference('ledscrcons.dll') 从ledscrcons进口类别1 send=mydll.z mydll.z.argtypes=[ctypes.c_char_p,ctypes.c_int] mydll.z.restype=c_int st=s.encode('cp1251') i=2 计数=0 critcnt=1
而我=0和count我猜您使用的库不可见。您可以通过设置属性使其Com可见:

[ComVisible(true)]
您还可以尝试IronPython,它是Python的.net实现。在IronPython中,只需执行以下操作

import clr
clr.AddReference('YourAssemblyName')
from YourNameSpace import YourClassName

C#-DLL不是本机可执行文件,但需要.NET运行时库。因此,不能将它们用于本机Python可执行文件。您必须切换到IronPython,这是C#中的python实现。

您不能使用ctypes访问.NET程序集。
我建议使用或

您应该包括您试图调用的、等的可能重复的C#函数声明。我已经下载了ironP,现在我的代码中有错误,请帮助我将此代码从py32重写到IPython好吗?第行中的错误:send=mydll.z(我已经编辑了代码)我猜这是因为脚本找不到.dll。尝试将该.dll的路径添加到sys.path。选中此[链接]此外,导入类后,可以直接使用该函数。查看详细信息。