VB3 How to Associate a File Extension with Your Application

Last reviewed: January 9, 1997
Article ID: Q119281
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic Programming System for Windows, version 1.0

SUMMARY

If your application makes use of data files and processes command-line arguments, you may want to associate the extension of your application's data file with your executable program name by modifying the REG.DAT file.

MORE INFORMATION

You can make modifications to the REG.DAT file by calling the RegCreateKey& and RegSetValue& application programming interface (API) functions.

Step-by-Step Example

  1. Start Visual Basic for Windows. If Visual Basic is already running, choose New Project from the File menu (ALT+F, N). Form1 is created by default.

  2. Add the following code to the general declarations section of Form1, taking care to enter each Declare statement on one single line:

       Declare Function RegCreateKey& Lib "SHELL.DLL" (ByVal hKey&, ByVal
       lpszSubKey$, lphKey&)
    
       Declare Function RegSetValue& Lib "SHELL.DLL" (ByVal hKey&, ByVal
       lpszSubKey$, ByVal fdwType&, ByVal lpszValue$, ByVal dwLength&)
    
       ' Return codes from Registration functions.
       Const ERROR_SUCCESS = 0&
       Const ERROR_BADDB = 1&
       Const ERROR_BADKEY = 2&
       Const ERROR_CANTOPEN = 3&
       Const ERROR_CANTREAD = 4&
       Const ERROR_CANTWRITE = 5&
       Const ERROR_OUTOFMEMORY = 6&
       Const ERROR_INVALID_PARAMETER = 7&
       Const ERROR_ACCESS_DENIED = 8&
    
       Const HKEY_CLASSES_ROOT = 1
       Const MAX_PATH = 256&
       Const REG_SZ = 1
    
    

  3. Place the following code in the Form_Click event of Form1:

       Sub Form_Click ()
    
         Dim sKeyName As String   'Holds Key Name in registry.
         Dim sKeyValue As String  'Holds Key Value in registry.
         Dim ret&                 'Holds error status if any from API calls.
         Dim lphKey&              'Holds created key handle from RegCreateKey.
    
         'This creates a Root entry called "MyApp".
         sKeyName = "MyApp"
         sKeyValue = "My Application"
         ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
         ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
    
         'This creates a Root entry called .BAR associated with "MyApp".
         sKeyName = ".bar"
         sKeyValue = "MyApp"
         ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
         ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
    
         'This sets the command line for "MyApp".
         sKeyName = "MyApp"
         sKeyValue = "c:\mydir\my.exe %1"
         ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
         ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, sKeyValue,
        MAX_PATH)
    
       End Sub
    
    

  4. Press ALT+F, V to save the project. Then press F5 to run the program. Click once on the form and exit the application.

  5. If you are running Windows version 3.1 or Windows for Workgroups, choose Run from the File menu (Alt+F, R) in Program Manager and then type "REGEDIT /V" (without the quotes) to run the registration database editor. When examining the REG.DAT, you will see the following entries:

          .bar = MyApp
          MyApp = My Application
    
            |
             -- Shell
                  |
                   -- open
                        |
                         -- command = c:\mydir\my.exe %1
    
       If you are running Windows NT, choose File Run (Alt+F, R) on the Program
       Manager and type "REGEDT32". This will run the registration database
       editor for Windows NT. The .BAR and MyApp entries can be found under
       the HKEY_CLASSES_ROOT on your local machine.
    


KBCategory: kbprg kbcode
KBSubcategory: PrgOther
Additional reference words: 1.00 2.00 3.00 vb3only


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: January 9, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.