【问题标题】:Using Python to Access Methods From C# Library - Interop .dll File使用 Python 访问 C# 库中的方法 - 互操作 .dll 文件
【发布时间】:2014-12-06 15:59:20
【问题描述】:

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

在这里查看 dll 结构:

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

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

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

print mydll[1]

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

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

请记住,我必须使用纯 Python。

【问题讨论】:

  • 您是否尝试过使用 Python for .NET (Python.NET)? ctypes 无法加载 .NET 程序集。

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


【解决方案1】:

你可以使用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() 。

【讨论】:

  • 我试过这个。我收到以下错误:AttributeError: module 'clr' has no attribute 'AddReference'
  • 能否请您粘贴您尝试过的代码行,import clr 是否适用于 python shell。 . ??
猜你喜欢
  • 2011-03-20
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 2012-04-01
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 2017-12-01
相关资源
最近更新 更多