Solved

Finding File Associations Programatically

Posted on 2006-10-31
21
232 Views
Last Modified: 2013-12-04
Hi,
I need to find out how to tell what program is associated with a particular filetype. .doc for example.
I cannot use FindExecutable. I need to know what will be launched if I call ShellExecuteEx().
Registry Keys would be acceptable. I need to be able to do this on Win2000,XP and Vista.

Regards,
0
Comment
Question by:PhilC
  • 10
  • 4
  • 2
  • +3
21 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
Why can't you use FindExecutable?
0
 

Author Comment

by:PhilC
Comment Utility
Because it does not work reliably with Long Filenames...
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
0
 

Author Comment

by:PhilC
Comment Utility
Exactly, and the solution there presented did not work for me.
I am currently looking at AssocQueryString() to resolve this issue.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Why not reading the associated executable directly from the registry? First you need to look up the default value of '.ext' (let's call the result 'filetype', then read the default value of 'HKEY_CLASSES_ROOT\[filetype]\shell\Open\command' and you have the executable you are looking for.
0
 

Author Comment

by:PhilC
Comment Utility
what is the Registry Key for the default value of '.ext'?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
That's simply 'HKEY_CLASSES_ROOT\.ext', or in your case 'HKEY_CLASSES_ROOT\.doc'
0
 

Author Comment

by:PhilC
Comment Utility
These don't seem to work on my XP box. When I change the association the change is not reflected in these registry keys...
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
How are you changing them and how are you checking for changes?
0
 

Author Comment

by:PhilC
Comment Utility
start->explore for all users->folder options. change the default app. Then run regedit and look for changes. nada...
There must be several keys that Windows uses?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:jkr
Comment Utility
No, these two are the only ones. And if that suddenly stopped working, hardly anything would run on XP...
0
 

Author Comment

by:PhilC
Comment Utility
I will try on my win2000 box
0
 
LVL 5

Expert Comment

by:NickGeorghiou
Comment Utility
Hey guys,

Just an extra note which might be relevant.

The (Default) value of a key that represents an extension sometimes points to a different location (key) for the information e.g. in a key such as:
 
'HKEY_CLASSES_ROOT\.ext'

the (Default) string may be set to something like ext_auto_file. In this case the file association information would be under the key:
 
'HKEY_CLASSES_ROOT\ext_auto_file'

Could this be the cause of the problem?
Nick
0
 

Author Comment

by:PhilC
Comment Utility
No, this is not the problem. I look at the .ext default key and then look in the ...\command key
0
 
LVL 8

Expert Comment

by:adg080898
Comment Utility
You are not looking at the correct key.

You were on the right track looking at 'HKEY_CLASSES_ROOT\.ext'.

Read this page:

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

In particular, look under the heading "Implementation details":

HKEY_CLASSES_ROOT\.txt
      (default) = "txtfile"

HKEY_CLASSES_ROOT\txtfile
         \DefaultIcon
            (default) = %SystemRoot%\system32\shell32.dll,-152
         \shell\open\command
            (default) = %SystemRoot%\system32\NOTEPAD.EXE %1

See how the ".ext" entry simply has a string ("txtfile" in the example above). Then windows looks under "HKEY_CLASSES_ROOT\txtfile". The actual association in under "shell\open\command".
0
 

Author Comment

by:PhilC
Comment Utility
Ended up having to use
AssocQueryStringW()
as
AssocQueryStringA()  has a bug in it per a Support call to Microsoft.

for those interested the final function looks like...
HRESULT CCSFindExecutable(char *pszFilePath, char *pszExecutable)
{
    LPWSTR pszExt,pszExe;
    DWORD cchExe;
    wchar_t szWFilePath[MAX_PATH];
       HRESULT hr=-1;

        MultiByteToWideChar(CP_ACP, 0, pszFilePath, -1, szWFilePath, MAX_PATH);

        pszExe = (LPWSTR)LocalAlloc(LPTR, sizeof(TCHAR) * MAX_PATH);
        if (pszExe)
        {
          pszExt = PathFindExtensionW(szWFilePath);
          if (pszExt)
          {
            cchExe = MAX_PATH;
            ZeroMemory(pszExe, sizeof(TCHAR)*MAX_PATH);

            hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, pszExt, NULL, pszExe, &cchExe);

            if (SUCCEEDED(hr))
            {
            WideCharToMultiByte(CP_ACP,0,(LPWSTR)pszExe,MAX_PATH,pszExecutable,MAX_PATH,NULL,NULL);
            }
        }
          LocalFree((HLOCAL)pszExe);
      }            

    return hr;
}
0
 

Author Comment

by:PhilC
Comment Utility
Admins: I would like to recover points as Microsoft Support ended up answering this question. Any chance of that?
0
 
LVL 8

Expert Comment

by:adg080898
Comment Utility
If I remember correctly, you are required to post the solution yourself to get the refund. An admin here would know for sure.
0
 

Author Comment

by:PhilC
Comment Utility
Ended up having to use
AssocQueryStringW()
as
AssocQueryStringA()  has a bug in it per a Support call to Microsoft.

for those interested the final function looks like...
HRESULT CCSFindExecutable(char *pszFilePath, char *pszExecutable)
{
    LPWSTR pszExt,pszExe;
    DWORD cchExe;
    wchar_t szWFilePath[MAX_PATH];
       HRESULT hr=-1;

        MultiByteToWideChar(CP_ACP, 0, pszFilePath, -1, szWFilePath, MAX_PATH);

        pszExe = (LPWSTR)LocalAlloc(LPTR, sizeof(TCHAR) * MAX_PATH);
        if (pszExe)
        {
          pszExt = PathFindExtensionW(szWFilePath);
          if (pszExt)
          {
            cchExe = MAX_PATH;
            ZeroMemory(pszExe, sizeof(TCHAR)*MAX_PATH);

            hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, pszExt, NULL, pszExe, &cchExe);

            if (SUCCEEDED(hr))
            {
            WideCharToMultiByte(CP_ACP,0,(LPWSTR)pszExe,MAX_PATH,pszExecutable,MAX_PATH,NULL,NULL);
            }
        }
          LocalFree((HLOCAL)pszExe);
      }            

    return hr;
}
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
Comment Utility
PAQd, 500 points refunded.

DarthMod
CS Moderator
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now