#51 Dynamic Cast of COM Pointers

com (8)

I don't know how many other COM libraries do this, but
specifically with iTunes, the Interfaces defined for
tracks form a hierarchy (in python-speak):

class Track(Item):

class FileOrCDTrack(Track):

class URLTrack(Track):

The problem is, a number of interfaces are defined to
return a pointer to a Track interface when in fact one
can test the object and safely refer to it via a
derived interface. For instance,
iTunesApp.CurrentTrack returns a Track interface
pointer, even if it is a FileOrCDTrack. In fact, all
but 1 interface prevents the user from accessing the
derived class properties (which include a lot of new
features that are part of the base class in
AppleScript). Direct Indexing by name, ID or playlist
order all return a pointer to the base interface.
Thus, none of these allow access to the derived
attributes in FileOrCDTrack even if the track is indeed
a File or CD based track. So the ONLY way to retrieve
the derived interface is to use the Enum method of the
LibraryPlaylist object and iterate through ALL the
tracks known, which in a large library can take an
astronomical amount of time. The Enum method does
however properly return FileOrCDTrack interfaces when

The idea of the Dynamic Cast would therefore be to
allow the user to perform a C++ style dynamic_cast on
an interface to convert it to a different interface.
In this example, one would take a track instance, call
it's 'kind' property to verify that it was a
FileOrCDTrack and then cast the interface pointer from
a Track interface to a FileOrCDTrack.


  • Roger Upole

    Roger Upole - 2006-09-29

    Logged In: YES

    You should be able to do this using QueryInterface.
    The attached code takes an IITTrack object and gets
    an IITFileOrCDTrack interface from it.

  • Roger Upole

    Roger Upole - 2006-09-29
  • Stefan Schukat

    Stefan Schukat - 2006-10-04

    Logged In: YES

    from win32com.client import CastTo

    FileTrack = CastTo(iTunesApp.CurrentTrack, "FileOrCDTrack")

    this should help.

  • Roger Upole

    Roger Upole - 2006-11-18

    Logged In: YES
    Originator: NO

    Since there are already two different ways to
    accomplish this, closing as out of date.


  • Roger Upole

    Roger Upole - 2006-11-18
    • status: open --> closed-out-of-date

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks