Enumerate all file type associations

Ruslan Sydorovych 86 Reputation points
2020-11-22T16:32:46.927+00:00

Hello! I want to list all the file type associations app names. My code:

#include <windows.h>
#include <shlwapi.h>
#include <ShlObj.h>
#include <iostream>
#pragma comment(lib, "shlwapi.lib")

int main()
{
    IQueryAssociations *iQueryAssoc = nullptr;
    HRESULT assocHRes = AssocCreate(CLSID_QueryAssociations, IID_IQueryAssociations, reinterpret_cast<void**>(&iQueryAssoc));

    if (assocHRes == S_OK) {
        HWND hWnd = GetConsoleWindow();
        LPCWSTR pszAssoc = L".xls";
        HRESULT initAssocHRes = iQueryAssoc->Init(NULL, pszAssoc, NULL, hWnd);

        if (initAssocHRes == S_OK) {
            TCHAR buffer[1024];
            DWORD bufferSize = 1024;
            HRESULT getStrAssocHRes = iQueryAssoc->GetString(ASSOCF_NONE, ASSOCSTR_FRIENDLYAPPNAME, NULL, buffer, &bufferSize);

            if (getStrAssocHRes == S_OK) {
                std::wcout << "App name: " << std::wstring(buffer).c_str() << std::endl;
            } else {
                std::wcout << "iQueryAssoc GetString failed!" << std::endl;
            }
        } else {
            std::wcout << "iQueryAssoc Init failed!" << std::endl;
        }
    } else {
        std::wcout << "AssocCreate failed!" << std::endl;
    }

    iQueryAssoc->Release();
    system("PAUSE");
    return 0;
}

My code works but it displays app name only for the ".xls" extension. I think, I need to use the while (iQueryAssoc->QueryInterface()) to get all of them. Is there any example how to use it? Should I call init method first and then QueryInterface method or just call QueryInterface method without init method? Thank you.

Windows development | Windows API - Win32
Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2020-11-24T17:24:20.723+00:00

    In fact, the documentation for SHAssocEnumHandlers is wrong (it does not enumerate all extensions)
    Windows (System Settings) does a loop on extensions by reading registry (RegEnumKey, HKEY_CLASSES_ROOT) and get information for each extension mainly with IQueryAssociations, like you used

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 49,636 Reputation points
    2020-11-22T17:40:14.383+00:00

    See nf-shobjidl_core-shassocenumhandlers

    The docs say -

    pszExtra

    Type: PCWSTR

    A pointer to a null-terminated buffer that contains a single file type extension, for instance ".jpg". Only handlers associated with the given extension are enumerated. If this value is NULL, all handlers for all extensions are enumerated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.