->I'm running VB 5. I found 2 knowledge base articles, but both
applied to
->16 bit VB. I tried the examples anyway and they didn't work.
->
->I can probably write some code to modify the registry and copy what
other
->applications have done with open and file, but I would like to make
sure
->I'm doing it right.
This question was asked frequently enough that I decided a while back
to write the code to do it. It requires a bit of registry code but
here is an example that associates the .xxx file extension with a
program called MyApp.EXE. You can call MyApp.Document anything you
want, but just make certain that the .xxx file extension entry points
to this value:
Sub CreateAssociation()
Dim sShellPath As String
CreateNewKey ".xxx", HKEY_CLASSES_ROOT
SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ
CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT
SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ
' Double quotes around the path are required if long filenames are
used
sShellPath = Chr$(34) & "c:\Pathname\Myapp.exe" & Chr$(34) & " " &
Chr$(34) & "%1" & Chr$(34)
SetKeyValue "MyApp.Document\shell\open\command", "", sShellPath,
REG_SZ
End Sub
In a BAS Module:
Global Const REG_SZ As Long = 1
Global Const REG_DWORD As Long = 4
Global Const HKEY_CLASSES_ROOT = &H80000000
Global Const HKEY_CURRENT_USER = &H80000001
Global Const HKEY_LOCAL_MACHINE = &H80000002
Global Const HKEY_USERS = &H80000003
Global Const ERROR_NONE = 0
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259
Global Const KEY_ALL_ACCESS = &H3F
Global Const REG_OPTION_NON_VOLATILE = 0
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long)
As Long
Declare Function RegCreateKeyEx Lib "advapi32.dll" 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
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
Long) As Long
Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
String, ByVal cbData As Long) As Long
Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
"RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
ByVal cbData As Long) As Long
Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As
Long, vValue As Variant) As Long
Dim lValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue & Chr$(0)
SetValueEx = RegSetValueExString(hKey, sValueName, 0&,
lType, sValue, Len(sValue))
Case REG_DWORD
lValue = vValue
SetValueEx = RegSetValueExLong(hKey, sValueName, 0&,
lType, lValue, 4)
End Select
End Function
Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
Dim hNewKey As Long 'handle to the new key
Dim lRetVal As Long 'result of the RegCreateKeyEx
function
lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&,
vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey,
lRetVal)
RegCloseKey (hNewKey)
End Sub
Sub SetKeyValue(sKeyName As String, sValueName As String,
vValueSetting As Variant, lValueType As Long)
Dim lRetVal As Long 'result of the SetValueEx function
Dim hKey As Long 'handle of open key
'open the specified key
lRetVal = RegOpenKeyEx(HKEY_CLASSES_ROOT, sKeyName, 0,
KEY_ALL_ACCESS, hKey)
lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
RegCloseKey (hKey)
End Sub
Paul
~~~~