?
Solved

Finding File Associations Programatically

Posted on 2006-10-31
21
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 2
  • +3
21 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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 accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

764 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