public PropertySetStorage(string filename)
 {
     if (Functions.IsAtLeastW2K())
     {
         STGFMT Fmt;
         Guid   IID_IPropertySetStorage = new Guid("0000013A-0000-0000-C000-000000000046");
         if (API.StgIsStorageFile(filename) == 0)
         {
             Fmt = STGFMT.STGFMT_DOCFILE;
         }
         else
         {
             Fmt = STGFMT.STGFMT_FILE;
         }
         int Res = API.StgCreateStorageEx(filename, 0x12, Fmt, 0, 0, 0, ref IID_IPropertySetStorage, ref this.m_IPropSetObj);
         if (Res == -2147286960)
         {
             Res = API.StgOpenStorageEx(filename, 0x12, Fmt, 0, 0, 0, ref IID_IPropertySetStorage, ref this.m_IPropSetObj);
             if (Res != 0)
             {
                 Marshal.ThrowExceptionForHR(Res);
             }
         }
         else
         {
             Marshal.ThrowExceptionForHR(Res);
         }
     }
     else
     {
         Storage Stg = new Storage(filename);
         this.m_IPropSetObj = (IPropertySetStorage)Stg.IStorageObj;
     }
 }
Exemple #2
0
        internal static int GetPropertyName(this IPropertySetStorage propertySetStorage, Guid fmtid, uint propid, out string name)
        {
            HRESULT hr = (int)HRESULT.E_FAIL;

            name = String.Empty;

            IPropertyStorage propertyStorage = null;
            uint             grfMode         = (uint)(STGM.READ | STGM.SHARE_EXCLUSIVE);

            try
            {
                if (NativeMethods.Succeeded(hr = propertySetStorage.Open(ref fmtid, grfMode, out propertyStorage)))
                {
                    hr = propertyStorage.GetPropertyName(propid, out name);
                }
            }
            catch
            {
            }
            finally
            {
                if (propertyStorage != null)
                {
                    propertyStorage.FinalRelease();
                }
            }

            return(hr);
        }
Exemple #3
0
        internal static int GetPropertyValue(this IPropertySetStorage propertySetStorage, Guid fmtid, uint propid, out PROPVARIANT propvar)
        {
            HRESULT hr = (int)HRESULT.E_FAIL;

            propvar = default(PROPVARIANT);

            PROPSPEC[]       propspec        = new PROPSPEC[1];
            PROPVARIANT[]    propvars        = { propvar };
            IPropertyStorage propertyStorage = null;
            uint             grfMode         = (uint)(STGM.READ | STGM.SHARE_EXCLUSIVE);

            try
            {
                if (NativeMethods.Succeeded(hr = propertySetStorage.Open(ref fmtid, grfMode, out propertyStorage)))
                {
                    hr = propertyStorage.GetProperty(propid, out propvar);
                }
            }
            catch
            {
            }
            finally
            {
                if (propertyStorage != null)
                {
                    propertyStorage.FinalRelease();
                }
            }

            return(hr);
        }
Exemple #4
0
        public static void SetProperty(string filename, string msg, SummaryPropId summaryType)
        {
            IPropertySetStorage propSetStorage      = null;
            Guid             IID_PropertySetStorage = new Guid("0000013A-0000-0000-C000-000000000046");
            uint             hresult = ole32.StgOpenStorageEx(filename, (int)(STGM.SHARE_EXCLUSIVE | STGM.READWRITE), (int)STGFMT.FILE, 0, (IntPtr)0, (IntPtr)0, ref IID_PropertySetStorage, ref propSetStorage);
            Guid             fmtid_SummaryProperties = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
            IPropertyStorage propStorage             = null;

            hresult = propSetStorage.Create(ref fmtid_SummaryProperties, (IntPtr)0, (int)PROPSETFLAG.DEFAULT, (int)(STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE), ref propStorage);

            PropSpec propertySpecification = new PropSpec();

            propertySpecification.ulKind     = 1;
            propertySpecification.Name_Or_ID = new IntPtr((int)summaryType);

            PropVariant propertyValue = new PropVariant();

            propertyValue.FromObject(msg);

            propStorage.WriteMultiple(1, ref propertySpecification, ref propertyValue, 2);
            hresult = propStorage.Commit((int)STGC.DEFAULT);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(propSetStorage);
            propSetStorage = null;
            GC.Collect();
        }
 public static extern int StgOpenStorageEx(
     [MarshalAs(UnmanagedType.LPWStr)] string pwcsName,
     uint grfMode,
     uint stgfmt,
     uint grfAttrs,
     IntPtr pStgOptions,
     IntPtr reserved2,
     [In] ref Guid riid,
     out IPropertySetStorage ppObjectOpen);
 public void Close()
 {
     if (!this.m_IgnoreClose && (this.m_IPropSetObj != null))
     {
         Marshal.ReleaseComObject(this.m_IPropSetObj);
         this.m_IPropSetObj = null;
         GC.SuppressFinalize(this);
     }
 }
Exemple #7
0
 public static extern int StgOpenStorageEx(
     [MarshalAs(UnmanagedType.LPWStr)] string pwcsName,
     uint grfMode,
     uint stgfmt,
     uint grfAttrs,
     IntPtr pStgOptions,
     IntPtr reserved2,
     [In] ref Guid riid,
     out IPropertySetStorage ppObjectOpen);
            // Methods
            internal StatPropSetStgCollection(IPropertySetStorage propertySetStorage)
            {
                StatPropSetStg      Stat;
                int                 fetched;
                IEnumSTATPROPSETSTG oEnum = propertySetStorage.Enum();

Label_0024:
                fetched = 0;
                if (oEnum.Next(1, out Stat, out fetched) == 0)
                {
                    base.InnerList.Add(Stat);
                    goto Label_0024;
                }
                Marshal.ReleaseComObject(oEnum);
            }
Exemple #9
0
        public void CreatePropertySets(bool useAnsi)
        {
            PropertySetStorage.Flags Flags;
            Guid g = System.Guid.Empty;
            IPropertySetStorage PropSetStg = (IPropertySetStorage)this.m_IStorageObj;

            if (useAnsi)
            {
                Flags = PropertySetStorage.Flags.ANSI;
            }
            else
            {
                Flags = PropertySetStorage.Flags.Default;
            }
            PropSetStg.Create(ref PropertySetStorage.FMTID_SummaryInformation, ref g, (int)Flags, Modes.Create | Modes.ShareExclusive | Modes.AccessReadWrite);
            PropSetStg.Create(ref PropertySetStorage.FMTID_DocSummaryInformation, ref g, (int)Flags, Modes.Create | Modes.ShareExclusive | Modes.AccessReadWrite);
            PropSetStg.Create(ref PropertySetStorage.FMTID_UserProperties, ref g, (int)Flags, Modes.Create | Modes.ShareExclusive | Modes.AccessReadWrite);
        }
Exemple #10
0
        public static IEnumerable <STATPROPSETSTG> AsEnumerable(this IPropertySetStorage propertySetStorage)
        {
            Assert.IsTrue(propertySetStorage != null);

            propertySetStorage.Enum(out var enumStatPropSetStg);

            while (true)
            {
                var statPropSetStgArray = new STATPROPSETSTG[1];
                enumStatPropSetStg.Next(1, statPropSetStgArray, out var fetched);

                if (fetched == 0)
                {
                    break;
                }

                yield return(statPropSetStgArray[0]);
            }
        }
Exemple #11
0
        public static IEnumerable <STATPROPSETSTG> AsEnumerable(this IPropertySetStorage propertySetStorage)
        {
            FoundationContract.Requires <ArgumentException>(propertySetStorage != null);

            IEnumSTATPROPSETSTG enumStatPropSetStg;

            propertySetStorage.Enum(out enumStatPropSetStg);

            while (true)
            {
                var  statPropSetStgArray = new STATPROPSETSTG[1];
                uint fetched;
                enumStatPropSetStg.Next(1, statPropSetStgArray, out fetched);

                if (fetched == 0)
                {
                    break;
                }

                yield return(statPropSetStgArray[0]);
            }
        }
Exemple #12
0
        public static void SetProperty(string filename, string msg, SummaryPropId summaryType)
        {
            IPropertySetStorage propertySetStorage = null;
            Guid riid = new Guid("0000013A-0000-0000-C000-000000000046");

            ole32.StgOpenStorageEx(filename, 0x12, 3, 0, IntPtr.Zero, IntPtr.Zero, ref riid, ref propertySetStorage);
            Guid             rfmtid          = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9");
            IPropertyStorage propertyStorage = null;

            propertySetStorage.Create(ref rfmtid, IntPtr.Zero, 0, 0x1012, ref propertyStorage);
            PropSpec propertySpecification = new PropSpec {
                ulKind     = 1,
                Name_Or_ID = new IntPtr((int)summaryType)
            };
            PropVariant propertyValues = new PropVariant();

            propertyValues.FromObject(msg);
            propertyStorage.WriteMultiple(1, ref propertySpecification, ref propertyValues, 2);
            propertyStorage.Commit(0);
            Marshal.ReleaseComObject(propertySetStorage);
            GC.Collect();
        }
Exemple #13
0
 public static extern int StgOpenStorageEx([MarshalAs(UnmanagedType.LPWStr)] string pwcsName, int grfMode, STGFMT stgfmt, int grfAttrs, int pStgOptions, int reserved2, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] ref IPropertySetStorage ppObjectOpen);
Exemple #14
0
        private void LoadPropertySet(IPropertySetStorage propertySetStorage, Guid fmtid)
        {
            var guid = fmtid;
            var hr   = propertySetStorage.Open(ref guid, STGM.STGM_READ | STGM.STGM_SHARE_EXCLUSIVE, out var propertyStorage);

            if (hr == STG_E_FILENOTFOUND || hr == STG_E_ACCESSDENIED)
            {
                return;
            }

            if (hr != 0)
            {
                throw new Win32Exception((int)hr);
            }

            propertyStorage.Enum(out var es);
            if (es == null)
            {
                return;
            }

            try
            {
                var stg = new STATPROPSTG();
                int fetched;
                do
                {
                    hr = es.Next(1, ref stg, out fetched);
                    if (hr != 0 && hr != 1)
                    {
                        throw new Win32Exception((int)hr);
                    }

                    if (fetched == 1)
                    {
                        var name = GetPropertyName(fmtid, propertyStorage, stg);

                        var propsec = new PROPSPEC[]
                        {
                            new PROPSPEC
                            {
                                ulKind = stg.lpwstrName != null ? PRSPEC.PRSPEC_LPWSTR : PRSPEC.PRSPEC_PROPID,
                            },
                        };

                        var lpwstr = IntPtr.Zero;
                        if (stg.lpwstrName != null)
                        {
                            lpwstr = Marshal.StringToCoTaskMemUni(stg.lpwstrName);
                            propsec[0].union.lpwstr = lpwstr;
                        }
                        else
                        {
                            propsec[0].union.propid = stg.propid;
                        }

                        var vars = new PROPVARIANT[1];
                        vars[0] = new PROPVARIANT();
                        try
                        {
                            hr = propertyStorage.ReadMultiple(1, propsec, vars);
                            if (hr != 0)
                            {
                                throw new Win32Exception((int)hr);
                            }
                        }
                        finally
                        {
                            if (lpwstr != IntPtr.Zero)
                            {
                                Marshal.FreeCoTaskMem(lpwstr);
                            }
                        }

                        object value;
                        try
                        {
                            switch (vars[0].vt)
                            {
                            case VARTYPE.VT_BOOL:
                                value = vars[0].union.boolVal != 0 ? true : false;
                                break;

                            case VARTYPE.VT_BSTR:
                                value = Marshal.PtrToStringUni(vars[0].union.bstrVal);
                                break;

                            case VARTYPE.VT_CY:
                                value = decimal.FromOACurrency(vars[0].union.cyVal);
                                break;

                            case VARTYPE.VT_DATE:
                                value = DateTime.FromOADate(vars[0].union.date);
                                break;

                            case VARTYPE.VT_DECIMAL:
                                var dec = IntPtr.Zero;
                                Marshal.StructureToPtr(vars[0], dec, false);
                                value = Marshal.PtrToStructure(dec, typeof(decimal));
                                break;

                            case VARTYPE.VT_DISPATCH:
                                value = Marshal.GetObjectForIUnknown(vars[0].union.pdispVal);
                                break;

                            case VARTYPE.VT_ERROR:
                            case VARTYPE.VT_HRESULT:
                                value = vars[0].union.scode;
                                break;

                            case VARTYPE.VT_FILETIME:
                                value = DateTime.FromFileTime(vars[0].union.filetime);
                                break;

                            case VARTYPE.VT_I1:
                                value = vars[0].union.cVal;
                                break;

                            case VARTYPE.VT_I2:
                                value = vars[0].union.iVal;
                                break;

                            case VARTYPE.VT_I4:
                                value = vars[0].union.lVal;
                                break;

                            case VARTYPE.VT_I8:
                                value = vars[0].union.hVal;
                                break;

                            case VARTYPE.VT_INT:
                                value = vars[0].union.intVal;
                                break;

                            case VARTYPE.VT_LPSTR:
                                value = Marshal.PtrToStringAnsi(vars[0].union.pszVal);
                                break;

                            case VARTYPE.VT_LPWSTR:
                                value = Marshal.PtrToStringUni(vars[0].union.pwszVal);
                                break;

                            case VARTYPE.VT_R4:
                                value = vars[0].union.fltVal;
                                break;

                            case VARTYPE.VT_R8:
                                value = vars[0].union.dblVal;
                                break;

                            case VARTYPE.VT_UI1:
                                value = vars[0].union.bVal;
                                break;

                            case VARTYPE.VT_UI2:
                                value = vars[0].union.uiVal;
                                break;

                            case VARTYPE.VT_UI4:
                                value = vars[0].union.ulVal;
                                break;

                            case VARTYPE.VT_UI8:
                                value = vars[0].union.uhVal;
                                break;

                            case VARTYPE.VT_UINT:
                                value = vars[0].union.uintVal;
                                break;

                            case VARTYPE.VT_UNKNOWN:
                                value = Marshal.GetObjectForIUnknown(vars[0].union.punkVal);
                                break;

                            default:
                                value = null;
                                break;
                            }
                        }
                        finally
                        {
                            PropVariantClear(ref vars[0]);
                        }

                        var property = new CompoundProperty(fmtid, name, stg.propid)
                        {
                            Value   = value,
                            Changed = false,
                        };
                        Properties.InternalAdd(property);
                    }
                }while (fetched == 1);
            }
            finally
            {
                Marshal.ReleaseComObject(es);
            }
        }
Exemple #15
0
 private static extern uint StgOpenStorageEx([In, MarshalAs(UnmanagedType.LPWStr)] string pwcsName, STGM grfMode, STGFMT stgfmt, int grfAttrs, IntPtr pStgOptions, IntPtr reserved2, ref Guid riid, out IPropertySetStorage ppObjectOpen);
Exemple #16
0
 internal PropertySets(IPropertySetStorage propertySetStorage)
 {
     _propertySetStorage = propertySetStorage;
     LoadPropertySets();
 }
Exemple #17
0
 internal static extern int StgCreatePropSetStg(UCOMIStorage storage, int reserverd, out IPropertySetStorage propSetStg);
Exemple #18
0
 private static extern int StgCreatePropSetStg(IStorage pStorage, uint reserved,
                                               out IPropertySetStorage ppPropSetStg);
Exemple #19
0
 internal static int GetPropertyValue(this IPropertySetStorage propertySetStorage, PIDDSI propid, out PROPVARIANT propvar)
 {
     return(propertySetStorage.GetPropertyValue(FormatId.DocSummaryInformation, (uint)propid, out propvar));
 }
 // Methods
 internal PropertySetStorage(IStorage storage)
 {
     this.m_IPropSetObj = (IPropertySetStorage)storage;
     this.m_IgnoreClose = true;
 }
Exemple #21
0
        internal int GetPropertyValue(Guid fmtid, uint propid, out PROPVARIANT propvar)
        {
            IPropertySetStorage propertySetStorage = (IPropertySetStorage)_rootStorage;

            return(propertySetStorage.GetPropertyValue(fmtid, propid, out propvar));
        }
Exemple #22
0
 internal static extern uint StgOpenStorageEx([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string name, int accessMode, int storageFileFormat, int fileBuffering, System.IntPtr options, System.IntPtr reserved, ref System.Guid riid, [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Interface)] ref IPropertySetStorage propertySetStorage);
Exemple #23
0
 public static extern uint StgCreateStorageEx([MarshalAs(UnmanagedType.LPWStr)] string name, int accessMode, int storageFileFormat, int fileBuffering, IntPtr options, IntPtr reserved, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] ref IPropertySetStorage propertySetStorage);