September 4, 2006

Creating a Media Center AddIn: Soup to Nuts

Filed under: Uncategorized — Anthony Park @ 4:18 pm

I recently posted instructions for creating a Media Center Hosted HTML Application Setup Project for Windows Vista


I am currently working on a new AddIn for Media Center, so I thought I would post the same instructions with respect to AddIns. Setup projects are a little different for AddIns than they are for Hosted HTML Applications.

Instead of describing just the setup project, I decided to post instructions for creating a Media Center AddIn “soup to nuts” (start to finish).

This guide assumes a couple of things:

1. You have Visual Studio 2003 installed and working
2. You have installed the Windows XP Media Center SDK

Step 1: Create the AddIn


- Open Visual Studio
- Select File | New | Project
- Select “Visual Studio C# Projects” | Media Center Edition Add-In
- Type in a location, and a name for the project.



After you click OK, Visual Studio will create a project for you with a Class1.cs file in it. This class implements two interfaces: IAddInModule and IAddInEntryPoint.

- In the Solution Explorer, rename the Class1.cs file to something appropriate. In our case, MCEHelloWorld_AddIn.cs.



- Inside Class1.cs, rename the class name, and set the namespace to something meaningful.



- The IAddInEntryPoint has a single method called “Launch”. In the Launch method, let’s add code to pop up a simple dialog.



- Now that we have our code in place, build the solution.

Step 2: Secure our Assembly


Media Center will only run AddIns that have been signed. So, we need to create a secure assembly.

The first step is to generate a keypair. If you already have a keypair that you use for other projects, you can just use that one. If not, it’s easy to create one with the “Strong Name Utility” that comes with Visual Studio.

- Open the Visual Studio Command Prompt by selecting it from the Start menu (in my case: Start | All Programs | Microsoft Visual Studio .NET 2003 | Visual Studio .NET Tools | Visual Studio .NET 2003 Command Prompt)

- Change to your project folder (in my case c:\MediaCenter\MCEHelloWorld_AddIn
- Generate a public/private keypair by typing: sn –k MCEHelloWorld_AddIn.snk



The Strong Name Utility generates a filed called “MCEHelloWorld_AddIn.snk”, which contains our public/private keypair.

Next, we need to add our keypair to the project.

- In Visual Studio, open the AssemblyInfo.cs file that is in the Solution Explorer.
- Change the AssemblyTitle to MCEHelloWorld_AddIn
- Change the AssemblyVersion to 1.0.0.1
- Change the AssemblyKeyFile to “..\\..\\MCEHelloWorld_AddIn.snk”



- Now that the keypair is hooked up, build the project again.

Step 3: Build Setup Project


Now that we have an awesome AddIn that we want to run inside of Media Center, we need a Setup project to install the assembly in the Global Assembly Cache (GAC) and register it with Media Center.

Step 3A: Create a new setup project


- Right-click on your Solution in the Solution Explorer, and select Add | New Project
- Choose “Setup and Deployment Projects” | Setup Project
- Type in a location and a name for the project.



When you click OK, Visual Studio will create a new setup project within your existing Solution.

Step 3B: Setup Project Settings


- In the Solution Explorer, select the Setup project.
- In the properties window, fill in the relevant project properties. In my case, I filled in Author, Manufacturer, ProductName, Title, and Version.



Step 3C: Add Files


- Click on the “File System Editor” toolbar button in the Solution Explorer to open the File System Editor
- Right-click “File System on Target Machine”, select “Add Special Folder” | “Global Assembly Cache Folder”



- Right-click on “Global Assembly Cache Folder”, select Add | Project Output
- Select Project: MCEHelloWorld_AddIn
- Select Primary Output



Step 3D: Check for .NET Runtime Correctly


At this point, we have a setup project that installs our AddIn assembly into the GAC. Visual Studio has detected that our setup project is dependent on the .NET Framework, so it has included a Launch Condition for us to make sure the Framework is installed. Since we are using Visual Studio 2003, the Launch Condition checks for version 1.1.4322 of the .NET Framework. It is important to support multiple versions of the .NET Framework if you want your AddIn to install on newer versions of Media Center (Windows Vista, for example). Thanks to instructions provided by Aaron Stebner in a recent blog post, you can do this easily:

- In the Solution Explorer, click the “Launch Conditions Editor” toolbar button to open the Launch Conditions Editor

- Click on the “.NET Framework” Launch Condition, and modify the “SupportedRuntimes” property to “1.1.4322;2.0.50727″ in the Property editor.



Step 4: Add Media Center Launch Condition


One of the requirements of any good setup project is that it will prevent the user from installing on a non-supported operating system. We can accomplish this task by using a launch condition.

- In the Solution Explorer, click the “Launch Conditions Editor” toolbar button to open the Launch Conditions Editor.

- Right-click on the “Search Target Machine” folder and select “Add Registry Search”.
- Name the new item something meaningful, like “Windows Media Center Required”.
- In the properties window, set the Property, RegKey, Root, and Value properties. According to Aaron Stebner, this is the correct way to check for the existence of Media Center in a setup project.



- Right-click on the “Launch Conditions” folder, and select “Add Launch Condition”.
- Name the new item something meaningful, like “Windows Media Center Required”.
- In the properties window, set the Condition, and Message properties. An Ident of “3.0″ is for Media Center 2005.





Step 5: Register your Application with Media Center


Okay, before we can complete this step, we need to generate two guids for our application. Luckily, Visual Studio has a “Create GUID” tool that will do the job.

- Select Tools | Create GUID from the Visual Studio menu.
- Select the “Registry Format” in the tool, and copy your new guid to the clipboard using the Copy button.



Okay, I said we needed two guids, right? The SDK recommends that you use sequential GUIDs to register your application to make troubleshooting and locating them in the registry easier. We’ll call our two GUIDs the “Application GUID” and the “Entry Point GUID”.

- Application GUID: {05A54682-A6B6-4dff-88BB-7A02C653DF35}
- Entry Point GUID: {05A54682-A6B6-4dff-88BB-7A02C653DF36}

Now that we have our two GUIDs, we’re ready to create the registry keys we need…

- In the Solution Explorer, click the “Registry Editor” toolbar button to open the Registry Editor.
- Right-click on the “User/Machine Hive” folder in the Registry Editor and select “New Key”. Create a structure that looks like this:



Notice how we use our “Application GUID” under the “Applications” key, and our “Entry Point GUID” under the “Background” and “Entry Points” keys?

- On each of the GUID keys, change the “DeleteAtUninstall” property to true. This will unregister the application when it is uninstalled.

- Under the Applications\[GUID] folder, create two string values called “CompanyName” and “Title”.



Under the Categories\Background\[GUID] folder, create a string value called “AppId” and a DWORD value called “TimeStamp”. Put the Application GUID in the Value property of the AppId, and put your favorite timestamp in the Value property of the TimeStamp.



- Under the Entry Points\[GUID] folder, create string values called “AddIn”, “AppId”, “Description”, “Title”, and a DWORD value called “TimeStamp”.

This is where it gets a little tricky… If you have problems with running your AddIn later, chances are that you made an error in THIS STEP.

The “AddIn” String value contains the information that Media Center needs to launch your AddIn. In my case, the string looks like this:

AnthonyPark.MediaCenter.AddIns.MCEHelloWorld_AddIn, MCEHelloWorld_AddIn, Version=1.0.0.1, Culture=neutral, PublicKeyToken=a6da5e5678fccef9, Custom=null

Notice that I have to provide the PublicKeyToken? Remember the public/private keypair that we generated earlier?

You can get the public key token by executing sn –Tp against your assembly from the Visual Studio 2003 Command Prompt.



- Put the Application GUID in the Value property of the AppId, and put your favorite timestamp in the Value property of the TimeStamp.



Step 6: Install and enjoy your Media Center AddIn!


1 Comment »

  1. Hi
    I was just wondering
    Can I create a MCE addin using C++ instead?
    Coz I only have visual C++ .net 2003 standard
    I installed the Windows XP Media Center SDK but don’t really see an option of creating MCE add-ins
    Thank you very much

    Comment by Frank — June 14, 2007 @ 12:37 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word