- File Types and File Associations
- Additional Resources
- File Types
- Public and Private File Types
- Registering a File Type
- Setting Optional Subkeys and File Type Extension Attributes
- Deleting Registry Information During Uninstallation
- File Types That Support Open Metadata
- Assigning a file type to an Application on Windows
- Contents
- Background / technical stuff
- How to do it
- Use the DocumentWindow class
- Use the DocumentWindow sources
- DocumentWindow.h
- GetFileType function (fileapi.h)
- Syntax
- Parameters
- Return value
- Remarks
- Windows registry — register an application to open a file type
- 2 Answers 2
File Types and File Associations
This section on file types and file associations is organized as follows:
Additional Resources
- Set Program Access and Computer Defaults (SPAD) is a Windows Control Panel which allows users with administrative privilege to set a machine default and hide or show an application. Media, Mail, Browser, Messenger and Java applications are examples of applications registered in SPAD. Set Your Default Programs (SYDP) is a Windows Control Panel, that works with limited privileges, and permits users to set a user default. Any application can register in SYDP. For information about SPAD and SYDP application registration, see Guidelines for File Associations and Default Programs, and Set Program Access and Computer Defaults (SPAD).
- For related conceptual background, see Overview of Verbs and File Associations.
- To create a Shell data store, see Implementing the Basic Folder Object Interfaces.
For related reference documentation, see the following topics:
- To execute a verb on a Shell item, see the InvokeVerb method.
- To retrieve a collection of verbs that can be executed on a Shell item, see the Verbs method.
- For performing an operation on a specified file, see either the ShellExecute or ShellExecuteEx functions.
- For a list of default perceived types, see the PERCEIVED enumeration.
- To retrieves a file’s perceived type based on its extension, see the AssocGetPerceivedType function.
File Types
This topic explains how to create new file types and how to associate your app with your file type and other well-defined file types. Files with a shared common file name extension (.doc, .html, and so on) are of the same type. For example, if you create a new text editor, then you can use the existing .txt file type. In other cases, you might need to create a new file type.
This topic is organized as follows:
Additional information can be found on the following topics:
Public and Private File Types
Public file types are also known as popular or contentious types because competing applications might want to be associated with these file types. Characteristics of public file types include:
- They are typically defined by standards bodies, and/or are promoted by their defining organizations as interchange formats.
- They are often exchanged between computers and users for diverse purposes.
- They need to be supported on many different platforms.
- Applications from multiple vendors are likely to handle them.
Some examples of file types that are considered public are the image file types .png, .gif, .jpg, and .bmp, and the audio types .wav, .mp3, and .au.
Unlike public file types, private or proprietary file types typically have a format that is implemented and understood by only one application or vendor. As a result, private file types are typically not prone to conflicts between applications. Some file types can start as private file types but later become public file types.
Windows does not differentiate between public and private file types. The distinction is relevant only in making decisions about your choice of file type registration.
Registering a File Type
To associate the file type with an existing application, locate the application ProgID in the registry. To associate the file type with a new application, define a ProgID for your application. For information about defining a new ProgID, see Programmatic Identifiers.
File name extension subkeys have the following general form: extension=ProgID. File name extension subkeys are stored in the HKEY_CLASSES_ROOT subtree.
It is important to include the leading period (.) when creating file type subkeys in the registry. For example, if you want a file type with the short extension .myp and the long extension .myp-file to be opened with an application called MyProgram, use the following syntax:
As demonstrated in the preceding example, if you also register a short file name extension (.myp), you should create a subkey for the long extension (.myp-file) as well. For more information, see File Type Handlers.
Setting Optional Subkeys and File Type Extension Attributes
File type extension entries in the registry have several optional subkeys and attributes.
The file type extension entries that are used by file associations are described in the following table. All values are of the REG_SZ type.
Registry entry | Action |
---|---|
Default | Set the default value of the extension subkey to the ProgID to which it is linked. |
Content Type | Set the Content Type value to the file type’s MIME content type. |
OpenWithList | Do not use. This subkey contains one or more application subkeys for applications that appear in the Open with dialog box entry for the file type and is intended only for .exe applications on operating systems prior to WindowsВ XP. Use OpenWithProgIds instead. |
OpenWithProgIds | This subkey contains a list of alternate ProgIDs for this file type. The programs for these ProgIDs appear in the Open with menu and are available as default Windows Store apps for the file type. Whenever an application takes over this file type by changing the default value, it should also add an entry to this list. |
PerceivedType | Set the PerceivedType value to the PerceivedType to which the file belongs, if any. This string is not used by Windows versions prior to WindowsВ Vista. For more information, see Perceived Types and Application Registration. |
The general form of a file name extension subkey is as follows. All entry types are of the REG_SZ type.
Important considerations about file types include:
The HKEY_CLASSES_ROOT subtree is a view formed by merging HKEY_CURRENT_USER\Software\Classes and HKEY_LOCAL_MACHINE\Software\Classes
In general, HKEY_CLASSES_ROOT is intended to be read from but not written to. For more information, see the HKEY_CLASSES_ROOT article.
To register a file type globally on a particular computer, create an entry for the file type in the HKEY_LOCAL_MACHINE\Software\Classes subkey.
To make a file type registration visible to the current user only, create an entry for the file type in the HKEY_CURRENT_USER\Software\Classes subkey.
An application can provide its own implementation of a verb, such as open or play, as shown in the following registry example.
Subkeys of the verb subkey include the command line and the drop target method: command and DropTarget.
When you create or change a file association, it is important to notify the system that you have made a change. Do so by calling SHChangeNotify and specifying the SHCNE_ASSOCCHANGED event. If you do not call SHChangeNotify, the change may not be recognized until after the system is rebooted.
To retrieve registry information regarding a file association, use the IQueryAssociations interface. For a scenario that illustrates this procedure, see File Association Sample Scenario.
Both the App Paths and Applications registry subkeys are used to register and control the behavior of the system on behalf of applications. For more detailed information about this functionality, see Application Registration.
Deleting Registry Information During Uninstallation
When uninstalling an application, the ProgIDs and most other registry information associated with that application should be deleted as part of the uninstallation. However, applications that have taken ownership of a file type (by setting the Default value of the file type’s HKEY_CLASSES_ROOT\.extension subkey to the ProgID of the application) should not attempt to remove that value when uninstalling. Leaving the data in place for the Default value avoids the difficulty of determining whether another application has taken ownership of the file type and overwritten the Default value after the original application was installed. Windows respects the Default value only if the ProgID found there is a registered ProgID. If the ProgID is unregistered, it is ignored.
Note that other file-type ownership information is stored in the HKEY_CURRENT_USERsubtree and also is used only when the application that it references is registered. Therefore, this data does not need to be removed when uninstalling an application.
As an example, the following shows the state of the registry before an application is uninstalled:
The following shows the state of those same registry entries after the application has been uninstalled.
File Types That Support Open Metadata
In WindowsВ 7 and later, the following file types support open metadata.
Assigning a file type to an Application on Windows
En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh
Contents
Background / technical stuff
File types in windows are defined in the Windows registry. First there is the entry of » » in HKCR which contains the file type name. In HKCRamp;lt;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:.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.
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
class MainWindow : public DocumentWindow < … protected:
all other things that need to be done is extending your constructor
MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags flags) :
and reimplement ddeFileOpen
void MainWindow::ddeOpenFile(const QString& filePath) <
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 .rc), this icon should be used for the files.
Use the DocumentWindow sources
DocumentWindow.h
- ifndef WIDGET_H
- define WIDGET_H
registerFileType(«GiMdi.Document», «MDI Text Editor Document», «.gidoc», 0, true);
class DocumentWindow : public QMainWindow <
GetFileType function (fileapi.h)
Retrieves the file type of the specified file.
Syntax
Parameters
A handle to the file.
Return value
The function returns one of the following values.
Return code/value | Description |
---|---|
FILE_TYPE_CHAR 0x0002 | The specified file is a character file, typically an LPT device or a console. |
FILE_TYPE_DISK 0x0001 | The specified file is a disk file. |
FILE_TYPE_PIPE 0x0003 | The specified file is a socket, a named pipe, or an anonymous pipe. |
FILE_TYPE_REMOTE 0x8000 | Unused. |
FILE_TYPE_UNKNOWN 0x0000 | Either the type of the specified file is unknown, or the function failed. |
В
You can distinguish between a «valid» return of FILE_TYPE_UNKNOWN and its return due to a calling error (for example, passing an invalid handle to GetFileType) by calling GetLastError.
If the function worked properly and FILE_TYPE_UNKNOWN was returned, a call to GetLastError will return NO_ERROR.
If the function returned FILE_TYPE_UNKNOWN due to an error in calling GetFileType, GetLastError will return the error code.
Remarks
In WindowsВ 8 and Windows ServerВ 2012, this function is supported by the following technologies.
Windows registry — register an application to open a file type
I’m making an installer for an application which has a custom URI scheme and its own file type to open with it. The application executed with a launcher.bat , In the registry I’ve set the launcher.bat to do so. It works as it should be, unless I want to register the application wit its name to be shown as opening application both in exporer and browsers instead of launcher.bat .
The registry file that applied by the installer script is the following (it is gets generated dynamically, so don’t mind the application path as well):
What else should I add to the registry to do so?
2 Answers 2
First name your type:
And then set the program to open:
Replace all names in angle-brackets with your names:
After a few days of digging, I have found the solution:
The registry above assigns extension to the launcher (or any executable) to open it with, but the OS only shows the executable name as the associated program — which is fine, because It wasn’t registred. This could be shown on the properties window of the file which are associated to, in exprorer, when you right click on the file and choose open with, and even in browesers when a URI scheme is registed.
To do so, the application has to be registred as well in the registry. After digging the internet, finally found on MSDN the extra registry changes that does so.