将 comtypes 移植到 win32com

Porting comtypes to win32com

在 Python 脚本中,我必须用 win32com 和 pythoncom 替换 comtypes 的使用。基本上我有这个代码:

from comtypes.client import CreateObject, GetEvents

object_IXXObjManager = comtypes.client.CreateObject(xxxId)
connection_IXXObjManager = GetEvents(object_IXXObjManager, IXXObjManagerEventHandler())

object_IXXObjCommunications = object_IXXObjManager.QueryInterface(comtypes.gen.XXOBJLib.IXXObjCommunications)
connection_IXXObjCommunications = GetEvents(object_IXXObjCommunications, IXXObjCommunicationEventHandler(), interface=comtypes.gen.XXOBJLib.IXXObjCommunicationsEvents)

目标是使用 win32com 获得类似的功能。据我了解,事件处理程序 类 不需要更改。 第一部分很简单:

import win32com.client
object_IXXObjManager = win32com.client.Dispatch(xxxId)
event_IXXObjManager = win32com.client.WithEvents(object_IXXObjManager, IXXObjManagerEventHandler) 

但是,我在尝试将事件处理程序映射到查询接口中的对象时卡住了。

object_IXXObjManager._oleobj_.QueryInterface( ??? )

你能帮帮我吗?我有一般的 sw 开发经验,但是 COM 知识有限。

替代

object_IXXObjCommunications = object_IXXObjManager.QueryInterface(comtypes.gen.XXOBJLib.IXXObjCommunications)

可以这样:

iface = object_XXObjManager._oleobj_.QueryInterface(pythoncom.IID_IDispatch)
iface_Communications = win32com.client.CastTo(iface,"XXObjCommunications")
connection_XXObjCommunications = win32com.client.Dispatch(iface_Communications)