Solved

Associating a file extension to an application.

Posted on 1998-06-02
7
174 Views
Last Modified: 2010-05-03
In Visual Basic 5, how do you associate a file extension with an applications.  For example, how would I associate all *.ksm files with c:\myapp.exe ?

Thanx.
0
Comment
Question by:ksm
  • 4
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
kamall earned 50 total points
ID: 1462297
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&

    Const HKEY_CLASSES_ROOT = 1
    Const MAX_PATH = 256&
    Const REG_SZ = 1

    The code to write to the registry:
      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 "XXX".
      sKeyName = "XXX"
      sKeyValue = "XXX App"
      ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
      ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
       
      'This creates a Root entry called .XXX associated with "XXX".
      sKeyName = ".XXX"
      sKeyValue = "XXX"
      ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
      ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
       
      'This sets the command line for "XXX".
      sKeyName = "XXX"
      sKeyValue = "c:\XXX\XXX.exe %1"
      ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
      ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, sKeyValue, MAX_PATH)

0
 

Author Comment

by:ksm
ID: 1462298
kamall:

Do you have to make ALL 3 of those calls for each association?
0
 
LVL 7

Expert Comment

by:kamall
ID: 1462299
Thats right ksm.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:ksm
ID: 1462300
kamall:

Sorry to take so long to get back to you.

I just tried your example and it did not work.  I didn't get an error.  But I didn't see any new entries in the registry.  I ran the program, ensured that I was executing the calls above and nothing happened.  I'm using VB5 under WinNT.

Any suggestions?

Kevin
0
 
LVL 7

Expert Comment

by:kamall
ID: 1462301
ksm,
Sorry for being away for a long time.
Regarding your comment, the code I gave you works fine in Windows 95 and it is tested. What I can guess is that the problem is the NT platform. There might be some different settings to take into considaration.

Regards
0
 

Author Comment

by:ksm
ID: 1462302
The registry settings between Win95 and WinNT 4 are similar.  So the code should work in both OS's w/o having to modify it.  I got a hold of some more code and it seems to be working (got it from the MS KB site, of all places).  So your code should have worked.  I'll compare the MS stuff and the code you gave to me and get back to you shortly.

Thanks.
0
 
LVL 7

Expert Comment

by:kamall
ID: 1462303
ksm,
Many people (me too) had problems so far on Windows NT with their developed applications that work perfectly on Windows 95. These problems do arise especially when using API calls. In most cases, no errors are reported by the NT OS.
I hope that the code you got will work, and I will be happy if you inform me of the results and show me the differences with the code I gave you.
I will be waiting for you.

Regards.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

860 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