Search Tools Links Login

File Associations and the Command Line


Visual Basic 6, or VB Classic

This tutorial will help you create a custom file extension that opens directly in your program when you open the file. Read on...

Original Author: Triumph

Code


So,
you want a file to open in your program, just
by opening the file, not starting the program first??á Read
on...


First, you need to come up with the extension(s) that your
program needs to be associated with.?á For example if you have a program
that saves a profile, ".prf" might be an appropriate extension.?á Just make
sure that your extension is not already used by something else.?á To do
this, enter the registry editor ([click] Start -> [click] Run... -> [type]
regedit).?á Go to HKEY_CLASSES_ROOT and search for your desired
extension.?á If it's not there, it's yours!!!


OK, now let's get to
some code.?á To set the extension, use this handy snippet (compliments of
MSDN Knowledge Base)





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 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.
Const ERROR_SUCCESS = 0&
Const ERROR_BADDB = 1&
Const ERROR_BADKEY = 2&
Const ERROR_CANTOPEN = 3&
Const ERROR_CANTREAD = 4&
Const ERROR_CANTWRITE = 5&
Const ERROR_OUTOFMEMORY = 6&
Const ERROR_INVALID_PARAMETER = 7&
Const ERROR_ACCESS_DENIED = 8&
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const MAX_PATH = 260&
Private Const REG_SZ = 1
Private Sub Form_Click()
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 called "MyApp".
sKeyName = "MyApp"
sKeyValue = "My Application"
ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
'This creates a Root entry called .BAR associated with "MyApp".
sKeyName = ".BAR"
sKeyValue = "MyApp"
ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
'This sets the command line for "MyApp".
sKeyName = "MyApp"
sKeyValue = "c:mydirmy.exe %1"
ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
ret& = RegSetValue&(lphKey&, "shellopencommand", REG_SZ, _
sKeyValue, MAX_PATH)
End Sub



Then, change 'MyApp' to match your program's name (abbreviated) and 'My Application' to your program's name, and the file path to your program's (make sure to leave the %1). Also, change the extension (here, .BAR) to your own, which can be lowercase or uppercase.


This will tell the computer that when a file of this
extension is to be opened, it should start your program.?á It does not,
however, tell your program to open the file when it starts up.?á To do that,
you need the next bit of code.


The way your program knows what file to open is the Command
Line.?á The command line contains any arguments your program needs?áwhen
it starts.?á In this case, it contains the path of the file to open.?á
The way to access the Command Line is through the keyword "Command".?á It
contains the string that is the file's path.?á So, in the Form_Load
event...




If Command <> "" Then
LoadFile(Command)
End If

One thing about the command line: it only works like this
for compiled EXEs.?á To simulate this in VB's IDE, go to the Project menu,
click Project Properties, then the Make tab, and put in the file path in the
"Command Line Arguments" text box.?á Compile and run.


Now you should be set!?á Start your program, create a
file, save it, exit the program, and double click on the file.?á It should
open in your program.?á If you have any problems, feel free to email
me.


Hope this helped.

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 126 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.