Solved

How to link a file extension to my vb program?

Posted on 2001-06-07
18
149 Views
Last Modified: 2010-05-02
I am making a vb program, I want after I install my vb program, when people double click on one kind of file extension, say ".abc", then it automatic active my program and execute that file. Is that possible?
0
Comment
Question by:zhangjing
  • 6
  • 5
  • 4
  • +1
18 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6162951
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6162955
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6162966
to get whatever parameters are passed to your application, in the form_load procedure go like this:

Private Sub Form_Load()
  MsgBox Command$
End Sub


Command$ is a variable that VB itself creates and fills or whatever. When the user doubleclicks a .abc file, your application will open, and the filename will be in Command$. You just have to check, when the form is loading, whether the user is starting a brand new instance, or just opening a .abc file by double clicking the file. There, how's that for making an easy procedure seem ahrd?
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 100 total points
ID: 6162978
Hi, here is it!

_______________________-

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ?1996-2001 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' You are free to use this code within your own applications,
' but you are expressly forbidden from selling or otherwise
' distributing this source code without prior written consent.
' This includes both posting free demo projects made from this
' code as well as reproducing the code in text or html format.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Public Const REG_SZ As Long = &H1
Public Const REG_DWORD As Long = &H4
Public Const HKEY_CLASSES_ROOT As Long = &H80000000
Public Const HKEY_CURRENT_USER As Long = &H80000001
Public Const HKEY_LOCAL_MACHINE As Long = &H80000002
Public Const HKEY_USERS As Long = &H80000003

Public Const ERROR_SUCCESS As Long = 0
Public Const ERROR_BADDB As Long = 1009
Public Const ERROR_BADKEY As Long = 1010
Public Const ERROR_CANTOPEN As Long = 1011
Public Const ERROR_CANTREAD As Long = 1012
Public Const ERROR_CANTWRITE As Long = 1013
Public Const ERROR_OUTOFMEMORY As Long = 14
Public Const ERROR_INVALID_PARAMETER As Long = 87
Public Const ERROR_ACCESS_DENIED As Long = 5
Public Const ERROR_MORE_DATA As Long = 234
Public Const ERROR_NO_MORE_ITEMS As Long = 259

Public Const KEY_ALL_ACCESS As Long= &H3F
Public Const REG_OPTION_NON_VOLATILE As Long = 0

Public Declare Function RegCloseKey Lib "advapi32.dll" _
   (ByVal hKey As Long) As Long

Public Declare Function RegCreateKeyEx _
    Lib "advapi32.dll" Alias "RegCreateKeyExA" _
   (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal Reserved As Long, _
    ByVal lpClass As String, _
    ByVal dwOptions As Long, _
    ByVal samDesired As Long, _
    ByVal lpSecurityAttributes As Long, _
    phkResult As Long, _
    lpdwDisposition As Long) As Long

Public Declare Function RegOpenKeyEx _
    Lib "advapi32.dll" Alias "RegOpenKeyExA" _
   (ByVal hKey As Long, _
    ByVal lpSubKey As String, _
    ByVal ulOptions As Long, _
    ByVal samDesired As Long, _
    phkResult As Long) As Long

Public Declare Function RegSetValueExString _
    Lib "advapi32.dll" Alias "RegSetValueExA" _
   (ByVal hKey As Long, _
    ByVal lpValueName As String, _
    ByVal Reserved As Long, _
    ByVal dwType As Long, _
    ByVal lpValue As String, _
    ByVal cbData As Long) As Long

Public Declare Function RegSetValueExLong _
   Lib "advapi32.dll" Alias "RegSetValueExA" _
  (ByVal hKey As Long, _
   ByVal lpValueName As String, _
   ByVal Reserved As Long, _
   ByVal dwType As Long, _
   lpValue As Long, _
   ByVal cbData As Long) As Long


Public Sub CreateAssociation()

   Dim sPath As String
   
  'File Associations begin with a listing
  'of the default extension under HKEY_CLASSES_ROOT.
  'So the first step is to create that
  'root extension item
   CreateNewKey ".xxx", HKEY_CLASSES_ROOT
   
   
  'To the extension just added, add a
  'subitem where the registry will look for
  'commands relating to the .xxx extension
  '("MyApp.Document"). Its type is String (REG_SZ)
   SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ
   
   
  'Create the 'MyApp.Document' item under
  'HKEY_CLASSES_ROOT. This is where you'll put
  'the command line to execute or other shell
  'statements necessary.
   CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT
   
   
  'Set its default item to "MyApp Document".
  'This is what is displayed in Explorer against
  'for files with a xxx extension. Its type is
  'String (REG_SZ)
   SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ
   
   
  'Finally, add the path to myapp.exe
  'Remember to add %1 as the final command
  'parameter to assure the app opens the passed
  'command line item.
  '(results in '"c:\LongPathname\Myapp.exe %1")
  'Again, its type is string.
   sPath = "c:\LongPathname\Myapp.exe %1"
   SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ
   
  'All done
   MsgBox "The file association has been made!"
   
End Sub


Public Function SetValueEx(ByVal hKey As Long, _
                           sValueName As String, _
                           lType As Long, _
                           vValue As Variant) As Long

   Dim nValue As Long
   Dim sValue As String
   
   Select Case lType
      Case REG_SZ
         sValue = vValue & Chr$(0)
         SetValueEx = RegSetValueExString(hKey, _
                                          sValueName, _
                                          0&, _
                                          lType, _
                                          sValue, _
                                          Len(sValue))
         
      Case REG_DWORD
         nValue = vValue
         SetValueEx = RegSetValueExLong(hKey, _
                                        sValueName, _
                                        0&, _
                                        lType, _
                                        nValue, _
                                        4)
   
   End Select
   
End Function


Public Sub CreateNewKey(sNewKeyName As String, _
                        lPredefinedKey As Long)

  'handle to the new key
   Dim hKey As Long
   Dim result As Long
   
   Call RegCreateKeyEx(lPredefinedKey, _
                       sNewKeyName, 0&, _
                       vbNullString, _
                       REG_OPTION_NON_VOLATILE, _
                       KEY_ALL_ACCESS, 0&, hKey, result)
   
   Call RegCloseKey(hKey)

End Sub


Public Sub SetKeyValue(sKeyName As String, _
                       sValueName As String, _
                       vValueSetting As Variant, _
                       lValueType As Long)

  'handle of opened key
   Dim hKey As Long
   
  'open the specified key
   Call RegOpenKeyEx(HKEY_CLASSES_ROOT, _
                    sKeyName, 0, _
                    KEY_ALL_ACCESS, hKey)
                   
   Call SetValueEx(hKey, _
                   sValueName, _
                   lValueType, _
                   vValueSetting)
                 
   Call RegCloseKey(hKey)

End Sub
'--end block--'  

http://www.mvps.org/vbnet/code/reg/association.htm
0
 
LVL 2

Author Comment

by:zhangjing
ID: 6166128
Well, I know I can use registery to do that. But think of one people first time install my program, and then he double click on .abc file, what will he get? He still can't make my program running, cause my program is not linked to .abc yet. So the only way to solve that is when they INSTALL my program, the install program already link it. That's most of other program did. I want to do things like that way, not by clicking a button or what else.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6166134
Then, i suggest compile the above codes to a program, then run it during the installation.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7133385
Hi zhangjing,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept JonFish85's comment(s) as an answer.

zhangjing, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 7137084
How this happened, zhangjing?! Do you accept my comments as the answer or JonFish85's? If you do accept my comments as answer, is me deserve a "C"?! Please give a reason here.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7137188
ryancys, you do not need to accept such an insult from zhangjing.  I suggest that you post to Community Support and have them remove this ugly mark from your grading history.  I am sorry that my cleanup efforts have lead to this.

-- Dan
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 7137220
Thanks Dan, yes i will post an request to change the grade at few days later, if zhangjing not give me a reasonable answer.

Thanks to clear up the comments in EE too, Dan. Your effort is appreciated.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 7137222
Opps!

> Thanks to clear up the comments in EE too, Dan. Your effort is appreciated.

should be:

Thanks to clear up the questions in EE too, Dan. Your effort is appreciated.
0
 
LVL 2

Author Comment

by:zhangjing
ID: 7139422
ryancys,

Yes, I do accept your comment as answer, the program works, but it doesn't totally solve the problem, your last line is this: "Then, i suggest compile the above codes to a program, then run it during the installation", right? But that means I have to rewrite the VB install program, of course that's possible, but I don't mean that, why not I make a VB compiler by myself? The reason I post this question here is I want to find some really "Good" solution, not so difficult. Also, for DanRollins, I don't understand why he want me to accept JonFish85's answer, I checked both of your answers very carefully, I found your answer is more "near" the result, but still not "exactly" reach it. So when I give mark, A means Excellent, B means good, C means Average, so I think "C"-"Average" is ok, if the rule here think that "Average" is an "insult", then what is "Reject" mean? Please let DanRollins or which other moderator decide, if you all think that it worth B or A, then please help me to modify it to B or A, and say Sorry to ryancys, I don't want to "insult" anyone.
0
 
LVL 2

Author Comment

by:zhangjing
ID: 7139435
Also, please read my question again: "I want after I install my vb program, when people double click on one kind of file extension, say ".abc", then it automatic active my program and execute that file. Is that possible?" I know all the code that after first run your program, then have form, blah, blah, blah, that's not the point, I want it to associate IN the install program, not AFTER that, most of the program can do that, does Microsoft Office need you to first install it then run it to link .doc to Word? of course not, that's what I want to do. So, maybe if you directly told me: "No, that's impossible to do in VB." That seems truth, then I may give you a "B" or even "A", sometimes, "No" is excellent answer. Sorry.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7139477
zhangjing,
If you want excellent answers from the Experts here, you need to provide clearly-worded questions that express exactly what you need to know.  You then need to provide additional feedback so that the Experts know how to continue in helping you.  If you abandon questions and never respond to the expert comments, and if you then grade with a C without giving the expert a chance to provide a better answer, then the Experts will stop helping you in new questions.

>>I don't understand why he want me to accept JonFish85's answer

Before making my recommendation, I checked JonFish85's first link and it describes in perfect detail how to programmatically associate a file extension with a program.  That answers your original question EXACTLY.  

You then added a new request that it needed to be done BEFORE your program was ever executed.  JonFish85's answer was still exactly correct -- you just need to take the obvious step of doing it in your setup program.

ryancys comment was similar to the code that had already been provided by JonFish85's link.  

So, that is why I recommended JonFish85's comment as an answer.  Do you have any other questions?

-- Dan
0
 
LVL 2

Author Comment

by:zhangjing
ID: 7140159
Dan,

You told me to "finalize it within 7 days." So after compare all answers, I'd like to choose ryancys's comment as answer.

Please read my original question carefully: "I want after I install my vb program, when people double click on one kind of file extension, say ".abc", then it automatic active my program and execute that file. Is that possible?"

If I use JonFish85 and ryancys's answer and code it in, will it have the effect "after I install my vb program, when people double click on one kind of file extension, say ".abc", then it automatic active my program and execute that file"?

Everybody here can try that, it WON'T. So I grade it C, it means "Average" acceptable answer, but not "Bad" answer, right?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7140353
Yes, I have read your question many times now.  There is a subtle mistake in it that has confused the experts.  You said:

   >> ...after I install my vb program, when people double click on one...
but you should have said:
   >> ...DIRECTLY after I install my vb program, BEFORE ANYONE RUNS IT, when people double click on one...

If you had said that then everybody here would have helped you work toward the solution you wanted.  I'm sorry that it did not go well for you.  

>> So I grade it C, it means "Average" acceptable answer, but not "Bad" answer, right?

No.  C is almost never acceptable.  It can ruin an expert's grading history.  Please look at the grading guidelines here:

   http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3

You did not ask for clarification of ryancys's final comment so you have no cause to give a C.
 
-- Dan
0
 
LVL 2

Author Comment

by:zhangjing
ID: 7142239
Yes, you understand my original question. Thank you!

Also, I noticed there is a sentence in the grading guidelines just below "C": You also have the option here of just asking Community Support to delete the question.

I don't think ryancys's answer hit my question, so I can't grade his answer as A or B, so I ask to delete the question.

Sorry to ryancys and JonFish85, that's all I can do.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 7142697
Ok, zhangjing. I accept your comments to explain why i deserve a 'C', i will not post a request on Community Support to change this question's grade. Just be kind next time to award other expert with better grades if you do post some other questions in EE. And what i can say is another 'C' grades in my PAQ question list. Hmmm, so bad sometimes. But as i accept your explaination it is ok.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now