Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Knowledge Base

Post new topic   Reply to topic
Create File Association
View previous topic :: View next topic  
Author Message
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Oct 10th, 2003 08:52 AM    Post subject: Create File Association Reply with quote

Code:
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


Usage :

Code:
AssociateMyApp "Your Program Name", App.Path & "\" & App.EXEName & ".exe %1", "xxx", , App.Path & "\" & App.EXEName & ".exe"

_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
vb_programmer
Freshman


Joined: 03 Jan 2004
Posts: 31

PostPosted: Jan 19th, 2004 01:10 PM    Post subject: Reply with quote

Very Nice ... I liked adding Command to it. Will be more useful if same function removes file ext association. Just one more argument ... bAssociate as Boolean

It's still veru useful and compact in its present state.
_________________
----- vb_programmer ------
iNova Creations : Software Development Company !
(www.inovacreations.com)

RutuOnline.com : A Programmer's Resource Center !
(www.rutuonline.filetap.com)


Last edited by vb_programmer on Jan 19th, 2004 01:12 PM; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger ICQ Number
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 19th, 2004 04:49 PM    Post subject: Reply with quote

Thanks for the suggestion... I'll look into it. It shouldn't be to hard, just delete a registry key Smile
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
vb_programmer
Freshman


Joined: 03 Jan 2004
Posts: 31

PostPosted: Jan 20th, 2004 12:43 PM    Post subject: Reply with quote

Yes. You're right. It's all about DeleteRegKey ...

I'm going to use this code sample for my PHP IDE project. Actually , I'm also planning to preserve the existing file association (if any) in registry so that user will be able to roll back.

One thing I must say , your submissions has inspired me to post few of my codes (may not be as high standard as yours !) to this forum. I saw the complete collection. There are only two names .. P.T.A.M. & Avis ! I would like to see my name there as well Wink

Soon , I'll come back and post something useful here ...
_________________
----- vb_programmer ------
iNova Creations : Software Development Company !
(www.inovacreations.com)

RutuOnline.com : A Programmer's Resource Center !
(www.rutuonline.filetap.com)
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Knowledge Base All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic