Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Associating a file to my app and allowing the file to be passed in..

Status
Not open for further replies.

andyUK

Programmer
Dec 19, 2000
39
GB
I need to assoicite miy vb app to a file extension. The setup should allow me to pass a file into the app.
 
You can associate any file exstention with your app but remember you cant UNASSOCIATE it. The only way it can be unassociated is if another app try's to re associate itself.
I will give you my base module I wrote to help you out.
<<--Cut below here---------->>

'File association Base Module by Todd Norris


Option Explicit

Public Declare Function RegCloseKey Lib &quot;advapi32.dll&quot; (ByVal hKey As Long) As Long
Public Declare Function RegCreateKeyEx Lib &quot;advapi32.dll&quot; Alias &quot;RegCreateKeyExA&quot; (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
Public Declare Function RegOpenKeyEx Lib &quot;advapi32.dll&quot; Alias &quot;RegOpenKeyExA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegSetValueExString Lib &quot;advapi32.dll&quot; Alias &quot;RegSetValueExA&quot; (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
Public Declare Function RegSetValueExLong Lib &quot;advapi32.dll&quot; Alias &quot;RegSetValueExA&quot; (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

Public Const REG_SZ As Long = 1
Public Const REG_DWORD As Long = 4
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const ERROR_NONE = 0
Public Const ERROR_BADDB = 1
Public Const ERROR_BADKEY = 2
Public Const ERROR_CANTOPEN = 3
Public Const ERROR_CANTREAD = 4
Public Const ERROR_CANTWRITE = 5
Public Const ERROR_OUTOFMEMORY = 6
Public Const ERROR_INVALID_PARAMETER = 7
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_INVALID_PARAMETERS = 87
Public Const ERROR_NO_MORE_ITEMS = 259
Public Const KEY_ALL_ACCESS = &H3F
Public Const REG_OPTION_NON_VOLATILE = 0

Public Sub CreateAssociation(PROG As String, EXT As String, BESKRIV As String, IconLib As String)
Dim sPath As String
sPath = App.Path & &quot;\&quot; & App.EXEName & &quot; %1&quot;

CreateNewKey &quot;.&quot; & EXT, HKEY_CLASSES_ROOT
SetKeyValue &quot;.&quot; & EXT, &quot;&quot;, PROG, REG_SZ

CreateNewKey PROG & &quot;\shell\open\command&quot;, HKEY_CLASSES_ROOT
CreateNewKey PROG & &quot;\DefaultIcon&quot;, HKEY_CLASSES_ROOT

SetKeyValue PROG, &quot;&quot;, BESKRIV, REG_SZ
SetKeyValue PROG & &quot;\shell\open\command&quot;, &quot;&quot;, sPath, REG_SZ
SetKeyValue PROG & &quot;\DefaultIcon&quot;, &quot;&quot;, IconLib, REG_SZ
End Sub
Public Sub CreateNewKey(sNewKeyName As String, lPredefinedKey As Long)
'handle to the new key
Dim hKey As Long
'result of the RegCreateKeyEx function
Dim r As Long
r = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hKey, r)
Call RegCloseKey(hKey)
End Sub

Public Sub SetKeyValue(sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long)
'result of the SetValueEx function
Dim r As Long
'handle of opened key
Dim hKey As Long
'open the specified key
r = RegOpenKeyEx(HKEY_CLASSES_ROOT, sKeyName, 0, KEY_ALL_ACCESS, hKey)
r = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
Call RegCloseKey(hKey)
End Sub


Public Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
Dim nValue 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
nValue = vValue
SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, nValue, 4)
End Select
End Function

<---stop cut-->>

Here is an example of how to call this in your form. Remember to put the appropriate information in the &quot;&quot;'s


Call CreateAssociation(&quot;App Name&quot;, &quot;File extention ie/ Zip or txt etc&quot;, &quot;File Type&quot;, &quot;Icon to be associated ie/ test.ico&quot;)


This forum also did a couple of carrage returns when i pasted it in and that will cause this module to throw an error. If you have a problem email me and i will send it to you.

Todd Norris
Hope this helps !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top