Solved

Associating file types with a .exe

Posted on 1998-06-24
7
251 Views
Last Modified: 2010-05-03
How do you associate a filetype to a certain .exe file made using vb and a certain icon?  What do I have to add to the setup program?
0
Comment
Question by:wsanchez
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:yowkee
ID: 1464148
wsanchez,

  Basically to associate a extension with your application, you must have appropriate information in the Registry. For example, your application name test.exe and stay in folder C:\temp, the extension to associate is .aaa. Then, in your registry, these key must existed:
(HKCR = HKEY_CLASSES_ROOT)

[HKCR\.aaa]
@Defaule="aaafile"

[HKCR\aaafile\shell\open\command]
@Default="C:\temp\test.exe %1"

' And the default icon for the file with .aaa
[HKCR\aaafile\DefaultIcon]
@Default="C:\temp\test.exe,1"   (this mean first icon contain in                                  test.exe)


Use regedit.exe to open the registry and looking for HKCR\.htm, HKCR\htmlfiles and you will know how to do it.

For setting this info, you must modify your setup wizard program, to add stuff for insert info into registry in the startup of setup.

For reference, look for MS KB Q185453. I think the PAQ also got some similiar question.

Regards
0
 
LVL 4

Expert Comment

by:yowkee
ID: 1464149
wsanchez,

  Basically to associate a extension with your application, you must have appropriate information in the Registry. For example, your application name test.exe and stay in folder C:\temp, the extension to associate is .aaa. Then, in your registry, these key must existed:
(HKCR = HKEY_CLASSES_ROOT)

[HKCR\.aaa]
@Defaule="aaafile"

[HKCR\aaafile\shell\open\command]
@Default="C:\temp\test.exe %1"

' And the default icon for the file with .aaa
[HKCR\aaafile\DefaultIcon]
@Default="C:\temp\test.exe,1"   (this mean first icon contain in                                  test.exe)


Use regedit.exe to open the registry and looking for HKCR\.htm, HKCR\htmlfiles and you will know how to do it.

For setting this info, you must modify your setup wizard program, to add stuff for insert info into registry in the startup of setup.

For reference, look for MS KB Q185453. I think the PAQ also got some similiar question.

Regards
0
 
LVL 7

Accepted Solution

by:
kamall earned 50 total points
ID: 1464150
You will put this code in your EXE and execute it only the first time your program runs. You can determine if it is the first time by creating a simple INI file on the user machine.

The following code will put an association to .xyz to the file XYZ:

    In your declarations section:
    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 = &H80000000
    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 "XYZ".
      sKeyName = "XYZ"
      sKeyValue = "XYZ App"
      ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
      ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
       
      'This creates a Root entry called .xyz associated with "XYZ".
      sKeyName = ".xyz"
      sKeyValue = "XYZ"
      ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
      ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
       
      'This sets the command line for "XYZ".
      sKeyName = "XYZ"
      sKeyValue = "c:\XYZ\XYZ.exe %1"
      ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&)
      ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, sKeyValue, MAX_PATH)

Regards
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:wsanchez
ID: 1464151
To kamall.  What do I write in the INI file? Sorry for asking but I'm quite new at this.  Also, what if the person who installs the software gives a different installation path during installation?  How would I reflect the path in the registry?
0
 
LVL 7

Expert Comment

by:kamall
ID: 1464152
wsanchez,
Sorry for the delay in reponding, but I didn't get e-mail notification of your comment. I saw your other question 'To kamall...' which led me here.
So, the solution to your question is very simple.
You can just create an empty INI file (or write anything you like) when your program runs for the first time, and put it in the application's directory. You can simply get the application directory using the App.Path
Now when your program runs, you simply check whether your INI file exists. If not, then it is the first time, otherwise it is not.
This will not cause you any problems if the user installs your application in any other directory name.
Regards
0
 

Author Comment

by:wsanchez
ID: 1464153
Thanks Kamall.
0
 
LVL 7

Expert Comment

by:kamall
ID: 1464154
You are welcome wsanchez.
For any more questions, leave me a comment.
Best regards.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 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

10 Experts available now in Live!

Get 1:1 Help Now