VB Code:
  1. 'Make a new project. Add a module. To the form add a command button.
  2.  
  3. 'Code:
  4. 'Add this code to the module:
  5.  
  6. Option Explicit
  7.  
  8. Public Const REG_SZ As Long = 1
  9. Public Const REG_DWORD As Long = 4
  10. Public Const HKEY_CLASSES_ROOT = &H80000000
  11. Public Const HKEY_CURRENT_USER = &H80000001
  12. Public Const HKEY_LOCAL_MACHINE = &H80000002
  13. Public Const HKEY_USERS = &H80000003
  14.  
  15. Public Const ERROR_NONE = 0
  16. Public Const ERROR_BADDB = 1
  17. Public Const ERROR_BADKEY = 2
  18. Public Const ERROR_CANTOPEN = 3
  19. Public Const ERROR_CANTREAD = 4
  20. Public Const ERROR_CANTWRITE = 5
  21. Public Const ERROR_OUTOFMEMORY = 6
  22. Public Const ERROR_INVALID_PARAMETER = 7
  23. Public Const ERROR_ACCESS_DENIED = 8
  24. Public Const ERROR_INVALID_PARAMETERS = 87
  25. Public Const ERROR_NO_MORE_ITEMS = 259
  26.  
  27. Public Const KEY_ALL_ACCESS = &H3F
  28. Public Const REG_OPTION_NON_VOLATILE = 0
  29.  
  30. Public Declare Function RegCloseKey Lib "advapi32.dll" _
  31.    (ByVal hKey As Long) As Long
  32.  
  33. Public Declare Function RegCreateKeyEx _
  34.     Lib "advapi32.dll" Alias "RegCreateKeyExA" _
  35.    (ByVal hKey As Long, _
  36.     ByVal lpSubKey As String, _
  37.     ByVal Reserved As Long, _
  38.     ByVal lpClass As String, _
  39.     ByVal dwOptions As Long, _
  40.     ByVal samDesired As Long, _
  41.     ByVal lpSecurityAttributes As Long, _
  42.     phkResult As Long, _
  43.     lpdwDisposition As Long) As Long
  44.  
  45. Public Declare Function RegOpenKeyEx _
  46.     Lib "advapi32.dll" Alias "RegOpenKeyExA" _
  47.    (ByVal hKey As Long, _
  48.     ByVal lpSubKey As String, _
  49.     ByVal ulOptions As Long, _
  50.     ByVal samDesired As Long, _
  51.     phkResult As Long) As Long
  52.  
  53. Public Declare Function RegSetValueExString _
  54.     Lib "advapi32.dll" Alias "RegSetValueExA" _
  55.    (ByVal hKey As Long, _
  56.     ByVal lpValueName As String, _
  57.     ByVal Reserved As Long, _
  58.     ByVal dwType As Long, _
  59.     ByVal lpValue As String, _
  60.     ByVal cbData As Long) As Long
  61.  
  62. Public Declare Function RegSetValueExLong _
  63.    Lib "advapi32.dll" Alias "RegSetValueExA" _
  64.   (ByVal hKey As Long, _
  65.    ByVal lpValueName As String, _
  66.    ByVal Reserved As Long, _
  67.    ByVal dwType As Long, _
  68.    lpValue As Long, _
  69.    ByVal cbData As Long) As Long
  70.  
  71.  
  72. Public Sub CreateAssociation()
  73.  
  74.    Dim sPath As String
  75.    
  76.   'File Associations begin with a listing
  77.   'of the default extension under HKEY_CLASSES_ROOT.
  78.   'So the first step is to create that
  79.   'root extension item
  80.    CreateNewKey ".xxx", HKEY_CLASSES_ROOT
  81.    
  82.    
  83.   'To the extension just added, add a
  84.   'subitem where the registry will look for
  85.   'commands relating to the .xxx extension
  86.   '("MyApp.Document"). Its type is String (REG_SZ)
  87.    SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ
  88.    
  89.    
  90.   'Create the 'MyApp.Document' item under
  91.   'HKEY_CLASSES_ROOT. This is where you'll put
  92.   'the command line to execute or other shell
  93.   'statements necessary.
  94.    CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT
  95.    
  96.    
  97.   'Set its default item to "MyApp Document".
  98.   'This is what is displayed in Explorer against
  99.   'for files with a xxx extension. Its type is
  100.   'String (REG_SZ)
  101.    SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ
  102.    
  103.    
  104.   'Finally, add the path to myapp.exe
  105.   'Remember to add %1 as the final command
  106.   'parameter to assure the app opens the passed
  107.   'command line item.
  108.   '(results in '"c:\LongPathname\Myapp.exe %1")
  109.   'Again, its type is string.
  110.    sPath = "c:\LongPathname\Myapp.exe %1"
  111.    SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ
  112.    
  113.   'All done
  114.    MsgBox "The file association has been made!"
  115.    
  116. End Sub
  117.  
  118.  
  119. Public Function SetValueEx(ByVal hKey As Long, _
  120.                            sValueName As String, _
  121.                            lType As Long, _
  122.                            vValue As Variant) As Long
  123.  
  124.    Dim nValue As Long
  125.    Dim sValue As String
  126.    
  127.    Select Case lType
  128.       Case REG_SZ
  129.          sValue = vValue & Chr$(0)
  130.          SetValueEx = RegSetValueExString(hKey, _
  131.                                           sValueName, _
  132.                                           0&, _
  133.                                           lType, _
  134.                                           sValue, _
  135.                                           Len(sValue))
  136.          
  137.       Case REG_DWORD
  138.          nValue = vValue
  139.          SetValueEx = RegSetValueExLong(hKey, _
  140.                                         sValueName, _
  141.                                         0&, _
  142.                                         lType, _
  143.                                         nValue, _
  144.                                         4)
  145.    
  146.    End Select
  147.    
  148. End Function
  149.  
  150.  
  151. Public Sub CreateNewKey(sNewKeyName As String, _
  152.                         lPredefinedKey As Long)
  153.  
  154.   'handle to the new key
  155.    Dim hKey As Long
  156.  
  157.   'result of the RegCreateKeyEx function
  158.    Dim r As Long
  159.    
  160.    r = RegCreateKeyEx(lPredefinedKey, _
  161.                       sNewKeyName, 0&, _
  162.                       vbNullString, _
  163.                       REG_OPTION_NON_VOLATILE, _
  164.                       KEY_ALL_ACCESS, 0&, hKey, r)
  165.    
  166.    Call RegCloseKey(hKey)
  167.  
  168. End Sub
  169.  
  170.  
  171. Public Sub SetKeyValue(sKeyName As String, _
  172.                        sValueName As String, _
  173.                        vValueSetting As Variant, _
  174.                        lValueType As Long)
  175.  
  176.   'result of the SetValueEx function
  177.    Dim r As Long
  178.    
  179.   'handle of opened key
  180.    Dim hKey As Long
  181.    
  182.   'open the specified key
  183.    r = RegOpenKeyEx(HKEY_CLASSES_ROOT, _
  184.                     sKeyName, 0, _
  185.                     KEY_ALL_ACCESS, hKey)
  186.                    
  187.    r = SetValueEx(hKey, _
  188.                   sValueName, _
  189.                   lValueType, _
  190.                   vValueSetting)
  191.                  
  192.    Call RegCloseKey(hKey)
  193.  
  194. End Sub
  195.  
  196. 'Add this code to the command button:
  197.  
  198. Private Sub Command1_Click()
  199.  
  200. CreateAssociation
  201.  
  202. End Sub