// @pymethod bytes|propsys|StgSerializePropVariant|Serializes a <o PyPROPVARIANT> static PyObject *PyStgSerializePropVariant(PyObject *self, PyObject *args) { PROPVARIANT *pv; SERIALIZEDPROPERTYVALUE *pspv=NULL; ULONG bufsize; HRESULT hr; // @pyparm <o PyPROPVARIANT>|propvar||The value to serialize if (!PyArg_ParseTuple(args, "O&:StgSerializePropVariant", PyWinObject_AsPROPVARIANT, &pv)) return NULL; PY_INTERFACE_PRECALL; hr = StgSerializePropVariant(pv, &pspv, &bufsize); PY_INTERFACE_POSTCALL; if (FAILED(hr)) return PyCom_BuildPyException(hr); PyObject *ret = PyString_FromStringAndSize((char *)pspv, bufsize); CoTaskMemFree(pspv); return ret; };
HRESULT SerializePropVariantAsString(REFPROPVARIANT propvar, PWSTR *ppszOut) { SERIALIZEDPROPERTYVALUE *pBlob; ULONG cbBlob; // serialize PROPVARIANT to binary form HRESULT hr = StgSerializePropVariant(&propvar, &pBlob, &cbBlob); if (SUCCEEDED(hr)) { // determine the required buffer size hr = E_FAIL; DWORD cchString; if (CryptBinaryToStringW((BYTE *)pBlob, cbBlob, CRYPT_STRING_BASE64, NULL, &cchString)) { // allocate a sufficient buffer hr = E_OUTOFMEMORY; *ppszOut = (PWSTR)CoTaskMemAlloc(sizeof(WCHAR) * cchString); if (*ppszOut) { // convert the serialized binary blob to a string representation hr = E_FAIL; if (CryptBinaryToStringW((BYTE *)pBlob, cbBlob, CRYPT_STRING_BASE64, *ppszOut, &cchString)) { hr = S_OK; } else { CoTaskMemFree(*ppszOut); *ppszOut = NULL; } } } CoTaskMemFree(pBlob); } return hr; }