Registering Windows file types with NSIS

8 October 2006 (programming windows) (2 comments)

While working on a Windows installer for Guikachu, I found the need to register .guikachu files, so that they can be opened by double-clicking on them. Read on for instructions (with sample NSIS script snippets) on how to create new file types on Windows.

First of all, Windows stores file type information separately from the extension->filetype mapping, which is a good thing – this way, you can register several file extensions to mean the same file type. File extensions are registered by creating a key in HKEY_CLASSES_ROOT with the extension (including the leading dot), and setting the default field to the file type.

WriteRegStr HKCR ".mtxt" "" "MyTextEditor.Document"

Like noted above, you can register any number of file extensions for a given file type. Now that in our example, .mtxt is registered, it is time to define the MyTextEditor.Document file type.

File type definitions go into the same HKEY_CLASSES_ROOT hierarchy as file extensions. File types contain information for the shell (i.e. Windows Explorer) and definitions of document operations. So let's first create our file type and give it a nice user-visible name, and set up its icon as well:

WriteRegStr HKCR "MyTextEditor.Document" "" \
      "MyText Document"
WriteRegStr HKCR "MyTextEditor.Document\DefaultIcon" "" \

The ,1 above means to use the second icon from the mytextedit executable. For this purpose, icons are ordered by their resource ID's, so even if you have two icons with ID's 100 and 105, their index will be 0 and 1 (and not 100 and 105), respectively. Note how the NSIS variable $INSTDIR is used to point to the right location of the executable.

OK now for the actions. Each action has an ID, an optional user-visible name, and an associated command. Actions are created as keys under HKEY_CLASSES_ROOT/FileType/shell. The user-visible name of the action is stored in the default field of the key, and the command sub-key stores the command to run (with %1 substituted with the file name). The default command is open, or whatever you set the default field of shell to. So here's an example defining two actions:

WriteRegStr HKCR "MyTextEditor.Document\shell\open\command" "" \
      '"$INSTDIR\mytextedit.exe" "%1"'
WriteRegStr HKCR "MyTextEditor.Document\shell\print\command" "" \
      '"$INSTDIR\mytextedit.exe" /p "%1"'

Whenever applicable, try to use the standard actions without specifying a user-visible command name. This way, Windows can display actions in menus in whatever language the user has set up.

As a final note, yes, there are more sophisticated ways to implement actions, by using DDE or COM messages. However, if you're creating a straight Windows port of some Unix software, like I was doing, the above should be enough to register your file types with Windows.

« 11 October 2006 
All posts
 RSS feeds to ease transition from Advogato »

sitesupport ( 2007-01-15 15:12:27

Nice article!

But extend not all of features of NSIS. I'm looking for description how to set icon for registered file type with NSIS. Could somebody help me?

cactus 2007-01-15 16:17:54

That's exactly what the DefaultIcon key is for. See the second line of the second code snippet.