parameters: (ob, target, typelib=None)
docstring: Cast' a COM object to another interface
docstring_summary: Cast' a COM object to another interface
docstring_tokens: Cast a COM object to another interface
function: def CastTo(ob, target, typelib=None):
      """'Cast' a COM object to another interface"""
      # todo - should support target being an IID
      mod = None
      if (
          typelib is not None
      ):  # caller specified target typelib (TypelibSpec). See e.g. selecttlb.EnumTlbs().
          mod = gencache.MakeModuleForTypelib(
              typelib.clsid, typelib.lcid, int(typelib.major, 16), int(typelib.minor, 16)
          )
          if not hasattr(mod, target):
              raise ValueError(
                  "The interface name '%s' does not appear in the "
                  "specified library %r" % (target, typelib.ver_desc)
              )
  
      elif hasattr(target, "index"):  # string like
          # for now, we assume makepy for this to work.
          if "CLSID" not in ob.__class__.__dict__:
              # Eeek - no makepy support - try and build it.
              ob = gencache.EnsureDispatch(ob)
          if "CLSID" not in ob.__class__.__dict__:
              raise ValueError("Must be a makepy-able object for this to work")
          clsid = ob.CLSID
          # Lots of hoops to support "demand-build" - ie, generating
          # code for an interface first time it is used.  We assume the
          # interface name exists in the same library as the object.
          # This is generally the case - only referenced typelibs may be
          # a problem, and we can handle that later.  Maybe <wink>
          # So get the generated module for the library itself, then
          # find the interface CLSID there.
          mod = gencache.GetModuleForCLSID(clsid)
          # Get the 'root' module.
          mod = gencache.GetModuleForTypelib(
              mod.CLSID, mod.LCID, mod.MajorVersion, mod.MinorVersion
          )
          # Find the CLSID of the target
          target_clsid = mod.NamesToIIDMap.get(target)
          if target_clsid is None:
              raise ValueError(
                  "The interface name '%s' does not appear in the "
                  "same library as object '%r'" % (target, ob)
              )
          mod = gencache.GetModuleForCLSID(target_clsid)
      if mod is not None:
          target_class = getattr(mod, target)
          # resolve coclass to interface
          target_class = getattr(target_class, "default_interface", target_class)
          return target_class(ob)  # auto QI magic happens
      raise ValueError
function_tokens: def CastTo ( ob , target , typelib = None ) : # todo - should support target being an IID mod = None if ( typelib is not None ) : # caller specified target typelib (TypelibSpec). See e.g. selecttlb.EnumTlbs(). mod = gencache . MakeModuleForTypelib ( typelib . clsid , typelib . lcid , int ( typelib . major , 16 ) , int ( typelib . minor , 16 ) ) if not hasattr ( mod , target ) : raise ValueError ( "The interface name '%s' does not appear in the " "specified library %r" % ( target , typelib . ver_desc ) ) elif hasattr ( target , "index" ) : # string like # for now, we assume makepy for this to work. if "CLSID" not in ob . __class__ . __dict__ : # Eeek - no makepy support - try and build it. ob = gencache . EnsureDispatch ( ob ) if "CLSID" not in ob . __class__ . __dict__ : raise ValueError ( "Must be a makepy-able object for this to work" ) clsid = ob . CLSID # Lots of hoops to support "demand-build" - ie, generating # code for an interface first time it is used.  We assume the # interface name exists in the same library as the object. # This is generally the case - only referenced typelibs may be # a problem, and we can handle that later.  Maybe <wink> # So get the generated module for the library itself, then # find the interface CLSID there. mod = gencache . GetModuleForCLSID ( clsid ) # Get the 'root' module. mod = gencache . GetModuleForTypelib ( mod . CLSID , mod . LCID , mod . MajorVersion , mod . MinorVersion ) # Find the CLSID of the target target_clsid = mod . NamesToIIDMap . get ( target ) if target_clsid is None : raise ValueError ( "The interface name '%s' does not appear in the " "same library as object '%r'" % ( target , ob ) ) mod = gencache . GetModuleForCLSID ( target_clsid ) if mod is not None : target_class = getattr ( mod , target ) # resolve coclass to interface target_class = getattr ( target_class , "default_interface" , target_class ) return target_class ( ob ) # auto QI magic happens raise ValueError