Last active
August 24, 2018 14:01
-
-
Save learn-more/341ad79303450d12b952fde0fdc7cdff to your computer and use it in GitHub Desktop.
dump IPropertyStore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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