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
Enumerate all file type associations
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.
-
Castorix31 84,471 Reputation points
2020-11-24T17:24:20.723+00:00
1 additional answer
Sort by: Most helpful
-
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.