【发布时间】:2017-02-16 01:10:33
【问题描述】:
我有一个设备,它记录光谱数据并由第 3 方应用程序控制。出于自动化目的,我想使用应用程序的 COM 接口来检索 Python 中的数据。由于没有合适的文档说明从 Python 中使用 API,我从不同的 web 来源收集了以下代码,成功获取了第一帧:
comtypes.client.GetModule(('{1A762221-D8BA-11CF-AFC2-508201C10000}', 3, 11))
import comtypes.gen.WINX32Lib as WinSpecLib
win32com.client.pythoncom.CoInitialize()
doc = win32com.client.Dispatch("WinX32.DocFile")
buffer = ctypes.c_float()
frame = 1
spectrum = doc.GetFrame(frame, buffer)
但是,对GetFrame 的调用与其在Visual Basic 中的定义不一致,由制造商提供:
Sub GetFrame(frame As Integer, buffer As Variant)
GetFrame将文档中的数据复制到 Visual Basic 数组中。如果buffer是一个空的Variant,GetFrame会创建一个大小和数据类型适当的数组,并在复制数据之前将缓冲区设置为指向它。
这意味着在 Visual Basic 中,变量 buffer 填充了数据,而函数 GetFrame 没有返回值,而在 Python 中 buffer 保持不变,但函数 GetFrame 确实返回了实际数据。
如果我没有观察到我的程序随机崩溃并抛出 MemoryError 并因此在代码的这一点上表明内存泄漏,我不会关心这些微妙之处。所以我怀疑每次调用GetFrame 都会为缓冲区分配一些内存但从未释放,因为win32com 不知何故弄乱了API 包装。
这种推理将我引向了我的实际问题:我如何内省该包装器并理解它的作用?到目前为止,我找不到任何提示 win32com 生成的代码存储在任何文件中,但也许我只是没有找到正确的地方。
在 IPython 中,我也尝试使用 doc.GetFrame?? 获取信息,但它没有返回任何实现:
Signature: doc.GetFrame(frame=<PyOleMissing object at 0x06F20BC8>, FrameVariant=<PyOleMissing object at 0x06F20BC8>)
Docstring: <no docstring>
File: c:\programming\python\src\<comobject winx32.docfile>
Type: method
我还可以尝试什么来获取有关 API 包装器的更多信息?
【问题讨论】:
标签: python introspection win32com