How do I associate a file extension with my application 
Author Message
 How do I associate a file extension with my application

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.



Sun, 30 Apr 2000 03:00:00 GMT  
 How do I associate a file extension with my application

->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
~~~~



Wed, 03 May 2000 03:00:00 GMT  
 How do I associate a file extension with my application

no problem if you can handle the registry...
run regedit, and look at the existing file types (at hkey_classes_root)
for examples...
(i have a current problem with the registry too... i'm trying to create
my own folder
using my own icon during the setup...)
if you cant handle the registry, mail me, and i'll send you example
source code :)

-retro



Mon, 08 May 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. associating file extensions with applications

2. associating file extensions with applications

3. Associating file extension with Parent Application

4. Creating file extension & assigning associated application

5. Creating file extension & assigning associated application

6. programmatically list extensions and associated applications -- how?

7. Running the application associated with and extension

8. To Associate an extension to an application.

9. Browse to associate a file extension

10. i need to associate a file extension!

11. Retrieving the associated program for a given file extension

12. associating a file extension with a vbs

 

 
Powered by phpBB® Forum Software