Option Explicit
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function SHChangeNotify Lib "shell32.dll" (ByVal wEventID As Long, ByVal uFlags As Long, ByVal dwItem1 As String, ByVal dwItems As String) As Long
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const SHCNE_ASSOCCHANGED = &H8000000
Private Const SHCNF_IDLIST = &H0
Public Sub AssociateMyApp(ByVal sAppName As String, ByVal sEXE As String, ByVal sExt As String, Optional ByVal sCommand As String, Optional ByVal sIcon As String)
Dim sCommandString As String
Dim lRegKey As Long
'Open/Create the Extension under the "HKEY_CLASSES_ROOT" Hive of the Registry
Call RegCreateKey(HKEY_CLASSES_ROOT, "." & sExt, lRegKey)
'Set the "Default" value of the Key to the Application Name
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sAppName, Len(sAppName))
'Close the Registry Key
Call RegCloseKey(lRegKey)
sCommand = "\Shell\" & IIf(Len(sCommand), sCommand, "Open") & "\Command"
'Create the Application Key in the "HKEY_CLASSES_ROOT" Hive of the Registry
Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & sCommand, lRegKey)
'Set the Command to the EXE
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sEXE, Len(sEXE))
'Close the Registry Key
Call RegCloseKey(lRegKey)
'If an Icon is required...
If Len(sIcon) Then
'Create a "DefaultIcon" entry under the Association Key
Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & "\DefaultIcon", lRegKey)
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sIcon, Len(sIcon))
Call RegCloseKey(lRegKey)
End If
'Notify the Shell that an Association has Changed, (Updates Icons).
SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, vbNullString, vbNullString
End Sub