Python:将COM pyunknown转换为已知数据类型

2024-05-21 08:17:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试让python应用程序做与VB应用程序相同的事情。在

应用程序通过COM API从外部设备检索图像。我使用了win32com并设法使用调度机制与comapi进行通信。在

在VB应用程序中,图像是这样存储的

pictureData = objResult.Properties('ResultImage')
myImage = AxHost.GetPictureFromIPicture(pictureData)

在我的Python应用程序中,但是在picture数据成员上,我得到pyunknown对象类型。如何从这个“未知”对象中获取图像数据?在

让我补充一下,对于字符串等其他“基本”数据,我可以在Python应用程序上看到它们。在


Tags: 数据对象图像comapi应用程序comapi调度
1条回答
网友
1楼 · 发布于 2024-05-21 08:17:19

我发现了一个可行的方法,也许并不完美,但如果有人谷歌提出这个问题,我希望它能有所帮助。在

from win32com.client import Dispatch
import pythoncom

imagedata = data.Properties.Item('ResultImage') #this is samas the VB line in the question , except I have to add 'Item' here 

#win32com does not define IPicture type (http://timgolden.me.uk/pywin32-docs/com_objects.html)
ipicture = Dispatch(imagedata.QueryInterface(pythoncom.IID_IDispatch))

import win32ui
import win32gui

#creates a PyCBitMap object
imagebitmap = win32ui.CreateBitmapFromHandle(ipicture.Handle)

#we need a DC handle to save the bitmap file for some reason...
dc_handler = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
#this overwrites any older file without asking
imagebitmap.SaveBitmapFile(dc_handler,'last_pic.bmp')

我脑子里还有几个问题:

  1. 我们只是被迫以动态方式与IPicture或任何未定义的COM(通过win32com)交互吗?有没有一种优雅的方法来扩展接口等的定义。? 我试过使用QueryInterface(Python.IID('{7BF80980-BF32-101A-8BBB-00AA00300CAB})。我从http://msdn.microsoft.com/en-us/library/windows/desktop/ms680761(v=vs.85).aspx得到了这个IID,但是在Python中我得到了一个错误,这个接口无法处理。

  2. 不知道为什么我不能使用为IPicture定义的方法,但是我可以访问这些属性? 我只是想用我真的。去拿()或ipicture.SaveAsFile文件()但这些都没用。

相关问题 更多 >