我正在尝试使用 python ctypes 库来访问在 Visual Fox Pro 中创建的 COM DLL 中的各种函数(来自 .prg 文件)。
这里是fox pro中的示例(根据实际代码进行了简化)
DEFINE CLASS Testing AS CUSTOM OLEPUBLIC PROCEDURE INIT ON ERROR SET CONSOLE OFF SET NOTIFY OFF SET SAFETY OFF SET TALK OFF SET NOTIFY OFF ENDPROC FUNCTION get_input_out(input AS STRING) AS STRING output = input RETURN output ENDFUNC ENDDEFINE
在 python 中,我正在做一些事情:
import ctypes link = ctypes.WinDLL("path\to\com.dll") print link.get_input_out("someinput")
dll 注册正常并已加载,但当我尝试调用该函数时,我只是得到以下信息。
AttributeError: function 'get_input_out' not found
我可以验证 dll 是否正常工作,因为我能够使用 COM 库通过 php 脚本访问这些函数。
我真的很想让这个在 python 中工作,但到目前为止我的尝试都是徒劳的,ctypes 甚至可以与 VFP 一起工作吗?任何建议将不胜感激。
最佳答案
对于使用 OLEPUBLIC 子句的 VFP COM 对象,您应该使用 python Win32 扩展,而不是 ctypes
模块。 Python Win32 扩展提供了一组功能齐全的组件,允许 Python 成为 COM 客户端,这正是本例中您想要的。
您的代码应如下所示:
from win32com.client import Dispatch
oFox = Dispatch("com.testing")
# where the file name compiled by VFP is com.dll and the olepublic class is testing.
# in Windows this stuff is not case sensitive.
print oFox.get_input_out("something")
# to close things down..
oFox = None
如果您只想访问 VFP 表,Microsoft 提供了一个免费的 ADO 兼容组件 vfpoledb
,它可用于通过 ADODB 访问表,而 ADODB 又可以通过相同的 访问表Win32 扩展中的 >Dispatch()
功能。
关于python - 使用 Python ctypes 访问 Visual Foxpro COM DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612663/