From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to associate a file type (extension) with my application?

I'd like to have my distribution kit associate a file type with my application, so when a user double-clicks on a file with that extension in Windows, it launches my app and passes the file spec as an argument. (My app would then open this data file and just read its contents.)

How can I accomplish this?

(If the procedure to build the distribution kit is complex, is there a batch/script/.reg file that I could create to allow the user to make the association once the app is installed?)

Thanks,
Ian
0 Kudos
Message 1 of 11
(5,124 Views)
Hello,

All of what you ask is done through the registry. To add this to a Distribution Kit you must add a .reg file to you distribution and have a batch file run it, or edit the distribution after it is built using a third party tool (both of these options are explained further in the Application Note for the DistKit : [CVIDIR]\bin\cdk_mod.pdf).

The layout of association registry keys is not too complicated. First you create an entry for the file extension (we are going to use .myf for Myfile). In this entry you specify what you want to call the file.

HKLM\Software\Classes\.myf (Default) Myfile

Then you create an entry for Myfile with all the necessary information for launching.

HKLM\Software\Classes\Myfile (Default) My wonderful file type
HKLM\Software\Classes\Myfile\shell\open\command (Default) "[MyAppDir]myapp.exe" "%1"

You can add whatever is necessary through the command line such as -o to open or whatever you have written. You can also assign the file an icon here.

HKLM\Software\Classes\Myfile\DefaultIcon (Default) [MyAppDir]myapp.exe,1


The use of quotes and such is the tricky part. My suggestion is to go into the registry on you development machine and author the keys until they work. Then export them or copy them into your distribution. If you are including this in you distribution you need to be sure to include the Directory table entry that corresponds to the location of the install, I used [MyAppDir] above as an example.

Jeff
NI
Message 2 of 11
(5,106 Views)
Sorry but the post removed white space so that it is hard to tell what the registry keys are supposed be.

They are in the form of:
KEY[space(s)]Name[space(s)]value

Hopefully this is not to confusing. If it is send me your email address and I will send you a better replica of what this is supposed to look like.

Jeff
0 Kudos
Message 3 of 11
(5,108 Views)
Thanks, Jeff, for the great answer.

I've no problem with the syntax, and that part of the registry is full of examples, too.

--Ian
0 Kudos
Message 4 of 11
(5,090 Views)
In the course of implementing this, I've come across some unwanted behavior.

When I specify an icon for my data file with this:

HKLM\Software\Classes\Myfile\DefaultIcon (Default) [MyAppDir]myicon.ico,0

my data files have the proper icon in Windows explorer. Great!

But, if I associate the data file with my app like this:

HKLM\Software\Classes\.myf (Default) Myfile
HKLM\Software\Classes\Myfile (Default) My wonderful file type
HKLM\Software\Classes\Myfile\shell\open\command (Default) "[MyAppDir]myapp.exe" "%1"

then the icon for my data file becomes a squished version of the icon for myapp.exe placed within an image of a paper page, complete with folded corner. Is there a way to prevent this clever "documentifying" of my data file's icon, and stick with the icon I specified?

Thanks in advance,
Ian
0 Kudos
Message 5 of 11
(4,935 Views)
Hello,

You just have to include all keys listed. You must include the DefaultIcon key along with the other keys to have it use that icon. Otherwise Windows tries and saying that the EXE will open this type and create that funky paper icon thingy.

Jeff
NI
Message 6 of 11
(4,922 Views)
That did it. Thanks for the help, Jeff!
0 Kudos
Message 7 of 11
(4,884 Views)

Reviving an old thread here ... does anyone know if the techniques in this thread are still compatible with Windows 10? 

 

(It seems to be more fussy about file associations and registry changes than older versions of Windows.)

 

Thanks!

0 Kudos
Message 8 of 11
(3,629 Views)
Then you create an entry for Myfile with all the necessary information for launching.

<snip/>

You can add whatever is necessary through the command line such as -o to open or whatever you have written. 

Jeff

I too am resurrecting an old thread.  This is a great starter for me, but I'm stumbling on this little bit.  What kind of event is generated when my application starts up to know that a file is being passed to it?   I'm confused about how to actually get the file piped into my app.

 

For instance, in Windows there's a "poor man's" approach to all of the above by simply using the Shift + Right Click context menu on the file in question.  This gives us extra options, including an "Open with...".2018-01-12 06_58_04-.png

 

 

Having done that for my file and associating it with my CVI app, my app will indeed load but nothing actually happens.  The file doesn't get loaded.

0 Kudos
Message 9 of 11
(3,103 Views)

No particular event is generated in this case, but your program receives an additional startup parameter (as if you have called it with e.g. "myprog.exe myfile.txt"). You retrieve this in argv[] parameter of the main(), signalled by the fact that argc parameter is >1 (argv[0] is always present holding actual program name).

 

In the case of automatic program launch in association with a particular file extension your main will be called with:

argc = 2

argv[1] = complete file pathname

 

It is your responsibility to detect this fact and actually decide what to do with the file.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 10 of 11
(3,098 Views)