Fail to read property of compound file

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have some code as below to read some property of word document, however, it always fails to open the summary information property set. Please invoke "Test.ReadProperty();" to see the problem.
Any ideas? Thanks in advance!
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Runtime.InteropServices;
<span style="color:Blue; using StructuredStorageWrapper;

<span style="color:Blue; namespace StructuredStorageWrapper
{
<span style="color:Blue; public <span style="color:Blue; enum SummaryPropId : <span style="color:Blue; int
{
Title = 0x00000002,
Subject = 0x00000003,
Author = 0x00000004,
Keywords = 0x00000005,
Comments = 0x00000006,
Template = 0x00000007,
LastSavedBy = 0x00000008,
RevisionNumber = 0x00000009,
TotalEditingTime = 0x0000000A,
LastPrinted = 0x0000000B,
CreateDateTime = 0x0000000C,
LastSaveDateTime = 0x0000000D,
NumPages = 0x0000000E,
NumWords = 0x0000000F,
NumChars = 0x00000010,
Thumbnail = 0x00000011,
AppName = 0x00000012,
Security = 0x00000013
}

<span style="color:Blue; public <span style="color:Blue; enum STGC : <span style="color:Blue; int
{
DEFAULT = 0,
OVERWRITE = 1,
ONLYIFCURRENT = 2,
DANGEROUSLYCOMMITMERELYTODISKCACHE = 4,
CONSOLIDATE = 8
}

<span style="color:Blue; public <span style="color:Blue; enum PROPSETFLAG : <span style="color:Blue; int
{
DEFAULT = 0,
NONSIMPLE = 1,
ANSI = 2,
UNBUFFERED = 4,
CASE_SENSITIVE = 8
}

<span style="color:Blue; public <span style="color:Blue; enum STGM : <span style="color:Blue; int
{
READ = 0x00000000,
WRITE = 0x00000001,
READWRITE = 0x00000002,
SHARE_DENY_NONE = 0x00000040,
SHARE_DENY_READ = 0x00000030,
SHARE_DENY_WRITE = 0x00000020,
SHARE_EXCLUSIVE = 0x00000010,
PRIORITY = 0x00040000,
CREATE = 0x00001000,
CONVERT = 0x00020000,
FAILIFTHERE = 0x00000000,
DIRECT = 0x00000000,
TRANSACTED = 0x00010000,
NOSCRATCH = 0x00100000,
NOSNAPSHOT = 0x00200000,
SIMPLE = 0x08000000,
DIRECT_SWMR = 0x00400000,
DELETEONRELEASE = 0x04000000
}

<span style="color:Blue; public <span style="color:Blue; enum STGFMT : <span style="color:Blue; int
{
STORAGE = 0,
FILE = 3,
ANY = 4,
DOCFILE = 5
}

[StructLayout(LayoutKind.Explicit, Size = 8, CharSet = CharSet.Unicode)]
<span style="color:Blue; public <span style="color:Blue; struct PropSpec
{
[FieldOffset(0)]
<span style="color:Blue; public <span style="color:Blue; int ulKind;
[FieldOffset(4)]
<span style="color:Blue; public IntPtr Name_Or_ID;
}

[ComVisible(<span style="color:Blue; true), ComImport(),
Guid(<span style="color:#A31515; "0000013A-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
<span style="color:Blue; public <span style="color:Blue; interface IPropertySetStorage
{
<span style="color:Blue; int Open(
[In, MarshalAs(UnmanagedType.Struct)] <span style="color:Blue; ref System.Guid rfmtid,
[In] <span style="color:Blue; int grfMode,
[Out] IPropertyStorage propertyStorage);
}


[ComVisible(<span style="color:Blue; true), ComImport(),
Guid(<span style="color:#A31515; "00000138-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
<span style="color:Blue; public <span style="color:Blue; interface IPropertyStorage
{
}

<span style="color:Blue; public <span style="color:Blue; class ole32
{
[StructLayout(LayoutKind.Explicit, Size = 12, CharSet = CharSet.Unicode)]
<span style="color:Blue; public <span style="color:Blue; struct STGOptions
{
[FieldOffset(0)]
<span style="color:Blue; ushort usVersion;
[FieldOffset(2)]
<span style="color:Blue; ushort reserved;
[FieldOffset(4)]
<span style="color:Blue; uint uiSectorSize;
[FieldOffset(8), MarshalAs(UnmanagedType.LPWStr)]
<span style="color:Blue; string pwcsTemplateFile;
}

[DllImport(<span style="color:#A31515; "ole32.dll", CharSet = CharSet.Unicode)]
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; uint StgOpenStorageEx(
[MarshalAs(UnmanagedType.LPWStr)] <span style="color:Blue; string name,
<span style="color:Blue; int accessMode, <span style="color:Blue; int storageFileFormat, <span style="color:Blue; int fileBuffering,
IntPtr options, IntPtr reserved, <span style="color:Blue; ref System.Guid riid,
[MarshalAs(UnmanagedType.Interface)] <span style="color:Blue; ref IPropertySetStorage propertySetStorage);
}
}

<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; class Test
{
<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; void ReadProperty()
{
IPropertySetStorage propSetStorage = <span style="color:Blue; null;
Guid IID_PropertySetStorage = <span style="color:Blue; new Guid(<span style="color:#A31515; "0000013A-0000-0000-C000-000000000046");

<span style="color:Blue; uint hresult = ole32.StgOpenStorageEx(
<span style="color:#A31515; "c:\test.doc",
(<span style="color:Blue; int)(STGM.READ | STGM.SHARE_EXCLUSIVE),
(<span style="color:Blue; int)STGFMT.STORAGE,
0,
(IntPtr)0,
(IntPtr)0,
<span style="color:Blue; ref IID_PropertySetStorage,
<span style="color:Blue; ref propSetStorage);


<span style="color:Green; // next you need to open the Summary Information property set
Guid fmtid_SummaryProperties = <span style="color:Blue; new Guid(<span style="color:#A31515; "F29F85E0-4FF9-1068-AB91-08002B27B3D9");
IPropertyStorage propStorage = <span style="color:Blue; null;
propSetStorage.Open(
<span style="color:Blue; ref fmtid_SummaryProperties,
(<span style="color:Blue; int)(STGM.READ | STGM.SHARE_EXCLUSIVE),
propStorage);

}
}

[/code]


View the full article
 
Back
Top