Home All Groups Group Topic Archive Search About

How to create a VB6 app that opens a file when the file is double

Author
14 Jun 2009 2:05 PM
TonyScarpelli
I'd like to create a vb6 application that will run when I click on a file.

When I click on a file, say a graphics file, with a certain extension, I
want a visual basic 6 .exe to open the file and process it.

This means I have to pass the file directory and name to the application and
I then show the file in a form.

I don't want to use any of the standard applications since I want to do this
myself.

Any ideas on how to do this?

Thanks.

Author
14 Jun 2009 3:07 PM
Wolfgang Enzinger
On Sun, 14 Jun 2009 07:05:01 -0700, TonyScarpelli wrote:

>I'd like to create a vb6 application that will run when I click on a file.
>
>When I click on a file, say a graphics file, with a certain extension, I
>want a visual basic 6 .exe to open the file and process it.
>
>This means I have to pass the file directory and name to the application and
>I then show the file in a form.

First you'd have to register the respective file type. You can do this
either in the respective Explorer dialog, or programmatically with this
code:

***************

(see the API viewer for missing declarations, or get them here:
http://www.enzinger.net/FileAsso.html)

Function AssocFileType(ByVal Extension As String, _
                       ByVal Identifier As String, _
                       ByVal Description As String, _
                       ByVal ApplicationPath As String) As Boolean

'*** Parameters:
' Extension:        extension to be registered, e.g. "TXT" / ".TXT"
' Identifier:       unique identifier for file type, e.g. "txtfile"
' Description:      description for file type, e.g. "Text File"
' ApplicationPath:  path to your application, e.g. "c:\myprog.exe"

Dim lphKey As Long, l As Long, s As String

If Left$(Extension, 1) <> "." Then
  Extension = "." & Extension
End If

If RegCreateKey(HKEY_CLASSES_ROOT, Extension, lphKey) = 0 Then
  If lphKey Then
    If RegSetValue(lphKey, "", REG_SZ, Identifier, 0&) = 0 Then
      l = RegCloseKey(lphKey)
      l = RegCreateKey(HKEY_CLASSES_ROOT, Identifier, lphKey)
      l = RegSetValue(lphKey, "", REG_SZ, Description, 0&)
      s = ApplicationPath & " ""%1"""
      l = RegSetValue(lphKey, "shell\open\command", REG_SZ, s, Len(s))
      s = ""
      l = RegSetValue(lphKey, "shell", REG_SZ, s, Len(s))
      s = ApplicationPath & ",0"
      l = RegSetValue(lphKey, "DefaultIcon", REG_SZ, s, Len(s))
      SHChangeNotify SHCNE_ASSOCCHANGED, 0&, 0&, 0&
      AssocFileType = True 'signal success
    End If
    l = RegCloseKey(lphKey)
  End If
End If
End Function

***************

Note that the above function requires sufficient write permissions in
the registry in order to work properly; it should therefor be run during
the installation process.

Then, all your program has to do is this:

***************

Sub Main()
Dim s As String
s = Command$()
If Len(s) > 2 Then
  If Left$(s, 1) = """" Then
    If Right$(s, 1) = """" Then
    'trim doublequotes
    s = Mid$(s, 2, Len(s) - 2)
    End If
  End If
End If

If Len(s) Then
  If FileExists(s) Then
  Open ...
  End If
End If
End Sub

***************

HTH,
Wolfgang
Author
14 Jun 2009 3:51 PM
mayayana
For some reason Microsoft's wack spam filter blocked
my response to this, so I'm posting it with extra spaces.

    I f   t h a t   f i l e   e x t e n s i o n   i s   s e t   t o   h a v
e   y o u r
E X E   a s   t h e   c o m m a n d   h a n d l e r   ( d e f a u l t
p r o g r a m )   t h e n   y o u r   E X E   g e t s   t h e   c o m m a n
d
l i n e .   Y o u   c a n   c h e c k   i t   i n   F o r m _ L o a d :

D i m   s   a s   S t r i n g
s   =   C o m m a n d $

( O n   W i n 9 x   t h a t   r e t u r n s   a   D O S   p a t h ,   w h i
c h   c a n
b e   f i x e d   b y   u s i n g   G e t L o n g P a t h N a m e . )

    I n   o r d e r   t o   g e t   t h e   c o m m a n d   s t r i n g   f
r o m   E x p l o r e r
w h e n   t h e   f i l e   i s   d o u b l e - c l i c k e d ,   y o u   n
e e d   t o   s e t   t h e
f o l l o w i n g   R e g i s t r y   v a l u e s   ( p r e s u m a b l y
w i t h   p e r m i s s i o n ) :

I f   t h e   f i l e   e x t e n s i o n   w e r e   . x y z   -

C r e a t e   k e y   H K C R \ . x y z \
S e t   d e f a u l t   v a l u e   t o   a   s e l e c t e d   c l a s s
n a m e ,   l i k e   " x y z f i l e "

C r e a t e   k e y   H K C R \ x y z f i l e \ S h e l l \ O p e n \ C o m
m a n d \
S e t   d e f a u l t   v a l u e   t o :   Y o u r E X E P a t h   % 1

A l s o ,   t h e   d e f a u l t   v a l u e   u n d e r   t h e   H K C R
\ x y z f i l e \   k e y
i s   t h e   n a m e   t h a t   s h o w s   i n   t h e   r e g i s t e r
e d   f i l e s   l i s t .

    Y o u   c a n   s e e   h o w   i t   w o r k s   b y   l o o k i n g
u p   a   s i m p l e
e x t e n s i o n   l i k e   . t x t .

    T h a t   d e s i g n   i s   w h a t   a l l o w s   p r o g r a m s
t o   e a s i l y   s t e a l
e x t e n s i o n s .   I n   t h e   o l d   b a t t l e   b e t w e e n
I E   a n d   N e t s c a p e
t h e y   w o u l d   b o t h   s t e a l   t h e   H T M L   e x t e n s i
o n .   A l l   t h e y
h a d   t o   d o   w a s   t o   r e d e f i n e   t h e   " c l a s s "
n a m e   u n d e r
H K C R \ . h t m l   t o   p o i n t   t o   t h e i r   c l a s s   k e y
,   b e c a u s e   t h a t ' s
h o w   E x p l o r e r   f i n d s   f i l e   e x t e n s i o n   d e f a
u l t s .   T h e n   u n d e r   t h e i r
o w n   c l a s s   k e y   t h e y   c o u l d   p u t   t h e   k e y s
S h e l l \ O p e n \ C o m m a n d \ ,
p r i n t \ ,   e d i t \ ,   D e f a u l t I c o n \ ,   e t c .

      Y o u   d o n ' t   n e e d   a l l   o f   t h o s e ,   b u t   y o
u   n e e d   a t   l e a s t   t h e
S h e l l   k e y .   Y o u   c a n   a l s o   s e t   t h e   D e f a u l
t I c o n   w i t h   t h a t
k e y ,   t h o u g h   i t   s e e m s   t o   w o r k   O K   i f   y o u
d o n ' t .   W i n d o w s
w i l l   j u s t   p u t   a   s m a l l   v e r s i o n   o f   y o u r
p r o g r a m   i c o n   i n s i d e   a
p l a i n   d o c u m e n t   i c o n .

      O f t e n   t h e   c l a s s   n a m e   i s   c o m m e r c i a l .
F o r   i n s t a n c e ,   y o u
m i g h t   s e t   t h e   d e f a u l t   v a l u e   o f   H K C R \ . x
y z   a s   " S c a r p e l l i X Y Z F i l e " .
I t ' s   u p   t o   y o u .   T h a t   d e s i g n   h a s   t h e   a d
v a n t a g e   o f   b e i n g   u n i q u e .
F o r   i n s t a n c e ,   I   l e t   I r f a n V i e w   b e   i n   c h
a r g e   o f   a l l   i m a g e   f i l e s .
I f   I   l o o k   u n d e r   t h e   . j p g   k e y   I   f i n d   a
c l a s s   n a m e   " I r f a n V i e w . J P G " .
I f   y o u   c r e a t e   t h e   k e y   H K C R \ S c a r p e l l i i X
Y Z F i l e \   t h e n   y o u r   k e y
c a n   b e   a d d e d   t o   H K C R   w i t h o u t   o v e r w r i t i
n g   t h e   g e n e r i c   k e y
x y z f i l e ,   i f   a l r e a d y   t h a t   e x i s t s .

        I n   t h e   e x a m p l e   o f   . T X T ,   i f
I   a d d   a   k e y   H K C R \ t x t f i l e \   t h e n   I ' l l   o v
e r w r i t e   t h e   s t a n d a r d
N o t e p a d   a s s i g n m e n t   k e y s .   I f   i n s t e a d   I
a d d   H K C R \ S c a r p e l l i T e x t F i l e \
t h e n   t h e   t x t f i l e   k e y   i s   l e f t   u n d i s t u r b
e d   i n   c a s e   t h e   p e r s o n   w a n t s
t o   g e t   i t   b a c k .   ( O r   i f   I   w a n t   t o   p r o v i
d e   t h e   o p t i o n   t o   r e v e r s e   t h e
c h a n g e ,   i n   w h i c h   c a s e   a l l   I ' d   h a v e   t o
d o   i s   o v e r w r i t e   t h e   d e f a u l t
v a l u e   i n   H K C R \ . t x t   w i t h   t h e   o n e   t h a t   w
a s   t h e r e   i n   t h e   f i r s t
p l a c e :   " t x t f i l e "   )