Results 1 to 7 of 7

Thread: Opening files with your program

  1. #1

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247

    Opening files with your program

    If i made a text editor, for example, how would i make it so the user can click on my special .abc files and they would open up with MY editor? Possible in VB?

  2. #2
    Hyperactive Member
    Join Date
    Feb 2002
    Location
    Connecticut, USA
    Posts
    308
    Are your .abc files text files?

  3. #3
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    You'd have to create a registry key under HKEY_CLASSES_ROOT called ".abc" and then make a new folder called "ShellEx" and do something there, I've forgotten, sorry. Try searching, it could be somewhere.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  4. #4

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247
    AttnSue- its just an example, but yes (if you want, they can be txt files, i just want to know how to open them it my program)

    Oafo- Thanks , thats a big help. Ill ask around about that.

    Anyone else know how?

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    File Association 101:

    VB Code:
    1. 'Make a new project. Add a module. To the form add a command button.
    2.  
    3. 'Code:
    4. 'Add this code to the module:
    5.  
    6. Option Explicit
    7.  
    8. Public Const REG_SZ As Long = 1
    9. Public Const REG_DWORD As Long = 4
    10. Public Const HKEY_CLASSES_ROOT = &H80000000
    11. Public Const HKEY_CURRENT_USER = &H80000001
    12. Public Const HKEY_LOCAL_MACHINE = &H80000002
    13. Public Const HKEY_USERS = &H80000003
    14.  
    15. Public Const ERROR_NONE = 0
    16. Public Const ERROR_BADDB = 1
    17. Public Const ERROR_BADKEY = 2
    18. Public Const ERROR_CANTOPEN = 3
    19. Public Const ERROR_CANTREAD = 4
    20. Public Const ERROR_CANTWRITE = 5
    21. Public Const ERROR_OUTOFMEMORY = 6
    22. Public Const ERROR_INVALID_PARAMETER = 7
    23. Public Const ERROR_ACCESS_DENIED = 8
    24. Public Const ERROR_INVALID_PARAMETERS = 87
    25. Public Const ERROR_NO_MORE_ITEMS = 259
    26.  
    27. Public Const KEY_ALL_ACCESS = &H3F
    28. Public Const REG_OPTION_NON_VOLATILE = 0
    29.  
    30. Public Declare Function RegCloseKey Lib "advapi32.dll" _
    31.    (ByVal hKey As Long) As Long
    32.  
    33. Public Declare Function RegCreateKeyEx _
    34.     Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    35.    (ByVal hKey As Long, _
    36.     ByVal lpSubKey As String, _
    37.     ByVal Reserved As Long, _
    38.     ByVal lpClass As String, _
    39.     ByVal dwOptions As Long, _
    40.     ByVal samDesired As Long, _
    41.     ByVal lpSecurityAttributes As Long, _
    42.     phkResult As Long, _
    43.     lpdwDisposition As Long) As Long
    44.  
    45. Public Declare Function RegOpenKeyEx _
    46.     Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    47.    (ByVal hKey As Long, _
    48.     ByVal lpSubKey As String, _
    49.     ByVal ulOptions As Long, _
    50.     ByVal samDesired As Long, _
    51.     phkResult As Long) As Long
    52.  
    53. Public Declare Function RegSetValueExString _
    54.     Lib "advapi32.dll" Alias "RegSetValueExA" _
    55.    (ByVal hKey As Long, _
    56.     ByVal lpValueName As String, _
    57.     ByVal Reserved As Long, _
    58.     ByVal dwType As Long, _
    59.     ByVal lpValue As String, _
    60.     ByVal cbData As Long) As Long
    61.  
    62. Public Declare Function RegSetValueExLong _
    63.    Lib "advapi32.dll" Alias "RegSetValueExA" _
    64.   (ByVal hKey As Long, _
    65.    ByVal lpValueName As String, _
    66.    ByVal Reserved As Long, _
    67.    ByVal dwType As Long, _
    68.    lpValue As Long, _
    69.    ByVal cbData As Long) As Long
    70.  
    71.  
    72. Public Sub CreateAssociation()
    73.  
    74.    Dim sPath As String
    75.    
    76.   'File Associations begin with a listing
    77.   'of the default extension under HKEY_CLASSES_ROOT.
    78.   'So the first step is to create that
    79.   'root extension item
    80.    CreateNewKey ".xxx", HKEY_CLASSES_ROOT
    81.    
    82.    
    83.   'To the extension just added, add a
    84.   'subitem where the registry will look for
    85.   'commands relating to the .xxx extension
    86.   '("MyApp.Document"). Its type is String (REG_SZ)
    87.    SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ
    88.    
    89.    
    90.   'Create the 'MyApp.Document' item under
    91.   'HKEY_CLASSES_ROOT. This is where you'll put
    92.   'the command line to execute or other shell
    93.   'statements necessary.
    94.    CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT
    95.    
    96.    
    97.   'Set its default item to "MyApp Document".
    98.   'This is what is displayed in Explorer against
    99.   'for files with a xxx extension. Its type is
    100.   'String (REG_SZ)
    101.    SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ
    102.    
    103.    
    104.   'Finally, add the path to myapp.exe
    105.   'Remember to add %1 as the final command
    106.   'parameter to assure the app opens the passed
    107.   'command line item.
    108.   '(results in '"c:\LongPathname\Myapp.exe %1")
    109.   'Again, its type is string.
    110.    sPath = "c:\LongPathname\Myapp.exe %1"
    111.    SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ
    112.    
    113.   'All done
    114.    MsgBox "The file association has been made!"
    115.    
    116. End Sub
    117.  
    118.  
    119. Public Function SetValueEx(ByVal hKey As Long, _
    120.                            sValueName As String, _
    121.                            lType As Long, _
    122.                            vValue As Variant) As Long
    123.  
    124.    Dim nValue As Long
    125.    Dim sValue As String
    126.    
    127.    Select Case lType
    128.       Case REG_SZ
    129.          sValue = vValue & Chr$(0)
    130.          SetValueEx = RegSetValueExString(hKey, _
    131.                                           sValueName, _
    132.                                           0&, _
    133.                                           lType, _
    134.                                           sValue, _
    135.                                           Len(sValue))
    136.          
    137.       Case REG_DWORD
    138.          nValue = vValue
    139.          SetValueEx = RegSetValueExLong(hKey, _
    140.                                         sValueName, _
    141.                                         0&, _
    142.                                         lType, _
    143.                                         nValue, _
    144.                                         4)
    145.    
    146.    End Select
    147.    
    148. End Function
    149.  
    150.  
    151. Public Sub CreateNewKey(sNewKeyName As String, _
    152.                         lPredefinedKey As Long)
    153.  
    154.   'handle to the new key
    155.    Dim hKey As Long
    156.  
    157.   'result of the RegCreateKeyEx function
    158.    Dim r As Long
    159.    
    160.    r = RegCreateKeyEx(lPredefinedKey, _
    161.                       sNewKeyName, 0&, _
    162.                       vbNullString, _
    163.                       REG_OPTION_NON_VOLATILE, _
    164.                       KEY_ALL_ACCESS, 0&, hKey, r)
    165.    
    166.    Call RegCloseKey(hKey)
    167.  
    168. End Sub
    169.  
    170.  
    171. Public Sub SetKeyValue(sKeyName As String, _
    172.                        sValueName As String, _
    173.                        vValueSetting As Variant, _
    174.                        lValueType As Long)
    175.  
    176.   'result of the SetValueEx function
    177.    Dim r As Long
    178.    
    179.   'handle of opened key
    180.    Dim hKey As Long
    181.    
    182.   'open the specified key
    183.    r = RegOpenKeyEx(HKEY_CLASSES_ROOT, _
    184.                     sKeyName, 0, _
    185.                     KEY_ALL_ACCESS, hKey)
    186.                    
    187.    r = SetValueEx(hKey, _
    188.                   sValueName, _
    189.                   lValueType, _
    190.                   vValueSetting)
    191.                  
    192.    Call RegCloseKey(hKey)
    193.  
    194. End Sub
    195.  
    196. 'Add this code to the command button:
    197.  
    198. Private Sub Command1_Click()
    199. CreateAssociation
    200. End Sub

    To get list of associations on your system :

    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' Copyright ©1996-2001 VBnet, Randy Birch, All Rights Reserved.
    3. ' Some pages may also contain other copyrights by the author.
    4. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    5. ' You are free to use this code within your own applications,
    6. ' but you are expressly forbidden from selling or otherwise
    7. ' distributing this source code without prior written consent.
    8. ' This includes both posting free demo projects made from this
    9. ' code as well as reproducing the code in text or html format.
    10. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    11.  
    12. Private Const MAX_PATH As Long = 260
    13. Private Const HKEY_CLASSES_ROOT As Long = &H80000000
    14. Private Const ERROR_SUCCESS As Long = 0
    15. Private Const vbDot As Long = 46
    16. Private Const SHGFI_USEFILEATTRIBUTES As Long = &H10
    17. Private Const SHGFI_TYPENAME As Long = &H400
    18. Private Const LB_SETTABSTOPS As Long = &H192
    19.  
    20. Private Type FILETIME
    21.    dwLowDateTime As Long
    22.    dwHighDateTime As Long
    23. End Type
    24.  
    25. Private Type SHFILEINFO
    26.    hIcon As Long
    27.    iIcon As Long
    28.    dwAttributes As Long
    29.    szDisplayName As String * MAX_PATH
    30.    szTypeName As String * 80
    31. End Type
    32.  
    33. Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
    34. Private Declare Function SHGetFileInfo Lib "shell32" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
    35. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    36.  
    37. Private Sub GetAssociatedFileListing()
    38.  
    39.    Dim dwIndex As Long
    40.    Dim sTypeName As String
    41.    Dim sSubkey As String * MAX_PATH
    42.    Dim sClass As String * MAX_PATH
    43.    Dim ft As FILETIME
    44.    
    45.    Do While RegEnumKeyEx(HKEY_CLASSES_ROOT, _
    46.                          dwIndex, _
    47.                          sSubkey, _
    48.                          MAX_PATH, _
    49.                          0, sClass, _
    50.                          MAX_PATH, ft) = ERROR_SUCCESS
    51.      
    52.       If Asc(sSubkey) = vbDot Then
    53.          
    54.         'Pass the returned string to get the file type
    55.          sTypeName = GetFileType(sSubkey)
    56.            
    57.          If Len(sTypeName) > 0 Then
    58.             List1.AddItem TrimNull(sSubkey) & vbTab & sTypeName
    59.          End If            
    60.       End If
    61.       dwIndex = dwIndex + 1  
    62.    Loop
    63. End Sub
    64.  
    65. Private Function GetFileType(sFile As String) As String
    66.  
    67.   'If successful returns the specified file's
    68.   'typename, returns an empty string otherwise.
    69.   'sFile does not have to exist and can be
    70.   'just a file extension.
    71.    Dim sfi As SHFILEINFO
    72.  
    73.    If SHGetFileInfo(sFile, 0&, _
    74.                     sfi, Len(sfi), _
    75.                     SHGFI_TYPENAME Or SHGFI_USEFILEATTRIBUTES) Then
    76.       GetFileType = TrimNull(sfi.szTypeName)
    77.    End If
    78.  
    79. End Function
    80.  
    81. Private Function TrimNull(startstr As String) As String
    82.  
    83.   'returns the string up to the first
    84.   'null, if present, or the passed string
    85.    Dim pos As Integer
    86.    
    87.    pos = InStr(startstr, Chr$(0))
    88.    
    89.    If pos Then
    90.       TrimNull = Left$(startstr, pos - 1)
    91.       Exit Function
    92.    End If
    93.  
    94.    TrimNull = startstr
    95.  
    96. End Function
    97.  
    98. Private Sub Command1_Click()
    99.    List1.Clear
    100.    Screen.MousePointer = vbHourglass  
    101.   'fill the listbox box with the file types and their extensions
    102.    Call GetAssociatedFileListing  
    103.    Screen.MousePointer = vbDefault  
    104.  
    105. End Sub
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6

    Thread Starter
    Addicted Member Flip's Avatar
    Join Date
    Jun 2002
    Location
    Burke, VA
    Posts
    247
    Thanks James! Some day ill be smart like you...

  7. #7
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Not smart I just know where to look (and how to store snippets of code)...

    Glad I could help...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width