Assigning a file type to an Application on Windows

From Qt Wiki
Revision as of 14:00, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Assigning a file type to an Application on Windows

Background / technical stuff

File types in windows are defined in the Windows registry. First there is the entry of “<extension>” in HKCR which contains the file type name. In HKCR\<file type name> the different actions are defined.

This means, there is a file extension gidoc, whose files are called “MDI Text Editor Document”. These documents should be opened in an application, which is located here: E:\Developement\projects\debug\SimpleCryptIoDevided.exe. The file itself should be a parameter.

Using this pattern, each double click on a file opens a new Process which gets the file as parameter. This is fine for SDI (Single Document Interface) applications.

Example below:

For MDI (Multi Document Interfaces), it should be opened in the existing Process. To achieve this, windows uses the old DDE mechanism. There must be some additional entries to achieve this:

If this is also added, Windows starts the process, which is given in command and then sends a WM_DDE_INITIATE broadcast message to windows. The application must react on this, then handle WM_DDE_EXECUTE messages and, if requested, WM_DDE_TERMINATE message. If the reaction on the DDE messages is not correc, Windows displays some error messages.

How to do it

Normally, your application registers the file types (changes the registry) during startup. If the DDE Mdi stuff should be used, it then has to create some ATOMS (::GlobalAddAtom). Then it starts up the rest and waits fo user input.

If the file type is registered, and no dde is set, the application is started with the file path as parameter.

If ddeexec keys are set in the registry, the application is started and the the DDE messages are send. While handling these messages, WM_DDE_ACK messages are send back to the caller and the messages must be handled.

This behaviour and the code would always look the same, only the registration keys and the handling of the DDE commands differ from application to application. There fore I created a class, that does all this and can be used as base class instead of QMainWindow: DocumentWindow.

Use the DocumentWindow class

If you want to create an (MDI) editor application, you can derive your main window class from DocumentWindow instead of QMainWindow. If you implement an MDI application, you should also reimplement the virtual method

ddeOpenFile

.
The example code is based on the Qt MDI Example [doc.qt.nokia.com]

all other things that need to be done is extending your constructor

and reimplement ddeFileOpen

That’s it. Now it should work to just start the application, and the file type “.gidoc” is registered with your application. If you double click on it in the Windows explorer, it should be opened in the currently running Process or, if no instance of your application is running, start a new one. If your application has a windows icon in the resource (not in the qrc file! a windows resource file <file>.rc), this icon should be used for the files.

Use the DocumentWindow sources

DocumentWindow.h

DocumentWindow.cpp

You may use this code without any warranty.

Future extensions

The class might be extended so it also works on Linux or Mac. The code of the class and a demo project are located on gitorious in the following repository: qtdevnet-registereditorfiletype [gitorious.org]

Versions

The current code is version 1 of the code.

Version 1

This marks the first public release.

Feedback

I would really welcome feedback on this class. If you spot big weaknesses, please let me know. For discussion on this wiki, please use this forum thread [developer.qt.nokia.com] .

Categories: