Sunday, July 12, 2009

In C++, how is it possible to get a list of the file extensions associated with a given application?

For a project, I need to get the list of all file extensions associated with a given executable. For example, I would like to be able to get a string like ".doc;.dot;.txt;.wri" when asking for what are the extensions associated with Word.


I am convinced there's a Windows API out there to do this but I can't find it.


Thanks for your help!

In C++, how is it possible to get a list of the file extensions associated with a given application?
Your best bet is to google the topic. I have done this for C++ and found numerous sources that detail the language. Try C++ file extensions, and then search. This should bring up sources that will have the info you need.
Reply:Hi.





This is not just a C++ issue but a Microsoft/operating system issues.





1. Identify the API provided by the operating system.


2. Write your C++ code to use that API.





For Windows, these associations are made in the Windows Registry. As a result, you'll need to learn which registry keys to modify and the API in C++:





1. Run regedt32.exe just to see the registry. Start-%26gt;run then type in regedt32.exe.





2. The C++ API for modifying the windows registry is here:


http://msdn2.microsoft.com/en-us/library...


http://msdn2.microsoft.com/en-us/library...





3. File Associations Defined in the Registry info is here:


http://msdn.microsoft.com/library/defaul...








Best! :-)
Reply:File association settings are stored within the system registry, under HKEY_CLASSES_ROOT. Each file extension is listed as a key with a name based on the file extension (e.g. plain text files are listed as ".txt" and AVI files are listed as ".avi"). If a file extension is associated with a program, its key would have a default value that would be the name of another key under HKEY_CLASSES_ROOT. This key will contain information about the program or programs that are associated with the file type.





For example, under HKEY_CLASSES_ROOT, there is a key named ".txt". The default value of this key is "txtfile". "HKEY_CLASSES_ROOT\txtfile" has a default value that is "Text File". This is the title of the file type. If the file type is associated with a program that opens it when a file of that type is double clicked on, "HKEY_CLASSES_ROOT\txtfile" will have a sub-key called "shell", which has another sub-key called "open". HKEY_CLASSES_ROOT\txtfile\shell\open" would have a sub-key called "command". This will have a default value that is the path and command parameters for the program that will open a text file.





So you will need to use commands that let you read the system registry. A reference for registry functions in the Windows API can be found at the address below.





http://msdn.microsoft.com/library/defaul...





Unfortunately, I do not think that you can simply specify an application and immediately find out what files it takes care of. The only solution I see is the following.





1. Produce some kind of table that has a column for an application's name or path and another for a list of file extensions that are associated with the program.


2. Enumerate the sub-keys of "HKEY_CLASSES_ROOT", using RegEnumKeyEx.


3. For every key whose name starts with a dot ("."):


  a) Open the sub-key, using RegOpenKeyEx.


  b) Read the key's default value with RegGetValue.


  c) If the default value you just read (%26lt;dvr%26gt;) is not empty:


    i) Open the sub-key "HKEY_CLASSES_ROOT \%26lt;dvr%26gt; \Shell \Open \Command", using RegOpenKeyEx. Note that I had to place spaces before the back-slashes to stop Yahoo Answers from cutting off the path half-way. The spaces shouldn't be there.


    ii) If successful, read the key's default value with RegGetValue.


    iii) If successful, decode the path you have just read to find out what program the file type is associated with.


    iv) If this program is not in the table yet, create a new row in the table, and add the program and the current file extension to it, otherwise add the file extension to the row that contains the name (or path, depending on how you want it done) of the program that has already been met.


    v) Close the key with RegCloseKey.


  d) Close the sub-key under HKEY_CLASSES_ROOT, using RegCloseKey.





By the end of this, you should have produced a complete table of all the programs on the computer that are associated with one or more specific file extensions and you will be able to select a specific program and list all of the file extensions it is associated with (with the right amount of code).





I suggest that you run "regedit.exe" and browse through the sub-keys of HEY_CLASSES_ROOT to work out what sub-keys and what values contain what information, in case you want to expand your program to do more complex stuff. Be very careful with your programming and when using RegEdit. If you accidentally change or delete a key or one of its values, you can mess up your system's settings.


No comments:

Post a Comment