VB DLL在Python中使用ctypes时无法找到函数*

5 投票
2 回答
2173 浏览
提问于 2025-04-17 18:43

我在用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项目中导入这个dll,测试了一下,结果是好的:

    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
  1. 然后我把它加载到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() , MyDll.MyFunctions.AddMyValues(1,2) , MyDll.MyFunctions.AddMyValues

这里到底出什么问题了?我搞不懂。

附注:还有一个类似的未解决问题:在Python中调用vb dll

2 个回答

0

可以使用 dumpbin.exe 这个工具来查看你的 DLL 文件,使用 /exports 或者 /linkermember 这个选项,可以看到 DLL 文件中实际导出的名称。

5

你不能这样做。你生成的这个DLL文件是一个.NET程序集,或者如果你提供了一个COM接口,那它就是一个COM组件。

Python的ctypes模块只支持C语言的ABI DLLs

撰写回答