[Python] Using IUnknown-derived interface in python / pywin32

Discussão em 'Python' iniciado por Stack, Novembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    In short: I'm trying to call a method on in IUknown-based interface. I have some parts, but they don't connect.

    Situation:

    o1 = win32com.client.Dispatch("SomeProgId")
    o2 = o1.SubObject


    The subobject is not co-creatable, o2._ole_obj_ is a <PyIDispatch at ...>`, as expected.

    o2 also supports another interface, IMyInterface, derived form IUnknown, and I want to call a method on that interface.

    The following:

    iid = pywintypes.IID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}") // IMyInterface iid
    o2._oleobj_.QueryInterface(iid)


    fails with

    TypeError: There is no interface object registered that supports this IID


    The Queryinterface call itself succeeds (I've verified that in the source for the COM object), and if I specify an unsupported IID, the error message is different. The call fails even without trying to assing the result.


    Okay, so I set out to find out about early binding support in python. Documentation seems spotty and outdated (or am I missing something?)

    The following succeeds:

    tlb = comtypes.client.GetModule(r"path-to-dll")
    x = tlb.IMyInterface()


    Of course x is not usable, but intellisense shows the correct methods etc. for x. Generally, Intellisense shows all elements from the type library, so that part seems to work.

    I've also tried:

    myitf = win32com.client.CastTo(o2._oleobj_, 'IMyInterface')


    which fails with


    No module named 'win32com.gen_py.x0x2x4.IMyInterface' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "…\Python312-32\Lib\site-packages\win32com\client_init_.py", line 213, in CastTo mod = gencache.GetModuleForCLSID(target_clsid) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ … No module named 'win32com.gen_py.x0x2x4.IMyInterface'
    ​

    2.4 is the typelib version, so everything looks good.

    Any help?

    Continue reading...

Compartilhe esta Página