使用Python ctypes访问Visual Foxpro COM DLL

问题描述:

我想用python ctypes库来访问在Visual Fox Pro(从.prg文件)中创建的COM DLL中的各种函数。使用Python ctypes访问Visual Foxpro COM DLL

这里是狐亲

 
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 

在蟒我做沿东西线的例子(从实际的代码简化):

 
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一起工作吗?任何意见,将不胜感激。

尝试从调用函数中删除括号。将print link.get_input_out("someinput")更改为print link.get_input_out "someinput"

+0

嗨,我给了它去,但不幸的是python认为这个SyntaxError:无效的语法。我不认为我最初的错误是关于函数变量,而是首先找到要调用的函数。 感谢您的提供! – hygap 2012-07-23 14:50:34

对于使用OLEPUBLIC子句的VFP COM对象,应该使用python Win32扩展,而不是​​模块。 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表中,微软提供了一个免费的ADO顺应部件vfpoledb可用于通过ADODB,访问表这转可以通过Win32扩展中的相同Dispatch()功能访问。