ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


Building .NET Add-Ins for Windows Media Center Edition

by Michael Earls
04/05/2005

Introduction

Windows XP Media Center Edition 2005 is an exciting platform for enjoying all of your media from the comfort of your sofa. However, there are many times that you might wish to extend Media Center to perform functionality that it does not have "out of the box." Microsoft has created a software development kit that allows you to write your own software that runs in Media Center. In a previous article, I gave you a broad-brush introduction to creating an HTML application that runs in the 10-foot experience of Windows Media Center. This article will introduce you to the creation of .NET add-ins that are hosted within Media Center. The .NET add-ins allow you to write functional code that can interact with Media Center using your favorite .NET language.

There are two types of .NET add-ins for Media Center:

Getting Started

One of the first things you need to know about creating add-ins for Windows XP Media Center Addition 2005 is that they must be written and compiled in .NET 1.0. This is due to the fact that the add-in is hosted by the Media Center process, and that's the only version that Microsoft supports at the present time. This causes many developers a little discomfort, because they are accustomed to working with Visual Studio 2003, which supports only .NET 1.1. However, you can continue to use your existing editor of choice to write the code and then use the .NET compiler on the Media Center computer to compile it. That is the approach that I'll take in this article so that you can get a quick feel for developing an add-in for Media Center without having to install an older version of Visual Studio.

Before we get started, make sure you've downloaded and installed the Media Center Software Developer Kit (SDK).

Related Reading

Home Hacking Projects for Geeks
By Eric Faulkner, Tony Northrup

Required Interfaces

In order to allow your class to interact with Media Center, you need to be familiar with two interfaces:

Sample Code--Hello, World

Let's create a class that will implement the required interfaces and show users a dialog when they run it. It's important that you not pop your own dialogs or create your own windows from your add-in. All interaction with the user should be done through the provided interface. Media Center provides us with a dialog that we can use. Create a directory on the Media Center machine to contain your source code. Then, save the code below to a file named HelloWorld.cs:

using System;
using System.Collections;
using Microsoft.MediaCenter.AddIn;

namespace Sample
{
  public class HelloWorld : MarshalByRefObject, 
                            IAddInModule, 
                            IAddInEntryPoint
  {
    private AddInHost mcHost;

    void IAddInModule.Initialize(
                           IDictionary dictAppInfo,
                           IDictionary dictEntryPoint)
    {
    }

    void IAddInModule.Uninitialize()
    {
    }

    void IAddInEntryPoint.Launch(AddInHost host)
    {
      mcHost = host;
      mcHost.HostControl.Dialog("Hello, World", 
                                "Greetings",
                                1,10,false);
    }
  }
}

Notice that the Launch method of the IAddInEntryPoint handed us a reference to an instance of the AddInHost. This is our doorway to the functionality provided by Media Center. Everything you'll ever need to do with Media Center is in that AddInHost instance. We simply asked the HostControl instance to pop a dialog for us.

We need to tell the compiler a little bit about our class, so create a text file named AssemblyInfo.cs, paste the following code into it, and save it in the same folder with the HelloWorld.cs file:


using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("Media Center Hello World")]
[assembly: AssemblyDescription("Hello, World")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("1.0.0")]

Building and Registering your Add-In

Now that we've got the code written, we'll need to compile it. We'll also need to register the compiled DLL with Media Center so it knows that it exists. We're going to use the compiler from the .NET directory on the Media Center to compile our code. Create another text file and paste the following text into it:

Echo Off
Echo Building Add-in...
c:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\csc.exe /target:library /reference:C:\WINDOWS\eHome\Microsoft.MediaCenter.dll HelloWorld.cs AssemblyInfo.cs
Echo copying file to eHome directory...
copy HelloWorld.dll C:\WINDOWS\eHome\HelloWorld.dll
Echo File Copied to eHome directory.
pause

Save the file to the same folder you just created, and name it make.cmd. When you double-click on this file, it will build the code and copy the resulting DLL to the directory that contains Media Center applications.

Registering the Class as a Background Add-In

Now that we've built the DLL and copied it to the correct folder, we'll need to register it with Media Center. We do this by creating an XML file containing the registration settings that Media Center needs and then calling RegisterMCEApp.exe (included with Media Center).

Note: The best way to register an add-in is to create a Windows Installer file. See the online SDK documentation for more details.

Create a new text file in the folder that you created above. Paste the following XML into the new text file:

<application title="Sample Add-In" 
    id="{CEA2344E-31EF-4136-A865-37373AD3CED3}">
  <entrypoint 
      id="{CEA2344E-31EF-4136-A865-37373AD3CED3}"
      addin="Sample.HelloWorld, HelloWorld"
      title="Hello, World"
      description=".NET Add-in Sample">
    <category category="Background" />
  </entrypoint>
</application>

Save the file as HelloWorld.mcl.

Open the make.cmd file from above and add the following text at the end of the file:

Echo Registering application with Media Center...
C:\WINDOWS\eHome\RegisterMCEApp.exe "c:\SAMPLE_FOLDER_PATH\HelloWorld.mcl"
pause

Change the SAMPLE_FOLDER_PATH to the correct path, save the file, and then double-click on it to compile, copy, and register it with Media Center.

Start Media Center by pressing the Start button on your remote control. You should see the dialog pop up with the message "Hello, World" (Figure 1). Congratulations; you've just created your first add-in. Close the Media Center application to continue.

Figure 1
Figure 1: Your first add-in

Notice how our add-in executed as soon as we started Media Center. It did this because we set it up as a background application in the .mcl file above by adding the following category information:

<category category="Background"/>

Registering a Class as an On-Demand Add-In

How would we make an on-demand add-in? It's simple. All we need to do is tell Media Center how we want it to be executed. We do this by using one of the 16categories defined in the Media Center SDK:

For our sample, we'll register our add-in to handle requests from More With This\Audio\Album. This means that our add-in will run when we press the More Information button on the Media Center remote control and select More from the resulting menu. Let's add some code to our add-in that will display the album artist in a dialog box when we do this. Open the HelloWorld.cs file and replace the original dialog code in the IAddInEntryPoint.Launch method with the following line of code:

mcHost.HostControl.Dialog("Artist: " + 
	  mcHost.HostControl.MediaContext["AlbumArtist"], 
    "Artist Info", 1, 10, false);

Open the HelloWorld.mcl file in a text editor and replace the <category> element with the following XML text:

<category category="More With This\Audio\Album"/>

Save the file. Before we build and register our add-in, let's ensure that our add-in is unregistered before it is built. This will ensure a clean registration each time we build it. Open the make.cmd file in a text editor and add the following text to the top of the file (just after the Echo Off line):

Echo Unregistering application from Media Center
C:\WINDOWS\eHome\RegisterMCEApp.exe /u "c:\SAMPLE_FOLDER_PATH\HelloWorld.mcl"

Save make.cmd, and then run it. Once your add-in has been built and registered, start up Media Center. Navigate to My Music and click on the OK button. Once the Albums list has loaded, navigate to one of the albums in your collection then press the More button on your Media Center remote control (Figure 2).

Figure 2
Figure 2. Using your new add-in

Select More, and click on OK (Figure 3).

Figure 3
Figure 3. Using your new add-in

Select Hello, World from the list and click on OK. You should see a dialog that shows the name of the album (Figure 4).

Figure 1
Figure 4. Using your new add-in

You have now created an on-demand add-in that interacts with the Media Center context.

Conclusion

This article took you through a very basic scenario to introduce you to the steps involved with creating your own add-ins for Media Center using .NET. There is an entire world of possibility out there for you to explore from within Media Center. The following resources will help you in your quest to create that next killer application for Windows XP Media Center Edition 2005:

Michael Earls is a Microsoft technology solutions consultant working for Magenic Technologies.


Return to ONDotnet.com.

Copyright © 2009 O'Reilly Media, Inc.