繁体   English   中英

将 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM