Mailbag: How to launch an EXE from within Windows Media Center

Question:

I have written an executable that I want to be able to run as a Windows Media Center application and as a standalone program outside of Media Center.  I am able to register the executable with Media Center using RegisterMceApp or RegisterApplication and it can be launched from within Media Center.  When I launch my executable from within Media Center, I see that Media Center automatically minimizes to get out of the way of the program when launching it.  However, when I close my program, Media Center does not restore, and the user has to navigate back to Media Center on their own (which is difficult with only a remote control and/or via a Media Center extender session).

How can I cause Media Center to restore to its previous full-screen state when a user exits my executable after it was launched within Media Center?

Answer:

To start with, you will need to register the EXE as a Windows Media Center application using the RegisterApplication API or the RegisterMceApp utility.  When registering an EXE, you will need to create an entry point that specifies a Run attribute that points to the path of the EXE on the local file system.

If your EXE is designed to run both from within Media Center and as a standalone application, you will need some mechanism of differentiating how the user launched the EXE so that you can have the application navigate back to Media Center when the user exits only in the case when it was launched from within Media Center.  One way to differentiate launch states is to have Windows Media Center pass a command line parameter when launching the application.

Windows Media Center does not support specifying any command line parameters in the Run attribute, so you will need to work around that limitation somehow.  Possible options include the following:

  1. Create a Windows link (.lnk) file and register this link file so it will be launched by the Run attribute.  Then, you can pass a command line parameter via the properties for the link file to signal to the application that it has been launched by Media Center
  2. Create a wrapper EXE that is used to launch the main application within Media Center.  The wrapper EXE can launch the main application with a Media Center-specific command line switch to signal to the application that it has been launched by Media Center and it will need to navigate back to Media Center when it exits

In addition to registering the application, you will need to add logic in the application itself to handle navigating back to Media Center when the application exits.  The following is an example algorithm that can be used to accomplish this:

  • Set a flag when the Media Center-specific command line switch is passed in when the application is launched
  • If the flag is set to true, perform any Media Center-specific initialization for the application (such as updating the UI so it will display better on a TV or behave better via a remote control)
  • When the application exits, check for the existence of this flag
  • If the flag is set to true, call the Win32 FindWindow API and pass in eHome Render Window for the class name and NULL for the window name
  • If FindWindow returns a valid HWND value, call the Win32 ShowWindow API and pass in the HWND and SW_RESTORE for the command

Example of an EXE that is registered as a Windows Media Center application:

The Windows Vista games (such as Chess, FreeCell, etc) are examples of EXEs that are designed to launch as Media Center applications and as standalone applications.  They use the 1st strategy listed above for their Media Center application registration.

As an example, you can open regedit on a Windows Vista Home Premium or Ultimate system and look at the Media Center entry point registration for one of the Windows games.  For the chess game, the Run attribute is located in the following location in the registry and contains the following setting:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Media Center\Extensibility\Entry Points\{115EADF1-41C4-471b-8FE5-7A52B91BFE75}]
Run=%ProgramFiles%\Microsoft Games\Chess\ChessMCE.lnk

Next, you can go to the %ProgramFiles%\Microsoft Games\Chess folder specified by the Run entry, right-click on the ChessMCE.lnk file and choose Properties.  In the properties page for the .lnk file, you can see that a command line parameter is being passed to the game executable by looking at the value in the Target text box.  For the Chess game, it looks like the following:

"%ProgramFiles%\Microsoft Games\chess\chess.exe" -mce

The Windows games use this -mce command line switch to determine whether or not they were launched within Media Center.  When the games are launched with the -mce switch, they will display in full-screen UI mode, hide the standard Windows title bar and menu, and display text with larger fonts that are easier to read on a TV from 10 feet away.  They also navigate the user back to Media Center when the user exits the application.

Interestingly, you can also launch the games with this -mce command line switch on your own outside of Media Center, and they will launch Media Center when you exit, even if it was not previously running on the system.