PROPVARIANT Helpers #3 - Simpler reading functions

You may have noticed that most of the PROPVARIANT helpers return an HRESULT.  This makes them easy to use in conjunction with COM calling patterns like the following:

IPropertyStore pps = ...
PROPVARIANT propvar = {0};
HRESULT hr = InitPropVariantFromInt32(100, &propvar);
if (SUCCEEDED(hr))
hr = pps->SetValue(PKEY_Rating, propvar);
if (SUCCEEDED(hr))

This works great for functions that already use HRESULTs.  But what if you just happen to absolutely know that the only VARTYPE your function will every see will be VT_UI4 every time the function is ever called?  Couldn't you just, say, read a member variable directly without checking the VARTYPE?  Well, you could, but would you take such a chance with the following helpers available for you to use?

// These will attempt to convert to the desired type, returning the provided default value if that isn't possible:
PSSTDAPI_(BOOL) PropVariantToBooleanWithDefault(REFPROPVARIANT propvarIn, BOOL fDefault); // VT_BOOL
PSSTDAPI_(SHORT) PropVariantToInt16WithDefault(REFPROPVARIANT propvarIn, SHORT iDefault); // VT_I2
PSSTDAPI_(USHORT) PropVariantToUInt16WithDefault(REFPROPVARIANT propvarIn, USHORT uiDefault); // VT_UI2
PSSTDAPI_(LONG) PropVariantToInt32WithDefault(REFPROPVARIANT propvarIn, LONG lDefault); // VT_I4
PSSTDAPI_(ULONG) PropVariantToUInt32WithDefault(REFPROPVARIANT propvarIn, ULONG ulDefault); // VT_UI4
PSSTDAPI_(LONGLONG) PropVariantToInt64WithDefault(REFPROPVARIANT propvarIn, LONGLONG llDefault); // VT_I8
PSSTDAPI_(ULONGLONG) PropVariantToUInt64WithDefault(REFPROPVARIANT propvarIn, ULONGLONG ullDefault); // VT_UI8
PSSTDAPI_(DOUBLE) PropVariantToDoubleWithDefault(REFPROPVARIANT propvarIn, DOUBLE dblDefault); // VT_R8

PropVariantToStringWithDefault is a little different. It will NOT attempt to make a conversion. It will return a direct pointer to the string data, or the provided default if that isn't possible.

PSSTDAPI_(PCWSTR) PropVariantToStringWithDefault(REFPROPVARIANT propvarIn, __in_opt LPCWSTR pszDefault); // VT_LPWSTR or VT_BSTR

Again, these functions come from propvarutil.h and are implemented in propsys.dll.