[python-win32] Casting to interface from different typelib?

Ben Gordon dbg1000 at gmail.com
Fri Jan 9 14:27:59 CET 2009


Any help here would be appreciated.  But I hope I don't kill my odds by 
having redacting the names of all the (proprietary) libraries involved.  
(Or by the fact that I'm new to COM.)

Anyhow, as part of a collaboration, I'm building a win32 application 
that uses COM to interact with several proprietary libs, and I do not 
have access to the sources.  I have VBA code (that works) that I am 
trying to port to python.

    VBA:
    ------------------------
    Dim pA  as TypelibA.InterfaceA
    Dim pB   as TypelibB.InterfaceB
    Dim othermodule as OtherModule

    'After this line, pA points to a new "thingA"
    Set othermodule as New OtherModule
    othermodule.generator "thingA", pA

    'Cast
    Set pB = pA
 
    msgbox pB.Count()
    ------------------------

The above code works just fine.  The cast ("Set pB = pA") is what seems 
to be giving me problems when converting to Python:

    Python:
    ------------------------
   
    pA = othermodule.generator("thingA")
    print pA
    #output: <win32com.gen_py.TypelibA.InterfaceA instance at 0x13123176>

    #Now, the "cast"
    mod = gencache.GetModuleForTypelib(typelibB_CLSID,0x0,1,0)
    pB = mod.InterfaceB(pA)

    #Check cast
    print pB
    #output: <win32com.gen_py.TypelibeB.InterfaceB instance at 
0x13161656>  #Looks good?

    #Test
    print pB.Count()
    #Output: pywintypes.com_error: (-2147319765, 'Element not found.', 
None, None)

I have also tried different methods for casting.  Most of the others 
(say, by using the IID directly) give me __repr__'s with nonsensical 
mixtures, such as "TypelibA.interfaceB", even though interfaceB is only 
defined in TypelibB.  "CastTo" fails because "The interface does not 
appear in the same library as the object."

All casting approaches result in the same "Element not found" error.  
All I know about InterfaceB is that it is probably a Collection or 
derived from a Collection.  I've read somewhere about issues with 
"dependent typelibs."  Is this what I'm coming up against?  Any ideas?

Many thanks,

  -Ben

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20090109/1c26d990/attachment.htm>


More information about the python-win32 mailing list