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
Solved

Finding File Associations Programatically

Posted on 2006-10-31
21
240 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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