Associate a File Extension with Your Application

The autor of this source code: Microsoft.

When you double click on file with MP3 extension, it opens with WinAmp (or other player).
This example will show you how to associate your own extension with your application.
When the user will double click on .xxx file (for example), it will open with your application, and your application will get the file name as parameter.

Preparations

Add 1 Command Button to your form.
Click the button to associate the .xxx extension with your application.

Module Code

Declare Function RegCreateKey Lib "advapi32.dll" Alias _
      "RegCreateKeyA" (ByVal hKey As Long, _
                        ByVal lpSubKey As String,  phkResult As Long) As Long
Declare Function RegSetValue Lib "advapi32.dll" Alias _
      "RegSetValueA" (ByVal hKey As Long, _
                 ByVal lpSubKey As String, ByVal dwType As Long, _
                 ByVal lpData As String, ByVal cbData As Long) As Long

' Return codes from Registration functions.
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_BADDB = 1&
Public Const ERROR_BADKEY = 2&
Public Const ERROR_CANTOPEN = 3&
Public Const ERROR_CANTREAD = 4&
Public Const ERROR_CANTWRITE = 5&
Public Const ERROR_OUTOFMEMORY = 6&
Public Const ERROR_INVALID_PARAMETER = 7&
Public Const ERROR_ACCESS_DENIED = 8&

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const MAX_PATH = 260&
Public Const REG_SZ = 1


Form Code

Private Sub Command1_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 .XXX associated with "MyApp".
      'You can replace ".XXX" with your wanted extension

          sKeyName = ".XXX"
      'replace all "MyApp" below with your application name
          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"
      'replace c:\mydir\my.exe with your exe file. In this example,
      'All the .XXX files will be opened with the file c:\mydir\my.exe

          sKeyValue = "c:\mydir\my.exe %1"
          ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
          ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, _
                              sKeyValue, MAX_PATH)
'*******************************************************
'That's All!
'Now to test this program, after you associate the .XXX files with
'c:\mydir\my.exe (In this example), start a new project, and
'copy the following 3 lines to your form (uncomment the lines)

'Private Sub Form_Load()
'    MsgBox Command
'End Sub

'compile the program to my.exe file, and put it in c:\mydir directory.
'Now go back to Windows and change the name of one of your files
'To   Test.xxx   and  double click on it. It will be opened with the program
'c:\mydir\my.exe and it will pop up message box: "c:\Test.xxx"
'***********************************************************
End Sub

Go Back