正在讀取結構化存儲文件。並試圖獲取根結構的所有子元素。但是我這樣做時會出現訪問衝突異常。調用IStorage的EnumElements時發生訪問衝突
這裏是本地方法,
[ComImport][Guid("0000000d-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IEnumSTATSTG
{
[PreserveSig] uint Next(uint celt, [MarshalAs(UnmanagedType.LPArray), Out] System.Runtime.InteropServices.ComTypes.STATSTG[] rgelt, out uint pceltFetched);
}
[ComImport][Guid("0000000b-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IStorage
{
[return: MarshalAs(UnmanagedType.Interface)]
IStream OpenStream([In, MarshalAs(UnmanagedType.BStr)] string pwcsName, IntPtr reserved1, [In, MarshalAs(UnmanagedType.U4)] int grfMode, [In, MarshalAs(UnmanagedType.U4)] int reserved2);
void EnumElements(
/* [in] */ uint reserved1,
/* [size_is][unique][in] */ IntPtr reserved2,
/* [in] */ uint reserved3,
/* [out] */ out IEnumSTATSTG ppenum);
}
[DllImport("ole32.dll", CharSet = CharSet.Unicode)]
internal static extern uint StgOpenStorageEx
(
[MarshalAs(UnmanagedType.LPWStr)] string name, uint accessMode,
uint storageFileFormat, uint fileBuffering, IntPtr options,
IntPtr reserved, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] ref IStorage stg
);
這是我的調用代碼。
IStorage _storageObject;
// Opening file,
NativeMethods.StgOpenStorageEx(path, (uint)STM.Read | STM.ShareDenyWrite, (uint)storageFileFormat, (uint)fileBuffering,
options, IntPtr.Zero, ref _iidIStorage, ref _storageObject);
// Here I am calling EnumElements, I get exception here.
IEnumSTATSTG pIEnumStatStg;
_storageObject.EnumElements(0, IntPtr.Zero, 0, out pIEnumStatStg);
需要注意的是,如果我調用另一個方法,如果的IStorage,喜歡的OpenStream,工作正常,
_storageObject.OpenStream(streamName, IntPtr.Zero, (int)accessMode, 0);
我試過STM標誌的不同組合,當我打開文件,但它不工作。
OpenStream是接口中的第二種方法。我實際上刪除了短代碼的其他方法, –
剛剛簽出。接口中缺少一種方法。現在工作正常。謝謝, –