I am reading a structured storage file. And trying to get all child elements of the root structure. But I am getting access violation exception while doing so.
Here is the native methods,
[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
);
And here is my calling code.
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);
Note that if I call another method if IStorage, like OpenStream, that works fine,
_storageObject.OpenStream(streamName, IntPtr.Zero, (int)accessMode, 0);
I tried different combinations of STM flags when I open file, but it is not working.