使用Python从C#库访问方法 - Interop .dll文件

时间:2014-12-06 15:59:20

标签: c# python .net dll python.net

我有一个.dll文件(带有“Interop。”前缀),其中包含用C#编写的库。在库中有一个类,几个枚举,几个接口和几个delgates。 (通过使用JetBrains dotPeek反编译.dll来观察)

在这里查看dll结构: DllStructure

我需要使用纯Python来访问类中的方法。我试过了:

from ctypes import *
name = "Interop.HTBRAILLEDRIVERSERVERLib.dll"
mydll = cdll.LoadLibrary(name)

但是,尝试调用类“HtBrailleDriverClass”中包含的任何方法会导致“找不到AttributeError:function'initialize'”。我还试图从它们的序数索引中访问它们:

print mydll[1]

但是这会产生错误“AttributeError:function ordinal 1 not found”。

是否有人能够阐明为什么我无法访问此.dll中的类以及为什么我无法访问任何方法?

请记住我必须使用纯Python。

1 个答案:

答案 0 :(得分:2)

您可以使用python.net访问它

import clr,sys
sys.path.append("path of your dll")
clr.AddReference("YourDllName")
import YourDllName

然后尝试打印班级成员的任何值,如

print YourDllName.ClassName.Member

来自你的python脚本

注意:您需要将clr.pyd和python.runtime.dll放入

Python27 / Dlls文件夹

如果您不想附加您的dll路径,请将dll放在python2.x / Lib / site-packages文件夹中。 那么你也可以避免第二行 - sys.path.append()。

相关问题