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 API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,585 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,690 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 84,471 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 45,236 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.