Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Linking and opening a file using its extension

Crazy JimCrazy Jim Member Posts: 18
I can create a program (e.g. myProgram.exe)that generates certain files with a certain extension, e.g filename.myExt.
Next I can link this extension *.myExt (using explorer) to myProgram.exe

Two questions:
1. When I click on a file 'someName.myExt' then myProgram.exe will start, this is alright, but I also want to have this file opened/read into the program automatically.
How to do this?

2. I can link an extension, using explorer, but I want to link the extension using code.
How to do this?

To those helpfull:
Lots of thanks!

Comments

  • KDivad LeahcimKDivad Leahcim Member Posts: 3,948
    : 1. When I click on a file 'someName.myExt' then myProgram.exe will start, this is alright, but I also want to have this file opened/read into the program automatically.
    : How to do this?
    :

    When your app starts, check the value of Command$. It holds the command line parameters passed to your program, including the name of the file clicked.

    : 2. I can link an extension, using explorer, but I want to link the extension using code.
    : How to do this?
    :

    Look into the Registry APIs (they all start with Reg). It's not the easiest thing to work with, but it's not too bad. There are also various objects floating around that can make registry editing easier but I've never used any of them. The basic format of the reg keys you'll need to set up is the following (txt for this example)

    HKCR.txt
    Default="txtfile"
    HKCR xtfile
    Default="Text Document"
    HKCR xtfileDefaultIcon
    Default="path to icon file, index of icon in file"
    HKCR xtfileshell
    Default(optional)="Default subkey for doubleclick"
    HKCR xtfileshellopen
    Default(optional)="&Open" 'Friendly text for context menu
    HKCR xtfileshellopencommand
    Default="path to exe and any additional parameters"

    For "additional parameters", you can use %1 (one) to indicate the filename, %l (lowercase or uppercase L) will give you the long file name. Quotes can be used to surround the filename, etc. An example is from the old Windows Media Player:

    mplayer.exe open %1

    You'll need the following APIs and constants (plus any additional constants or UDTs referenced by these):

    RegOpenKey
    RegCloseKey
    RegSetValue

    REG_SZ 'string data type

    REG_ALL_ACCESS

    Those basics will allow you to create the association. Any more (such as checking for prior associations to keep your app from breaking another app) will also require RegQueryValue and RegEnumKey. You may also need to use the *Ex versions of these APIs (RegSetValueEx instead of RegSetValue), I don't recall which ones will or won't work for some things on Win32. Win16 only had the HKCR hive and only had one value per key. Win32 allows much more but the APIs are also more complex.

    Good luck!
Sign In or Register to comment.