Visual Basic Registry Routines
RegSetValueEx: Create a Registered File Association
     
Posted:   Monday June 29, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   Paul Clement, VBnet - Randy Birch
     

Related:  

CreateProcess: Start Separate Instances of the Default Browser
FindExecutable: Obtain Exe of the Default Browser
FindExecutable: Find Exe Associated with a Registered Extension
     
 Prerequisites
None.

Newsgroup questions seem to come in waves, and the topic of this page is no exception -  how to create (associate) a Visual Basic program application extension in the registry. Thanks to an newsgroup example provided by Paul Clement, this marks the first in the registry series of VBnet code examples.

 

The illustrations show the two registry keys that need to be created and filled in order to have a given file extension associated with your project.  The actual routine to create the association is only around 10 lines .. most of the code below is comments. The first step is to create the extension identifier (in this code example the extension '.xxx', and add to it a pointer to the file type.  Next, you create the file type item itself, and under it add the desired shell commands to execute for different actions (open, print, etc).reg2.gif (9445 bytes)

The code creates the MyApp.Document type, associates it with the extension .xxx, and adds an Open command to the shell section. Once you've run the demo code here, create or save a text file with the extension .xxx, or use the test file included in this compiled VB5 test app (6k) to test the passing of the associated .xxx file via the command line.

 BAS Module Code
None.

 Form Code
Add a single command button to a form along with the following code. Make any desired changes in the CreateAssociation routine once the code runs successfully:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const REG_SZ As Long = &H1
Private Const REG_DWORD As Long = &H4
Private Const HKEY_CLASSES_ROOT As Long = &H80000000
Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
Private Const HKEY_USERS As Long = &H80000003

Private Const ERROR_SUCCESS As Long = 0
Private Const ERROR_BADDB As Long = 1009
Private Const ERROR_BADKEY As Long = 1010
Private Const ERROR_CANTOPEN As Long = 1011
Private Const ERROR_CANTREAD As Long = 1012
Private Const ERROR_CANTWRITE As Long = 1013
Private Const ERROR_OUTOFMEMORY As Long = 14
Private Const ERROR_INVALID_PARAMETER As Long = 87
Private Const ERROR_ACCESS_DENIED As Long = 5
Private Const ERROR_MORE_DATA As Long = 234
Private Const ERROR_NO_MORE_ITEMS As Long = 259

Private Const KEY_ALL_ACCESS As Long= &HF003F
Private Const REG_OPTION_NON_VOLATILE As Long = 0

Private Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long

Private Declare Function RegCreateKeyEx Lib "advapi32" _
    Alias "RegCreateKeyExA" _
   (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal Reserved As Long, _
    ByVal lpClass As String, _
    ByVal dwOptions As Long, _
    ByVal samDesired As Long, _
    ByVal lpSecurityAttributes As Long, _
    phkResult As Long, _
    lpdwDisposition As Long) As Long

Private Declare Function RegOpenKeyEx Lib "advapi32" _
    Alias "RegOpenKeyExA" _
   (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) As Long

Private Declare Function RegSetValueEx Lib "advapi32" _
   Alias "RegSetValueExA" _
  (ByVal hKey As Long, _
   ByVal lpValueName As String, _
   ByVal Reserved As Long, _
   ByVal dwType As Long, _
   lpValue As Any, _
   ByVal cbData As Long) As Long


Private Sub Command1_Click()

   Call CreateAssociation
   
End Sub


Private Sub CreateAssociation()

   Dim sPath As String
   
  'File Associations begin with a listing
  'of the default extension under HKEY_CLASSES_ROOT.
  'So the first step is to create that
  'root extension item. We'll use an extension
  'of .xxx to make it easy to locate in the registry.
   CreateNewKey ".xxx", HKEY_CLASSES_ROOT
   
   
  'To the extension just added, add a
  'subitem where the registry will look for
  'commands relating to the .xxx extension
  '("MyApp.Document"). Its type is String (REG_SZ)
   SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ
   
   
  'Create the 'MyApp.Document' item under
  'HKEY_CLASSES_ROOT. This is where you'll put
  'the command line to execute or other shell
  'statements necessary.
   CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT
   
   
  'Set its default item to "MyApp Document".
  'This is what is displayed in Explorer against
  'files with the .xxx extension. Its type is
  'String (REG_SZ)
   SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ
   
   
  'Finally, add the path to myapp.exe
  'Remember to add %1 as the final command
  'parameter to assure the app opens the passed
  'command line item, and enclose the path in quotes 
  'to ensure the long path name is received by the app.
  '(results in '"c:\LongPathname\Myapp.exe" "%1")
  'Again, its type is string.
   sPath = """c:\Some Long Path Name\Goes Here\Myapp.exe""" & " " & """%1"""
   SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ
   
  'All done
   MsgBox "The file association has been made!"
   
End Sub


Private Function SetValueEx(ByVal hKey As Long, _
                            sValueName As String, _
                            lType As Long, _
                            vValue As Variant) As Long

   Dim nValue As Long
   Dim sValue As String
   
   Select Case lType
      Case REG_SZ
         sValue = vValue & Chr$(0)
         SetValueEx = RegSetValueEx(hKey, _
                                    sValueName, _
                                    0&, _
                                    lType, _
                                    ByVal sValue, _
                                    Len(sValue))
         
      Case REG_DWORD
         nValue = vValue
         SetValueEx = RegSetValueEx(hKey, _
                                    sValueName, _
                                    0&, _
                                    lType, _
                                    ByVal nValue, _
                                    4)
   
   End Select
   
End Function


Private Sub CreateNewKey(sNewKeyName As String, _
                        lPredefinedKey As Long)

  'handle to the new key
   Dim hKey As Long
   Dim result As Long
   
   Call RegCreateKeyEx(lPredefinedKey, _
                       sNewKeyName, 0&, _
                       vbNullString, _
                       REG_OPTION_NON_VOLATILE, _
                       KEY_ALL_ACCESS, 0&, hKey, result)
   
   Call RegCloseKey(hKey)

End Sub


Private Sub SetKeyValue(sKeyName As String, _
                       sValueName As String, _
                       vValueSetting As Variant, _
                       lValueType As Long)

  'handle of opened key
   Dim hKey As Long
   
  'open the specified key
   Call RegOpenKeyEx(HKEY_CLASSES_ROOT, _
                    sKeyName, 0, _
                    KEY_ALL_ACCESS, hKey)
                    
   Call SetValueEx(hKey, _
                   sValueName, _
                   lValueType, _
                   vValueSetting)
                  
   Call RegCloseKey(hKey)

End Sub
 Comments
Save the project & run. Clicking the button will instantly display the message box, and the association has been made.

To retrieve the name of the file(s) used as the start-up parameter, use the Command$() function in VB.

Note: To ensure your test association is as you expect, be sure that either the app exe is named myapp.exe and is placed into a folder on drive C: named c:\longpathname, or make the appropriate changes to the code above *before* running the CreateAssociation routine!


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter