The Running instance of Windows Media Player

Filed under Media, MP3s, Office, Utilities

I’m finishing up my little Signature Enhancement Utility for Outlook and had finally gotten the Media Center 12 “Currently listening to” functions operational (This is just a minor feature I’ve seen popular on website blogs and forums, where the tag line contains not only the author’s name but what they are currently listening to, if anything, nifty and fun, but not in the least practical).

I figured I’d go ahead and try to support Windows Media Player 11 (and hopefully earlier versions) as well.

Basically, the idea is to grab a reference to the running instance, interrogate it as to the “playing” state and, if it’s playing or paused, retrieve the name, album, artist, etc info on the playing track and make it available as replaceable fields in the signature.

With Media Center, it was almost trivial:

Set omc = GetObject(, "MediaJukebox Application")
If not omc is nothing Then
   '---- it's running
   ' if it's not running, they can't be playing any music
   With omc
   Select Case .GetPlayback.State
      Case PLAYSTATE_PAUSED, PLAYSTATE_PLAYING
      '---- Media center info is available
      ps = .GetCurPlaylist.Position
      CurTrackTitle$ = .GetCurPlaylist.GetFile(ps).Name
      etc...

Obviously, if the GETOBJECT fails to return anything, Media Center isn’t currently running so the user can’t be listening to anything.

Three hours of Googling later, plus tons of experimentation and I’m not even an inch closer to getting this working for Media Player.

Using ROTView(the Running Object Table viewer, comes with various installations of Visual Studio), it does appear that WMP registers “something” with the ROT, which I’d think would be accessible by VB’s GetObject().

Alas, “Windows Media Player”, “WindowsMediaPlayer”, “MediaPlayer.MediaPlayer”, and on and on, all came up empty.

I scoured the registry for anything that even remotely looked like the moniker of a WMP registration with the ROT and everything I tried also came up empty. I’m sure it’s another case of knowing the magic password, but so far, it appears to be a tad more involved than Speak, friend, and enter.

So, for now, looks like I’ll have to rely on the FunPack for support of a limited set of attributes of the currently playing song in WMP. Apparently, for ITunes, you can use this plugin to accomplish the same thing, though I don’t use ITunes and probably won’t bother with testing that.

If anyone’s ever had any success with accessing the running instance of Media Player, I’d love to hear about it!

13 Comments

  1. Ralf says:

    Perhaps a bit brute-forcey, but have you tried invoking the ActiveX interface (AxWindowsMediaPlayer) then watching what gets accessed in the registry? Eventually you might spot the "right" object reference go whizzing by… RegMon has saved my butt a few times.

  2. Darin says:

    Hi Ralf
    Thanks for the comment. I believe I understand what you’re suggesting, but doing so creates a "new instance" of the media player control, which runs completely separately from the already loaded, playing, instance. So it doesn’t appear that that approach would work. Out of curiosity, I tried RegMon on it as well, but didn’t see anything that popped out at me. I’d already found what "looked" like ROT entries in the registry for Media Player, but using GETOBJECT on them proved fruitless.
    Ah well, I can’t stand Windows Media Player anyway. Maybe this isn’t a bad thing:-)

  3. Ralf says:

    Agreed, why support a product you loathe? πŸ™‚

    My suggestion regarding AxWindowsMediaPlayer was to invoke a TEST instance of the thing, just to see what resources it gathered from the registery, not to incorporate it into the final compile.

    Bottom line I think you got my point, but if RegMon revealed nothing then what Dark Artes is Redmond using to harness this evil we call MP11? I thought *everything* left a class name footprint in the registry. Perhaps everything is obscurified because of DRM? Maybe they don’t want developers harnessing a running instance of Media Player since it might lead to a bypass of copy protection?

    What’s ProcessExplorer say? Is anything enumerated there?

    As a tangental observation, am I the only one who’s pissed that Office 2007 no longer returns a usable Task ID when invoked via ShellExecute? Used to be I’d get a nice handle to the process which would be valid until Word or Excel exited. Now I get a bogus Task ID that’s probably owned by a loader process, which invokes Word then departs. No way to get the true handle to Word short of all the GetObject and window enumeration tricks we used to resort to back in Windows95 days — without any guarantee you’re getting the One True Instance. Grrrrrrr….

    Any thoughts on this?

  4. Darin says:

    I didn’t see anything particularly useful in ProcessExplorer. The prob is, it doesn’t look like WMP registers anything useful in the ROT. But I’ll just wait and see on that.

    About Word and it’s task ID, I didn’t see any probs with 2007. I wrote a little VB app to SHELL to WINWORD and print the return value (which is the ProcessID),

    Then I used Process Explorer to check on all loaded processes and WINWORD was still loaded with the same process id. The way I understand it, TaskID and ProcessID is used interchangeably.

    What you might be seeing is the effect of already having a loaded instance.

    Try this, load Word2007, then run a vb app that does Debug.print Shell("pathfilenameof winword.exe")

    You should see that what VB prints out is a ProcessID that is different from that shown in TaskMan, and yet there’s only one instance of WinWord

    This is because WinWord normally runs as a single instance app, so if you’re already running an instance and you shell (or dbl click on the exe) to run a new instance, the WinWord.exe DOES actually load for a split second, but it immediately sees that it’s already running and it just hands off any command line parameters to the already loaded instance.

    On the other hand, if you use CREATEOBJECT("Word.Application"), you’ll actually see multiple instances of WINWORD.EXE in taskman.

    I don’t recommend doing it this way, though, because multiple simultaneously loaded instances of Word is known to cause quite a few problems.

    Better to try GETOBJECT(, "Word.Application") and if that fails, THEN use CreateObject.

    Neither Excel nor PPT have those problems, BTW.

  5. Ralf says:

    Good information, thanks. I think you’re spot-on as to why I’m having difficulties. What drove me nuts was inconsistent behavior: sometimes I’d get a useful task ID, other times not. I bet the times it failed was when I had my worklog.doc file open. πŸ™‚

    I used to rely on GetObject(,) quite a bit, but users complained about my app hijacking their resume or shopping list so I stopped doing that. I usally invoke a single early-bound instance of Word.Application and reuse that as much as possible.

    Also, for simple printing & previewing of Word docs, I like the TX Text editor control. It’s not free, but does a nice job of rendering even complex Word documents accurately, and at warp speed. TX can load a .doc file, manipulate it via code, print it, then close before Word.Application has finished substantiating. Yes, it’s that fast.

  6. Ralf says:

    BTW, Raymond Chen has an amusing thread going about awful .sigs:
    http://blogs.msdn.com/oldnewthing/archive/2007/06/06/3106089.aspx#comments

  7. Darin says:

    Good stuff.

    I love this guy’s old sig.

    http://www.everything2.com/index.pl?node_id=650826

    Reminds me of my hacking days and the CDC (Cult of the Dead Cow, if that means anything to you, you’re REALLY old, like me<g>)

  8. Ralf says:

    "Moo! I’m coming to eat you!"

    Or was that the cult of the carniverous cow? I forget. So many hackers, so little time.

    I hung around with some crackers & hackers in the 80s and 90s, but aside from conversation never did anything interesting, which looking back 20 years later is a good thing. A clean record is a must in our era of Homeland Security and paranoia. If there’s a file maintained on me someplace it’s probably stamped "MOSTLY HARMLESS".

    My favorite thing was subverting Commodore 64 copy protection. The 1541 disk drive had its own 6502-class processor and enough RAM to load small utilities, so I’d spend my spare time watching copy-protection code go whizzing by and patching it — in real time. When I found a combination of patches that worked, I’d write a loader and bring it to our weekly Copyholics Anonymous meeting. It’s *possible* some of my stuff made it beyond that small group, but I doubt it. Compuserve was the big dog on the street and wasn’t too friendly to file attachments. πŸ™‚

    A couple of years ago I grew wistful for the Good Old Days and downloaded a Commodore 64 emulator. Instead of happy fun memory time I was instantly disoriented; even the keyboard layout was alien. I poked around with it for a couple of hours and came to the conclusion we were all crazy.

    Man, the Good Old Days sucked!

  9. yn says:

    WMPlayer.OCX works fine.

  10. IanN says:

    Heya
    I have written/found this script on the idea it goes in my start up folder. When the computer is turned on it starts windows media player playing my playlist called XPS. As starting media player in this negates any commands lines to minimize it this script would fill in, after waiting 5 seconds and assuming the media player is open it then minimizes it.
    set shell = Wscript.createobject(“wscript.Shell”)
    shell.run “wmplayer /playlist xps”, 7, False
    wscript.Sleep 5000
    success = shell.appactivate(“Windows Media Player”)
    if success then shell.sendkeys “% n”

    Now i have tested this script and it works for stuff like mspaint, notepad and calculator. I think the problem lies within “success = shell.appactivate(“Windows Media Player”)” as in Windows media player is not the correct “title name” if u will but i got no idea what to put in its place.

    After searching the internet for a while i did come across something called “API” which is meant to allowed u to capture windows would this be any use to me??

    *Extra Info i don’t have any programming software this was all done with a notepad and naming it .vbs πŸ™‚

    *Extra Extra If you can find the process id of media player you can activate it that way. This was suggested by a user on this site but i donno how i would find the process ID.

    Any help would be nice “i am not massivly computer savie” )

  11. admin says:

    @IanN

    I usually use Spy++ (comes with Visual Studio) to ferret out window classes, names, etc). If you don’t have VS, try googling “Windows Spy Utility”. There are tons of them

    For instance, here’s one that’s written in Managed code (.NET)

    http://msdn.microsoft.com/en-us/magazine/cc163617.aspx

    Most likely, “Windows Media Player” is not what is actually in the title bar of the window, so AppActivate isn’t finding it.

    • IanN says:

      i downloaded http://www.catch22.net/software/winspy and it shows the title as Windows media player, now the weird thing is this works on my dads old vista PC and Windows 7 laptop but not mine …
      is there any way u could information priovided by that software to add a little code that could be used to ID it ??

      the Proccess ID thing works, but ofc the proccess id is always different every time wmp is lauched

      • admin says:

        Yeah, you can’t really use the process ID, as that changes each time you run the program. I’ll look and see if there’s something else that might work with WMP.

Post a Reply to Darin

Your email is never published nor shared. Required fields are marked *

*
*