Opening Files in Written VB Apps

Started by Urban,

Urban

I know this has nothing to do with BASS...
actually it does, but anyway...

Has anyone out there got source code or knowledge on how to get a VB application to open file formats...

for example Photoshop can open BMP, Windows Media Player opens AVIs... how can you write an little program in VB to open a file and manipulate it for the programs well being.

Like I write a program that stores playlist as a unique name, like .upl or something like that, but I don't want to go through all the trouble of opening my program and then importing the file... I just want to double click on the file and walla - the information that was stored by my little app is stored into a list.

Anyone give me any ideas?

Thanks

Urban


Sbeastian

Well, if you want to know which file has been passed to your program, you can use "Command". Make sure you register your MP3 files for example to open with your player (Explorer -> Tools -> Folder Options -> File Types) and grab the passed files inside Form_Load using "Command".

To see what I mean, type the following code inside Form_Load:
MsgBox Command$

gogman

Howdy,

I think this code should cover what you are trying to do:

Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, _
ByVal lpData As String, ByVal cbData As Long) As Long

' Return codes from Registration functions.
Private Const ERROR_SUCCESS = 0&
Private Const ERROR_BADDB = 1&
Private Const ERROR_BADKEY = 2&
Private Const ERROR_CANTOPEN = 3&
Private Const ERROR_CANTREAD = 4&
Private Const ERROR_CANTWRITE = 5&
Private Const ERROR_OUTOFMEMORY = 6&
Private Const ERROR_INVALID_PARAMETER = 7&
Private Const ERROR_ACCESS_DENIED = 8&

Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const MAX_PATH = 260&
Private Const REG_SZ = 1

Public Sub AssociateFile ( flExtension As String, appName As String, appPath As String, appDesc As String)

' This sub creates and sets the necessary keys in the registry

' Arguments:
' flExtension = the file extension to be associated.
' appName = your application's name
' appPath = the path for the app's executable
' appDesc = a simple description of your program

          Dim sKeyName As String   'Holds Key Name in registry.
          Dim sKeyValue As String  'Holds Key Value in registry.
          Dim ret&           'Holds error status if any from API calls.
          Dim lphKey&        'Holds created key handle from RegCreateKey.

      'This creates a Root entry for the app's name.

          sKeyName = appName
          sKeyValue = appDesc      ' The application's description
          ret& = RegCreateKey (HKEY_CLASSES_ROOT, sKeyName, lphKey&)
          ret& = RegSetValue (lphKey&, "", REG_SZ, sKeyValue, 0&)

      'This creates a Root entry with the name of flExtension associated with appName
          sKeyName = flExtension
          sKeyValue = appName
          ret& = RegCreateKey (HKEY_CLASSES_ROOT, sKeyName, lphKey&)
          ret& = RegSetValue (lphKey&, "", REG_SZ, sKeyValue, 0&)

      'This sets the command line for the application
          sKeyName = appName
          sKeyValue = appPath & " %1"
          ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
          ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, _
          sKeyValue, MAX_PATH)

End Sub

And that's it! Use the AssociateFile function to associate any sort of file extension with your application.
For example, to associate files with an extension ".lst" with a program called ListViewer, simply use the code below:

Call AssociateFile ( ".lst", "ListViewer", "c:\ListViewer\LViewer.exe", "List Viewer Application" )