VB dll在Python中使用ctypes不起作用(找不到函数*)

5

我在VB中努力创建一个dll,该dll将对Python可见。

当我将dll导入Python时,没有VB函数是可见的。

以下是我的做法:

  1. 最简单的VB类
Public Class MyFunctions
        Public Function AddMyValues(ByVal Value1 As Double, ByVal Value2 As Double)
            Dim Result As Double
            Result = Value1 + Value2
            Return Result
        End Function
    End Class`
  1. 我将其保存为 dll (从 Visual Studio 2010 构建)。

  2. 我尝试将其导入到其他 VB 项目中以检查是否正常工作(结果良好):

    Imports ClassLibrary1
Module Module1

    Sub Main()
        Dim Nowa As New ClassLibrary1.MyFunctions

        Dim Result As String
        Result = Nowa.AddMyValues(123, 456.78).ToString
        Console.WriteLine(Result)
        Console.ReadLine()
    End Sub

End Module
我将其加载到Python中并尝试使用:
from ctypes import *
MojaDLL = cdll.LoadLibrary("E:\\ClassLibrary1.dll")
MojaDLL.MyFunctions
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python25\lib\ctypes\__init__.py", line 361, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python25\lib\ctypes\__init__.py", line 366, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'MyFunctions' not found

我试过使用MyDll.MyFunctions()MyDll.MyFunctions.AddMyValues(1,2)MyDll.MyFunctions.AddMyValues代替MyDll.MyFunctions,但是出了什么问题我并不理解。

PS. 这里有一个类似的未解决问题:在Python中调用VB DLL


PS. 当我将dll注册为COM时,它运行良好。 - Intelligent-Infrastructure
2个回答

5
你不能这样做。你正在生成一个.NET程序集的DLL文件,或者如果你公开了一个COM接口,那么它就是一个COM组件。
Python的ctypes模块只支持C ABI DLL。

啊啊啊 :/ 那么我有没有办法在 Python 中使用 VB DLL?除了 COM 之外,似乎没有更健壮的方法(注册名称、每个名称一个 DLL、难以更新等等)。 - Intelligent-Infrastructure
@Intelligent-Infrastructure 或许有一个 Python 的 .NET 绑定,但我其实怀疑它的存在 - COM 似乎是唯一的方法。 - Konrad Rudolph

0

使用 dumpbin.exe 工具对你的 DLL 进行检查,使用 /exports 或者 /linkermember 选项来查看 DLL 中实际导出的名称。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接