Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

MimeType.exe makes ActiveX object default MIME type player


Summary

MimeType.exe demonstrates the steps necessary to register an ActiveX object as the default player for a MIME type and to receive the file name of the file to be played. Mimetype.exe is an MFC sample that creates an ActiveX control and registers it as the default player for a MIME type. It also illustrates how to handle the calls that Internet Explorer makes when sending the URL of the file to play.

For example, suppose that you have a component registered as the default player for the MIME type associated with the .mtp file extension. When Internet Explorer sees the following EMBED tag:
   <EMBED SRC="test.mtp" HEIGHT=200 WIDTH=600></EMBED>
				
or the following OBJECT tag:
   <OBJECT TYPE="application/x-mimetype" HEIGHT=200 WIDTH=600>
       <PARAM NAME="SRC" VALUE="test.mtp">
   </OBJECT>
				
or the user clicks on a link to a file with the .mtp extension:
   <A HREF="http://myserver/test.mtp">Play MIME-type</A>
				
Internet Explorer launches the registered component and passes Test.mtp's path to it as a property.

↑ Back to the top


More information

The following file is available for download from the Microsoft Download Center:
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591 How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.

Purpose

Mimetype.exe is an MFC sample that creates an ActiveX control and registers it as the default player for a MIME type. It also illustrates how to handle the calls that Internet Explorer makes when sending the URL of the file to play. The sample has been built and tested with Visual C++ on Intel platforms.

MIME Type Players

There are three situations where Internet Explorer (IE) uses MIME type players: when IE sees an EMBED tag in HTML; when a link is accessed either by clicking a hyperlink or by typing a URL in the address bar; and when IE sees an OBJECT tag in HTML when there is no CLASSID attribute but there is a MIME TYPE attribute specified.

In the case of the EMBED tag, the player associated with the MIME type of the SRC attribute URL is looked up. If a player exists, it is created within the current page with dimensions specified in the EMBED tag. The SRC URL is then passed to the player.

Similarly, when a new URL is navigated to, the player associated with the target URL is determined. If a player exists, it is loaded in the destination page and the URL is passed to the player. The dimensions of the player always are full size of the destination frame.

In the case of an OBJECT tag with no CLASSID attribute but with a MIME TYPE attribute, the player associated with the MIME TYPE attribute is looked up. If a player exists, it is created within the current page with dimensions specified in the OBJECT tag. The SRC URL is then passed to the player.

It is important to note that in each case the file to play is not downloaded by Internet Explorer. It is up to the player to download the file or interact with it in any way it sees fit.

Registering as a Player

In order for an ActiveX component to act as the default MIME type player, it must have the following registry entries:
  • HKEY_CLASSES_ROOT\MIME\DataBase\Content Type\<mime type>. This is required only if you are creating a new MIME type. It adds the new MIME type <mime type> to the MIME type database.
  • HKEY_CLASSES_ROOT\MIME\DataBase\Content Type\<mime type> Extension = <mime type ext>. This associates the file extension <mime type ext> with the MIME type <mime type>. Note that Extension is a named value, not a subkey.
  • HKEY_CLASSES_ROOT\MIME\DataBase\Content Type\<mime type> CLSID = <player CLSID>. This associates player's clsid with the MIME type <mime type>. Note that CLSID is a named value, not a subkey.
  • HKEY_CLASSES_ROOT\<mime type ext>. Registers the file extension. This is necessary only for new file extensions. In addition, if a player can be found directly from a MIME type, it is not necessary to register a file extension.
  • HKEY_CLASSES_ROOT\<mime type ext> Content Type = <mime type>. This associates the MIME type <mime type> with the file extension <mime type ext>, which is useful if the server does not know the MIME type of a given file. IE can check the extension and determine the MIME type from it. Notice that Content Type is a named value, not a subkey.
  • HKEY_CLASSES_ROOT\CLSID\<player clsid>. Class ID of player. This is automatically added if you are using MFC or the ActiveX Designer Framework to create your control.
  • HKEY_CLASSES_ROOT\CLSID\<player clsid>\Control. This identifies the player as a control that conforms to the OCX '94 specification. For this release of Internet Explorer, MIME type players must register as OCX '94 controls. Future versions of Internet Explorer will support other types of ActiveX objects as players. This key is added automatically if the control was created with MFC or the ActiveX Designer Framework.
  • HKEY_CLASSES_ROOT\CLSID\<player clsid>\EnableFullPage\<mime type extension>. This tells Internet Explorer that this player can show full frame. It is required to handle navigation to URLs with <mime type extension> as their extension.
The following code is taken from the sample's DllRegisterServer method (in Mimetype.cpp) and illustrates each of the registration steps in order:
   // Create new MIME type key for our new MIME type. Only necessary for
   // new mime types.
   if ( ERROR_SUCCESS != RegCreateKey(HKEY_CLASSES_ROOT, pszMTSubKey,
        &hkey) )
      break;

   // Add extension value to the MIME type key to associate .mtp files
   // with the application/x-mimetype MIME type
   if ( ERROR_SUCCESS != RegSetValueEx(hkey, pszMTExtVal, 0, REG_SZ,
      (const BYTE *)pszMTExt, strlen(pszMTExt)) )
      break;

   // Add class id to associate this object with the mime type
   if ( ERROR_SUCCESS != RegSetValueEx(hkey, pszMTCLSIDVal, 0, REG_SZ,
      (const BYTE *)pszMTCLSID, strlen(pszMTCLSID)) )
      break;

   RegCloseKey(hkey);

   // Register .mtp as a file extension. This is only necessary for new
   // file extensions. A new player for .avi files for instance would not
   // require this.
   if ( ERROR_SUCCESS != RegCreateKey(HKEY_CLASSES_ROOT, pszMTExt, &hkey)
 )
      break;

   // Add content type to associate this extension with the content type.
   // This is required and is used when the mime type is unknown and IE
   // looks up associations by file extension.
   if ( ERROR_SUCCESS != RegSetValueEx(hkey, pszMTContentVal, 0, REG_SZ,
      (const BYTE *)pszMTContent, strlen(pszMTContent)) )
      break;

   RegCloseKey(hkey);

   // Open the key under the control's clsid
   // HKEY_CLASSES_ROOT\CLSID\<CLSID>
   wsprintf(szSubKey, "%s\\%s", pszMTCLSIDVal, pszMTCLSID);
   if ( ERROR_SUCCESS != RegOpenKey(HKEY_CLASSES_ROOT, szSubKey, &hkey) )
      break;

   // Create the EnableFullPage and extension key under this so that we
   // can display files with the extension full frame in the browser.
   wsprintf(szSubKey, "%s\\%s", pszMTFullPage, pszMTExt);
   if ( ERROR_SUCCESS != RegCreateKey(hkey, szSubKey, &hkey1) )
      break;
				

Receiving the URL to Play

In all cases Internet Explorer passes the URL of the file to play to the default player via the IPropertyBag interface. That is, when Internet Explorer launches a player, it creates an instance of IPropertyBag and loads the "SRC" property with the URL of the file to play. Then it queries the player for IPersistPropertyBag and sends it the IPropertyBag instance through IPersistPropertyBag::Load.

In MFC, this manifests itself as a property exchange so you can load the "SRC" property directly in your override of DoPropExchange. The following code is from the sample CMimetypeCtrl::DoPropExchange method (in mtpctl.cpp):
   PX_String(pPX, "SRC", m_cstrFileName);
				
This gets the string property from the property bag and puts it into a CString member.

NOTE: If you are using the ActiveX Designer Framework (basectl in the ActiveX SDK), you get an override called LoadTextState that gets called from the framework's implementation of IPersistPropertyBag. You can call IPropertyBag::Read to get the "SRC" property.

Downloading the File

Internet Explorer does not download the file before sending the URL to the player. The player is responsible for retrieving the file and playing it. There are a number of ways to handle this depending on what the player needs to do. One way is illustrated in the sample with the URLDownloadToCacheFile function. This function downloads the file only if necessary and returns the name of the local cached copy of the file. It should be noted that URLDownloadToCacheFile is a blocking function. Even though the data is downloaded asynchronously the function does not return until all the data is downloaded.

If complete asynchronous downloading is desired, one of the other UOS functions, such as URLOpenStream, or perhaps general URL monikers would be more appropriate. The following code is taken from CMimetypeCtrl::DoPropExchange (in Mtpctl.cpp) and fills up the string m_cstrCacheFileName with the complete path of the local file:
   if ( FAILED(URLDownloadToCacheFile(
      GetControllingUnknown(),                // control's IUnknown
      m_cstrFileName,                         // URL to download
      m_cstrCacheFileName.GetBuffer(MAX_PATH),// buffer to fill with name
      MAX_PATH,                               // buffer length
      0,                                      // reserved
      NULL )) )                               // no status information
   {
      AfxMessageBox("Cannot download file");
      m_cstrCacheFileName = "Not Found";
      return;
   }
				

Running the Sample

Mimetype.exe is a self-extracting executable that you can expand to install the project files for the Mimetype control. Build the sample with Visual C++ 5.0 using the supplied Mimetype.dsw project file. The control is automatically be registered as part of the build process. You can then load the sample Test.htm into your browser.

The result of this should be two instances of the mimetype control loaded in the current page. The control displays as a rectangle with the name of the URL (Test.mtp) and the name of the local file (<path>\Test.mtp). The sample page also contains a link. Clicking on the link brings up a new page with the control loaded full frame displaying the same URL information.

The reason the supplied URL and the local file path are the same is that the URLDownloadToCacheFile function returns the full path of the supplied URL if it is a local (file:) URL. If the SRC URL in the EMBED and OBJECT tags or the destination of the link were a remote file (http:, ftp:), then the local file path would contain the full path of the cached copy of the remote file.

NOTE: The Mimetype control created by the sample project is not digitally signed. Because of this, depending on the current IE security settings, IE might either fail to instantiate the control or will warn before loading the control. If the IE security settings are set to High, IE will not load the sample control. To allow the control to be instantiated the IE security level must be set to either Medium or Low for the zone where the control is being loaded from. To set the security level in IE select the View menu and then the Internet Options menu item, choose the Security tab. Select either Medium or Low as the security level for the zone where the sample page is coming from.

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB165072, kbsample, kbinterop, kbinfo, kbfile

↑ Back to the top

Article Info
Article ID : 165072
Revision : 6
Created on : 6/22/2014
Published on : 6/22/2014
Exists online : False
Views : 364