SAMPLE: ActiveX Object as the Default Player for a MIME TypeLast reviewed: January 19, 1998Article ID: Q165072 |
The information in this article applies to:
SUMMARYThis article and its associated sample describe 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. As an example of the use of this, 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.
MORE INFORMATIONThe following file is available for download from the Microsoft Software Library:
~ Mimetype.exe (size: 56765 bytes)For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q119591 TITLE : How to Obtain Microsoft Support Files from Online Services PurposeMimetype.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 PlayersThere 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 PlayerIn order for an ActiveX component to act as the default MIME type player, it must have the following registry entries:
// 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 PlayIn 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 FileInternet 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 SampleMimetype.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. Keywords : kbinterop kbprg kbhowto Version : 1.0,4.0,4.01 Platform : WINDOWS Issue type : kbhowto Solution Type : kbfile |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |