Пример #1
0
        internal static int GetProperty(this IPropertyStorage propertyStorage, uint propid, out PROPVARIANT propvar)
        {
            HRESULT hr = (int)HRESULT.E_FAIL;

            propvar = default(PROPVARIANT);

            PROPSPEC[]    propspec = new PROPSPEC[1];
            PROPVARIANT[] propvars = { propvar };

            propspec[0].ulKind   = (uint)PRSPEC.PROPID;
            propspec[0].u.propId = propid;

            if (NativeMethods.Succeeded(hr = propertyStorage.ReadMultiple(1, propspec, propvars)))
            {
                propvar = propvars[0];
            }

            return(hr);
        }
Пример #2
0
        GetOleProperty(
            Guid fmtid,
            uint propId
            )
        {
            CheckDisposed();

            // fmtid is always either DocSum or Sum.
            IPropertyStorage ps =
                fmtid == FormatId.SummaryInformation ? _psSummInfo : _psDocSummInfo;

            if (ps == null)
            {
                // This file doesn't even contain the property storage that this
                // property belongs to, so it certainly doesn't contain the property.
                return(null);
            }

            object obj = null;

            PROPSPEC[]    propSpecs = new PROPSPEC[1];
            PROPVARIANT[] vals      = new PROPVARIANT[1];

            propSpecs[0].propType     = (uint)PropSpecType.Id;
            propSpecs[0].union.propId = propId;

            VARTYPE vtExpected = GetVtFromPropId(fmtid, propId);

            int hresult = ps.ReadMultiple(1, propSpecs, vals);

            if (hresult == SafeNativeCompoundFileConstants.S_OK)
            {
                try
                {
                    if (vals[0].vt != vtExpected)
                    {
                        throw new FileFormatException(
                                  SR.Get(
                                      SRID.WrongDocumentPropertyVariantType,
                                      propId,
                                      fmtid.ToString(),
                                      vals[0].vt,
                                      vtExpected
                                      )
                                  );
                    }

                    switch (vals[0].vt)
                    {
                    case VARTYPE.VT_LPSTR:
                        //
                        // We store string properties as CP_ACP or UTF-8.
                        // But no matter which format the string was encoded, we always use the UTF-8
                        // encoder/decoder to decode the byte array, because the UTF-8 code of an ASCII
                        // string is the same as the ASCII string.
                        //
                        IntPtr pszVal = vals[0].union.pszVal;
                        //
                        // Because both the ASCII string and UTF-8 encoded string (byte array) are
                        // stored in a memory block (pszVal) terminated by null, we can use
                        // Marshal.PtrToStringAnsi(pszVal) to convert the memory block pointed by
                        // pszVal to a string. Then from the string.Length, we can get the number of
                        // bytes in the memory block. Otherwise, we cannot easily tell how many bytes
                        // are stored in pszVal without an extra parameter.
                        //
                        string ansiString = Marshal.PtrToStringAnsi(pszVal);
                        int    nLen       = ansiString.Length;

                        byte[] byteArray = new byte[nLen];
                        Marshal.Copy(pszVal, byteArray, 0, nLen);

                        obj = UTF8Encoding.UTF8.GetString(byteArray);
                        break;

                    case VARTYPE.VT_FILETIME:
                        //
                        // DateTime doesn't have a conversion from FILETIME. It has a
                        // misleadingly named "FromFileTime" method that actually wants
                        // a long. So...
                        //
                        obj = new Nullable <DateTime>(DateTime.FromFileTime(vals[0].union.hVal));
                        break;

                    default:
                        throw new FileFormatException(
                                  SR.Get(SRID.InvalidDocumentPropertyVariantType, vals[0].vt));
                    }
                }
                finally
                {
#pragma warning suppress 6031 // suppressing a "by design" ignored return value
                    SafeNativeCompoundFileMethods.SafePropVariantClear(ref vals[0]);
                }
            }
            else if (hresult == SafeNativeCompoundFileConstants.S_FALSE)
            {
                // Do nothing -- return the null object reference.
            }
            else
            {
                SecurityHelper.ThrowExceptionForHR(hresult);
            }

            return(obj);
        }