I have been working on some debugging topics that will hopefully eventually be included in the Windows Media Center SDK for Windows Vista, and I wanted to put some of these topics together and create an end-to-end scenario demonstrating how you can use Visual Studio 2005 to debug your Windows Media Center application code as it is running within Windows Media Center.
Please note that these instructions require Visual Studio 2005 Professional Edition or higher because the lower editions of Visual Studio 2005 (such as the Express Editions) do not include a debugger that allows you to attach to running processes.
As I wrote this blog post, I tried out these steps with the Q podcast and video blog client sample application that is included in the Windows Media Center SDK. However, the same set of steps can be used for any Windows Media Center application that includes an add-in assembly.
Step 1 - Build and install the Windows Media Center application
The first step to start debugging your application is to compile your code in Visual Studio 2005, install the resultant assembly to the GAC, and use RegisterMceApp.exe or RegisterApplication to register the application so that it can be launched from within Windows Media Center.
Step 2 - Enable Windows Media Center add-in launch debugging
Set the following registry value on your system to cause a Windows Media Center dialog to appear when attempting to launch any add-in. The dialog will display the process name and process ID that you can use to attach a debugger, set breakpoints, and step through the code in your add-in that is executed when Windows Media Center attempts to load and run it.
EnableAddInLaunchDebugging = 1 (REG_DWORD)
Step 3 - Launch Windows Media Center and click on the entry point to start your add-in
After setting the EnableAddInLaunchDebugging registry value, launch Windows Media Center and click on the entry point for your application. When doing this, a dialog box like the following will appear and you can use it to attach a debugger:
Step 4 - Attach to the ehexthost process in Visual Studio 2005
After launching your application in Windows Media Cener, leave the Debug Application dialog open and launch Visual Studio 2005 Professional or higher. Click on the Tools menu and choose Attach to Process... A dialog like the following will appear:
This dialog lists all running processes on your system. Locate the process name and process ID that is listed in the Debug Application dialog in Windows Media Center, click on it to highlight it and then click the Attach button to cause the Visual Studio debugger to attach to the process.
Note that if Visual Studio 2005 was already running when you clicked on your Windows Media Center application, you may need to click the Refresh button in the Attach to Process dialog before the process you are looking for appears in the list.
Step 5 - Configure symbol settings for the add-in assembly in Visual Studio 2005
Now that you have attached to the process, you need to configure symbol settings so that you can set breakpoints and debug your add-in assembly code. Open the Modules window in Visual Studio by pressing Ctrl + Alt + U or by going to the Debug menu, choosing Windows and then choosing Modules.
When the Modules window appears, locate the DLL that represents your add-in assembly, right-click on it and choose Symbol Settings... A dialog like the following will appear:
Use the new folder icon to add a symbol file (.pdb) location. Provide the full path to the \bin\<flavor> directory for the built binary for your add-in assembly. Make sure to choose the correct <flavor> (either debug or release) depending on which flavor of the add-in assembly is currently running in Windows Media Center. After adding the symbol file location, make sure that the check box next to the symbol path is checked and then click OK to dismiss the symbol settings Options dialog.
You can verify that you chose the correct symbol location by looking in the Module window and verifying that the Symbol Status for your add-in assembly now says Symbols Loaded.
Step 6 - Configure Visual Studio option to allow setting breakpoints in managed code
Go to the Tools menu in Visual Studio 2005 and choose Options... Expand the Debugging item in the options tree and select General. Verify that the option named Enable Just My Code (Managed only) is checked. Click OK to dismiss the options dialog.
Step 7 - Set breakpoints and start debugging
Now you are ready to set some breakpoints in your source code and start debugging. Open up your source code files in the Visual Studio IDE and click on the line numbers you are interested in debugging to set breakpoints. After you have set all of the breakpoints you want, click OK on the Debug Application dialog in Windows Media Center to resume execution of your application. You should see breakpoints hit if you set them in the correct places, and you can use the Visual Studio debugger to step through your code.
Step 8 - Repeat as necessary
If you need to start a new debugging session, you can start again at step 3 of the above instructions. The process ID in the Debug Application dialog will change each time you launch your application, so you will need to make sure to attach to the new instance of ehexthost.exe in Visual Studio 2005.
Hopefully these steps will be useful to you as you develop your Windows Media Center applications in Windows Vista.