Skip to content

Instantly share code, notes, and snippets.

@learn-more
Last active August 24, 2018 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save learn-more/341ad79303450d12b952fde0fdc7cdff to your computer and use it in GitHub Desktop.
Save learn-more/341ad79303450d12b952fde0fdc7cdff to your computer and use it in GitHub Desktop.
dump IPropertyStore
#include <atlbase.h>
#include <atlcom.h>
#include <atlcomcli.h>
#include <comdef.h>
#include <Propvarutil.h>
#include <shobjidl.h>
#include <atlsafe.h>
#include <strsafe.h>
#pragma comment(lib, "Propsys.lib")
static void throw_if_(HRESULT hr, const char* name)
{
if (!SUCCEEDED(hr))
{
wprintf(L"%S:\n", name);
throw _com_error(hr);
}
}
#define throw_if(x) throw_if_((x), #x)
static void print_variant(CComVariant& var)
{
if (var.vt == (VT_ARRAY | VT_BSTR))
{
CComSafeArray<BSTR> sa;
sa.Attach(V_ARRAY(&var));
LONG lower = sa.GetLowerBound();
LONG upper = sa.GetUpperBound();
wprintf(L" [ ");
for (LONG l = lower; l <= upper; ++l)
{
CComBSTR tmp = sa.GetAt(l);
wprintf(L"%s'%s'", l != lower ? L", " : L"", (WCHAR*)tmp);
}
wprintf(L" ]");
return;
}
HRESULT hr = var.ChangeType(VT_BSTR);
if (SUCCEEDED(hr))
{
wprintf(L"'%s'", var.bstrVal);
}
else
{
wprintf(L"[Failed converting 0x%x to VT_BSTR]", var.vt);
}
}
static void test_propstore(const wchar_t* name)
{
CComPtr<IPropertyStore> spStore;
throw_if(SHGetPropertyStoreFromParsingName(name, NULL, GPS_BESTEFFORT, __uuidof(IPropertyStore), (void**)&spStore));
DWORD cnt = 0;
throw_if(spStore->GetCount(&cnt));
wprintf(L"%d properties for %s\n", cnt, name);
for (DWORD n = 0; n < cnt; ++n)
{
PROPERTYKEY key;
throw_if(spStore->GetAt(n, &key));
CComVariant var;
PROPVARIANT prop;
throw_if(spStore->GetValue(key, &prop));
throw_if(PropVariantToVariant(&prop, &var));
PropVariantClear(&prop);
CComHeapPtr<WCHAR> pszName;
HRESULT hr = PSGetNameFromPropertyKey(key, &pszName);
if (!SUCCEEDED(hr))
{
WCHAR buf[512];
throw_if(PSStringFromPropertyKey(key, buf, _countof(buf)));
wprintf(L" [%s] = ", buf);
}
else
{
wprintf(L" %s = ", (WCHAR*)pszName);
}
print_variant(var);
wprintf(L"\n");
}
}
int wmain(int argc, wchar_t* argv[])
{
CoInitialize(NULL);
for (int n = 1; n < argc; ++n)
{
test_propstore(argv[n]);
}
if (argc == 1)
test_propstore(L"c:\\Windows\\Media\\Alarm01.wav");
return 0;
}
@serup
Copy link

serup commented Aug 24, 2018

I tried this and encountered some heap issue - any chance you have an update to this version?

@serup
Copy link

serup commented Aug 24, 2018

perhaps you need to change line 71 to : PWSTR pszName;
and add CoTaskMemFree(pszName); between line 81 and 82

@serup
Copy link

serup commented Aug 24, 2018

suggest adding following between line 36 and 37
sa.Detach();

otherwise you could encounter some exception due to the fact that the attached var is destroyed premature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment