Visual Basic Registry Routines
RegEnumKeyEx: Retrieve the Registered File Associations
     
Posted:   Sunday March 18, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows 2000
OS restrictions:   None
Author:   VBnet - Randy Birch, Brad Martinez
     

Related:  

RegSetValueEx: Create a Registered File Association
FindExecutable: Find Exe Associated with a Registered Extension
     
 Prerequisites
None.

This demo shows how to retrieve the registered file associations on a given system.

One method often suggested in newsgroups involves enumerating the keys under HKEY_CLASSES_ROOT, retrieving the subkeys, then doing further enumeration of the registry for each value returned to determining the type of association for each extension registered. For example, the returned key might be ".col", (HKEY_CLASSES_ROOT\.col) which points to the file type "COLFile", whose entry at HKEY_CLASSES_ROOT\COLFile returns "HTML Help Collection file" as the association type.

Thankfully, Windows provides a shortcut to this information via the SHGetFileInfo API and its SHGFI_TYPENAME flag. This method begins, as above, by enumerating only the extensions in the registry, but instead of doing a secondary enumeration for the file type, the returned extension is handed to SHGetFileInfo which retrieves the file type string for us.

If you examine the registry under HKEY_CLASSES_ROOT, you'll see that both the registered file extensions and the descriptive file type information are listed at the same level in the registry tree. To restrict the enumeration in this routine to only the file extensions, the first character of the enumerated data returned is checked for a leading dot and, if present, indicates the string is a file extension.

 BAS Module Code
None.

 Form Code
Add a single command button (Command1) and a listbox (List1) to a form, along with the following code:

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 MAX_PATH As Long = 260
Private Const HKEY_CLASSES_ROOT As Long = &H80000000
Private Const ERROR_SUCCESS As Long = 0
Private Const vbDot As Long = 46 ' Asc(".") = 46
Private Const SHGFI_USEFILEATTRIBUTES As Long = &H10
Private Const SHGFI_TYPENAME As Long = &H400
Private Const LB_SETTABSTOPS As Long = &H192

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type

Private Type SHFILEINFO 
   hIcon As Long
   iIcon As Long
   dwAttributes As Long
   szDisplayName As String * MAX_PATH
   szTypeName As String * 80
End Type

Private Declare Function RegEnumKeyEx Lib "advapi32" _
   Alias "RegEnumKeyExA" _
   (ByVal hKey As Long, _
   ByVal dwIndex As Long, _
   ByVal lpName As String, _
   lpcbName As Long, _
   ByVal lpReserved As Long, _
   ByVal lpClass As String, _
   lpcbClass As Long, _
   lpftLastWriteTime As FILETIME) As Long

Private Declare Function SHGetFileInfo Lib "shell32" _
   Alias "SHGetFileInfoA" _
  (ByVal pszPath As String, _
   ByVal dwFileAttributes As Long, _
   psfi As SHFILEINFO, _
   ByVal cbFileInfo As Long, _
   ByVal uFlags As Long) As Long

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long


Private Sub Form_Load()

  'Setup tabstops in the listbox by first
  'clearing existing tabs, then setting the
  'new tabstop value.
  
   ReDim TabArray(0) As Long
   
  'only one tabstop
   TabArray(0) = 75
  
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 1&, TabArray(0))
   List1.Refresh
   
   Command1.Caption = "Get Associations"

End Sub


Private Sub Command1_Click()

   List1.Clear
   Me.MousePointer = 11
   
  'fill the listbox box with the
  'file types and their extensions
   Call GetAssociatedFileListing
   
   Me.MousePointer = 0
   
End Sub


Private Sub GetAssociatedFileListing()

   Dim dwIndex As Long
   Dim sTypeName As String
   Dim sSubkey As String * MAX_PATH
   Dim sClass As String * MAX_PATH
   Dim ft As FILETIME
   
   Do While RegEnumKeyEx(HKEY_CLASSES_ROOT, _
                         dwIndex, _
                         sSubkey, _
                         MAX_PATH, _
                         0, sClass, _
                         MAX_PATH, ft) = ERROR_SUCCESS
      
      If Asc(sSubkey) = vbDot Then
         
        'Pass the returned string to get the file type
         sTypeName = GetFileType(sSubkey)
            
         If Len(sTypeName) > 0 Then
            List1.AddItem TrimNull(sSubkey) & vbTab & sTypeName
         End If
            
      End If

      dwIndex = dwIndex + 1
   
   Loop

End Sub


Private Function GetFileType(sFile As String) As String

  'If successful returns the specified file's
  'typename, returns an empty string otherwise.
  'sFile does not have to exist and can be
  'just a file extension.
   Dim sfi As SHFILEINFO

   If SHGetFileInfo(sFile, 0&, _
                    sfi, Len(sfi), _
                    SHGFI_TYPENAME Or SHGFI_USEFILEATTRIBUTES) Then
      GetFileType = TrimNull(sfi.szTypeName)
   End If

End Function


Public Function TrimNull(startstr As String) As String

  'returns the string up to the first
  'null, if present, or the passed string
   Dim pos As Integer
   
   pos = InStr(startstr, Chr$(0))
   
   If pos Then
      TrimNull = Left$(startstr, pos - 1)
      Exit Function
   End If
  
   TrimNull = startstr
  
End Function
 Comments
For assistance designing tabs in a list (or textbox), see the CoolTabs page.

 
 

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