Windows Vista Forums

Instantiating COM objects
  1. #1



    Newbie
    Join Date : May 2007
    Posts : 7
    Local Time: 11:58 AM

    Instantiating COM objects

    First of all I know next to nothing about COM, fiddling in Powershell is my first experience with it. Apologies if this is obvious but I can't for the life of me find the answers. I've created the following C# program based on the iTunes SDK examples, which works fine

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using iTunesLib;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
        class Program
       {
            static void Main(string[] args)
            {
                  iTunesApp app = new iTunesAppClass();
    
                  IITTrackCollection tracks = app.LibraryPlaylist.Tracks;
                  int numTracks = tracks.Count;
                  int count = 0;
    
                  while (numTracks != 0)
                  {
                        IITTrack track = tracks[numTracks];
                        IITFileOrCDTrack t = (IITFileOrCDTrack) track;
    
                        if (t.Location == null)
                       {
                              Debug.WriteLine(t.Name);
                              t.Delete();
                              count++;
                        }
                        numTracks--;                
                   }
                   Debug.WriteLine(count);
             }
        }
    }
    It simply finds all the dead links within iTunes and deletes them. How would I go about porting this to Powershell? It's creating new COM objects and casting that I'm stuck with. $track = $tracks[numTracks] won't work, "Unable to index into an object..." How do I make Powershell aware of the types such as IITTrackCollection?


      My System SpecsSystem Spec

  2. #2


    klumsy@xtra.co.nz Guest

    Re: Instantiating COM objects

    do you have the source if iTunesLib

    thats the part we need to see, i presdume its C# source code.. so
    you'd have to either do whatever it does in powershell(and if it does
    native code calling, rather than just com, then you won't be able to
    do it soley in powershell).. Alternatively you can compile that lib
    into a DLL in C#, then in powershell load that DLL and call its
    methods.


      My System SpecsSystem Spec

  3. #3



    Newbie
    Join Date : May 2007
    Posts : 7
    Local Time: 11:58 AM


      Thread Starter
    This is what I've been following, it says the type library is built into iTunes, so I don't need to use tlbimp.exe to generate a dll? In Visual Studio I just added a reference to the "iTunes 1.9 Type Library" from the COM components list.

    Last edited by Mycroft; 24 May 2007 at 10:25 AM.
      My System SpecsSystem Spec

  4. #4


    Don Jones [MVP] Guest

    Re: Instantiating COM objects

    PowerShell's not always able to "hook" into every COM object... between its
    adaptation layer and .NET's interop, some COM objects just don't work the
    right way all the time.

    Is there a strong reason to port this into PowerShell if it's working in C#?

    --
    Don Jones
    Windows PowerShell MVP
    Founder: www.ScriptingAnswers.com
    Co-Author: "Windows PowerShell: TFM"

    "Mycroft" <Mycroft.2r2jhb@no-mx.forums.net> wrote in message
    news:Mycroft.2r2jhb@no-mx.forums.net...
    >
    > First of all I know next to nothing about COM, fiddling in Powershell is
    > my first experience with it. Apologies if this is obvious but I can't
    > for the life of me find the answers. I've created the following C#
    > program based on the iTunes SDK examples, which works fine
    >
    >
    > Code:
    > --------------------
    > using System;
    > using System.Collections.Generic;
    > using System.Text;
    > using iTunesLib;
    > using System.Diagnostics;
    >
    > namespace ConsoleApplication1
    > {
    > class Program
    > {
    > static void Main(string[] args)
    > {
    > iTunesApp app = new iTunesAppClass();
    >
    > IITTrackCollection tracks = app.LibraryPlaylist.Tracks;
    > int numTracks = tracks.Count;
    > int count = 0;
    >
    > while (numTracks != 0)
    > {
    > IITTrack track = tracks[numTracks];
    > IITFileOrCDTrack t = (IITFileOrCDTrack) track;
    >
    > if (t.Location == null)
    > {
    > Debug.WriteLine(t.Name);
    > t.Delete();
    > count++;
    > }
    > numTracks--;
    > }
    > Debug.WriteLine(count);
    > }
    > }
    > }
    > --------------------
    > It simply finds all the dead links within iTunes and deletes them. How
    > would I go about porting this to Powershell? It's creating new COM
    > objects and casting that I'm stuck with. $track = $tracks[numTracks]
    > won't work, "Unable to index into an object..." How do I make Powershell
    > aware of the types such as IITTrackCollection?
    >
    >
    > --
    > Mycroft



      My System SpecsSystem Spec

  5. #5



    Newbie
    Join Date : May 2007
    Posts : 7
    Local Time: 11:58 AM


      Thread Starter
    Not especially, its more of a learning exercise.


    Using the iTunes methods that take standard paramater types such as string work fine, eg ,

    $tunes = new-object -com itunes.application
    $itunes.playFile("c:\music\song.mp3")

    Its using the iTunes specific types:

    $tracks = $tunes.libraryplaylist.tracks

    returns an IITTrackCollection, which Powershell can't seem to index into, although you can see all the tracks. iTunes seems to have a very nice C# interface, it even exposes an IEnumerable interface so you can iterate over the tracks in C#. But Powershell doesn't seem to understand the types properly, just wondered if I can get the same level of support as in C#

      My System SpecsSystem Spec

  6. #6


    klumsy@xtra.co.nz Guest

    Re: Instantiating COM objects

    $tracks = $tunes.libraryplaylist.tracks

    have you tried

    $tracks.length

    $tracks | get-member etc to see how powershell sees this object?





      My System SpecsSystem Spec

  7. #7



    Newbie
    Join Date : May 2007
    Posts : 7
    Local Time: 11:58 AM


      Thread Starter
    Yup, $tracks prints out all the tracks. $tracks.length doesn't work, it sees $tracks as a System.__ComObject
    $tracks | gm works, eventually, its a large playlist I suppose. It gives me the methods/properties of the individual $track objects contained in $tracks, of type System.__ComObject#{00d7fe99-7868-4cc7-ad9e-acfd70d09566}

    If I do

    $tracks | select -first 1

    it prints out the first one, then hangs for a while, looks like its still going over all the other tracks, just not printing them out. This is probably a dead end if it doesn't understand the types as well as C#. I'll have to leave it there I think, thankyou for the input.

      My System SpecsSystem Spec

  8. #8


    Jean Guest

    Re: Instantiating COM objects

    > returns an IITTrackCollection, which Powershell can't seem to index

    What about :

    $itunes = New-Object -com itunes.application
    $tracks=$itunes.LibraryPlaylist.Tracks
    $tracks|
    Sort-Object Index|
    %{$_|Select-Object Index,Name,Album,Artist,Composer,Time}


    To access the third element :

    $tracks.Item(3)|Select-Object Index,Name,Album,Artist,Composer,Time

    To play the third element :

    $tracks.Item(3).Play()

    Regards,

    --
    Jean - JMST
    Belgium



      My System SpecsSystem Spec

  9. #9


    Jean Guest

    Re: Instantiating COM objects

    > What about

    To iterate :

    1..$tracks.Count|%{$tracks.Item($_)}

    Regards,

    --
    Jean - JMST
    Belgium



      My System SpecsSystem Spec

  10. #10



    Newbie
    Join Date : May 2007
    Posts : 7
    Local Time: 11:58 AM


      Thread Starter
    Aha! That's very useful. Now I can just do

    $t = 1..$tracks.Count | %{$tracks.Item($_)}

    to store all the track objects in an array for faster access. That cast I thought I needed isn't even necessary. Thankyou!

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Instantiating COM objects problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
WMI objects hegde PowerShell 1 18 Aug 2009
NET objects to COM objects jazzlearner .NET General 1 15 Nov 2008
Nokia COM objects Reventlov VB Script 11 20 Aug 2008
passing whole objects ahood PowerShell 4 06 Aug 2008
Using SMO objects Carlos Felipe França da Fonseca PowerShell 0 06 Jul 2008