Solved

Finding File Associations Programatically

Posted on 2006-10-31
21
243 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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