?
Solved

Finding File Associations Programatically

Posted on 2006-10-31
21
Medium Priority
?
246 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
20 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17842238
Why can't you use FindExecutable?
0
 

Author Comment

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

Expert Comment

by:AlexFM
ID: 17842790
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:PhilC
ID: 17842937
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
ID: 17843193
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
ID: 17843364
what is the Registry Key for the default value of '.ext'?
0
 
LVL 86

Expert Comment

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

Author Comment

by:PhilC
ID: 17844405
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
ID: 17844469
How are you changing them and how are you checking for changes?
0
 

Author Comment

by:PhilC
ID: 17844615
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
 
LVL 86

Expert Comment

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

Author Comment

by:PhilC
ID: 17844894
I will try on my win2000 box
0
 
LVL 5

Expert Comment

by:NickGeorghiou
ID: 17847753
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
ID: 17849090
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
ID: 17932686
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
ID: 17932849
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
ID: 18324979
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
ID: 18398033
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
ID: 18398163
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
ID: 18461736
PAQd, 500 points refunded.

DarthMod
CS Moderator
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

755 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