SAMPLE: How to Use File Associations

Last reviewed: August 5, 1996
Article ID: Q122787
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.1 and 3.5
    

SUMMARY

Windows provides file associations so that an application can register the type of documents it supports. The benefit of doing this is that it allows the user to double-click or select a document in File Manager to edit or print it. File association is also supported by the ShellExecute() API. File associations also allow the user to open multiple documents with a single instance of the application via the File Manager.

ShellExecute() has even more benefit in Windows 95.

The sample FILEASSO.EXE demonstrates how to use file associations. Download FILEASSO.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download FILEASSO.EXE (size: 40993 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the SOFTLIB\MSLFILES directory
          Get FILEASSO.EXE (size: 40993 bytes) 
    

MORE INFORMATION

The following information applies to both File Open and File Print. For ease of reading, this article will discuss File Open to explain how File Associations work.

When the user double-clicks a document, the File Manager calls ShellExecute() with filename. ShellExecute() checks the Registration Database for an entry that associates that file extension with a particular application. If an entry exists and does not specify DDE commands, then ShellExecute() launches the application as specified in the registry. If the registry specifies to use DDE commands, ShellExecute() attempts to establish a DDE conversation with that application using the application topic. If an application responds to the DDE connections, ShellExecute() sends a DDE Execute command, as specified in the registry. It is up to the application to define the specifics on this conversation, particularly the service and topic name to connect to, and also the correct DDE execute command syntax to use. However, if attempts to establish the conversation fail, ShellExecute() launches the application specified in the regiistry and tries to establish the DDE connection again.

There is one more option available when the application is not running, which seems to be appropriate for File Print. In this option, ShellExecute() sends a different Execute statement, the application needs to Open and Print the document. When the Printing is done, it exits.

There are two steps for an application to open multiple documents through single application instance via File association. As an example, assume MyApp is the application and AssocSupport is the topic. Most applications use MyApp as their application name and System as the topic.

  1. When the application starts, register a DDE Server with the application name and topic (for example MyApp, and AssocSupport). The application also has to support DDE Execute Statements. The execute statement could be any format; at minimum, it should be:

          <Action> <fileName> <options>
    

    Here <Action> is anything specifying unique identification of the action, such as Open or Print. The <fileName> is the file that should be operated on. Finally, <options> can be any options that need to be passed on.

    A typical Execute Statement follows this format:

          [<Action>(<FileName>)]
    

    For example, Microsoft Word uses:

          [Open("%1")]
    

    The Application has to support the required functionality for executed statements.

  2. File association can be done in Windows NT via File Manager or regedit.

Using the File Manager to Set File Associations

When associating a file type using the File Manager, choose Associate from the File menu. The Associate dialog presents the list of existing file associations. This dialog allows you to add a new file type (or file association), modify an existing file type, or delete an existing file type. The New File Type button allows the user to add an association for a new file extension. Here are the steps:

  1. Add a File type name. For example, name it "Microsoft Word 6.0 Document."

  2. Choose an action (Open or Print). For example, select the Uses DDE check box.

  3. Add the directory path and application name. For example, enter WINWORD as the application.

  4. Select the option Uses DDE.

  5. Set the Application as the DDE Server Name.

  6. Set the Topic as the DDE Server. For example enter System as the Topic.

  7. Set the DDE Message <Action> <fileName> <options> to be the same as your application's Execute Statement. However the <fileName> and <options> should be replaced by whatever the command line arguments are. For example use:

          DDE Message : [FileOpen("%1")]
    

Using Regedit in Windows NT to Set File Associations

NOTE: Regedit is available only in Windows NT, not in Windows version 3.1.

The user can also associate files with an application by using regedit. From the Edit menu, choose Add File Type or Modify File Type (to modify an existing file type). A dialog similar to File Manager Associate dialog appears. Follow the same steps as described for File Manager. In Windows version 3.1, once you have defined a File Type via this method, go to the File Manager associate dialog and attach the file type to the extension.

Using a Program to Set File Assoications

You can also set the associations programmatically. This is useful when setting up your application on other machines. You would provide this functionality through your installation program. The first way to do this (the simplier method) is to use regedit to merge the changes from a file. The syntax for this is:

   regedit <filename>.reg

An example of a <filename>.reg is:

   REGEDIT
   HKEY_CLASSES_ROOT\.riy = FMA000_File_assoc
   HKEY_CLASSES_ROOT\FMA000_File_assoc = File_assoc
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\command = fileasso.EXE
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\ddeexec = [Open(%1)]
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\ddeexec\application
= Myserver
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\ddeexec\topic = system
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\command = fileasso.EXE
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\ddeexec = [Open(%1)]

HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\ddeexec\application = MYServer
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\ddeexec\topic = System
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\ddeexec\ifexec =
[Test(%1) ]

In the program, you can also add keys to the registry by using the registry APIs. The developer needs to add the following keys to the registration database:

   // Your extensions.
   HKEY_CLASSES_ROOT\.riy = FMA000_File_assoc

   //File type name.
   HKEY_CLASSES_ROOT\FMA000_File_assoc = File_assoc

   // Command to execute when application is not running or dde is not
   // present and Open command is issued.
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\command = fileasso.EXE

   // DDE execute statement for Open.
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\ddeexec = [Open(%1)]

   // The server name your application responds to.
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\ddeexec\application =
   Myserver

   // Topic name your application responds to.
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\open\ddeexec\topic = system

   // Command to execute when application is not running or dde is not
   // present and print command is issued.
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\command = fileasso.EXE

   // DDE execute statement for Print.
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\ddeexec = [Open(%1)]

   // The server name your application responds to.
   HKEY_CLASSES_ROOT\FMA000_File_assoc\shell\print\ddeexec\application =
   MYServer

   // Topic name your application responds to.
   HKEY_CLASSES_ROOT\FMA000_File assoc\shell\print\ddeexec\topic = System

   // DDE execute statement for print if the application is not already
   // running. This gives the options for a an application to Run, Print
   // and Exit.
   HKEY_CLASSES_ROOT\FMA000_File assoc\shell\print\ddeexec\ifexec =
   [Test(%1)]

REFERENCES

Windows SDK Programmers Reference, Volume 1, chapter 7, Shell Library or Books Online.

Window 3.1 SDK Help file, Registration Database, Shell Library Functions.

Win32 Programmers Reference, Volume 2, chapter 52, Registry and Initialization Files or Books Online.

Win32 SDK Help file Registry and Initialization

File Manager Help File.

REGEDIT.HLP

REGEDT32.HLP


Additional reference words: 3.10 3.50
KBCategory: kbui kbfile kbwebcontent
KBSubcategory: UsrMisc


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 5, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.