COM object VARIANT parameters in comtypes (python)

蹲街弑〆低调 提交于 2021-02-09 11:42:11

问题


I am trying to use comtypes 1.1.0 package to access COM object within python 2.7.6.1 and I have a basic problem to get correct data from COM object method due to return VARIANT type

>>> from comtypes.client import CreateObject
>>> fm1 = CreateObject("MCB.PCM")
>>> fm1.ReadVariable("dwt")
(<comtypes.automation.LP_tagVARIANT object at 0x06A541C0>,<comtypes.automation.LP_tagVARIANT object at 0x06A54210>, <comtypes.automation.LP_tagVARIANT object at 0x06A54260>, True)

How to convert VARIANT (VARIANT pointer) within python?

By OLEViewer I have

[id(0x60020002)]
VARIANT ReadVariable(
        [in] VARIANT bsVar, 
        [out, optional] VARIANT* vValue, 
        [out, optional] VARIANT* tValue, 
        [out, optional] VARIANT* bsRetMsg);

Do you have any idea to converter it and correct date from COM method?

Should I use pywin32 (win32com package) instead?

Thank you very much.

Peter


回答1:


I'm not a python expert, but handling VARIANTS as well.
This "PSEUDO" code piece might help you

from ctypes import *
from comtypes import automation 

if __name__ == '__main__':

    ...

    bsVar = automation.VARIANT("dwt")
    vValue = automation.VARIANT(0) 
    tValue = automation.VARIANT(0)
    bsRetMsg = automation.VARIANT(0)

    ReadVariable(bsVar, addressof(vValue), addressof(tValue), addressof(bsRetMsg))

    ...

PS:
Moving forward lerning how Python works, I think, but not tested, the following might work:

    ...
    vValue,tValue,bsRetMsg = ReadVariable(automation.VARIANT("dwt"))
    ...


来源:https://stackoverflow.com/questions/24345405/com-object-variant-parameters-in-comtypes-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!