Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

StgOpenStorage (ole32)
 
.
Summary
Summary

C# Signature:

/// <summary>Opens a compound document file and represents it as an IStorage object</summary>
/// <remarks>Use on Windows 2000 and earlier</remarks>
/// <param name="pwcsName">The path and filename of the file to open</param>
/// <param name="pstgPriority">Generally passed as null</param>
/// <param name="grfMode">Access Method</param>
/// <param name="snbExclude">Must be NULL</param>
/// <param name="reserved">Pass as zero (0)</param>
/// <param name="ppstgOpen">out IStorage</param>
/// <returns>int HRESULT</returns>

/// Needs more help form MS

[DllImport("ole32.dll")]
static extern int StgOpenStorageEx([MarshalAs(UnmanagedType.LPWStr)] string
   pwcsName, uint grfMode, uint stgfmt, uint grfAttrs, ref STGOPTIONS
   pStgOptions, IntPtr reserved2, [In] ref Guid riid,
   [MarshalAs(UnmanagedType.IUnknown)] out object ppObjectOpen);

User-Defined Types:

None.

static extern int StgOpenStorage(
    [MarshalAs(UnmanagedType.LPWStr)] string pwcsName
    , IStorage pstgPriority
    , STGM grfMode                // Access Method (uint)
    , IntPtr snbExclude            // Must be NULL
    , uint reserved                // Reserved
    , out IStorage ppstgOpen);    // Returned Storage

Notes:

None.

///PreserveSig set to false automatically converts HRESULTS values to exceptions.
///Note the return value is void not an int (HRESULT).
[DllImport("ole32.dll", PreserveSig = false)]
static extern void StgOpenStorage([MarshalAs(UnmanagedType.LPWStr)] string pwcsName,
IStorage pstgPriority, STGM grfMode, IntPtr snbExclude, uint reserved,
out IStorage ppstgOpen);

Tips & Tricks:

Please add some!

User-Defined Types:

None.

Sample Code:

Please add some!

Notes:

Win2K and later should use the StgOpenStorageEx function.

Alternative Managed API:

The System.IO.Packaging namespace contains public StorageInfo and StreamInfo classes that encapsulate OLE structured storage access. Unfortunately, the StorageRoot class, which is needed to create or open the files, is an internal class. However, you can use reflection to access the methods on StorageRoot if necessary. The code below will open a storage file and convert it to XML where each stream is hex encoded.

Tips & Tricks:

Please add some!

    public static void ReadOSSFile(string fileName, XmlWriter writer)
    {
        int checkResult = StgIsStorageFile(fileName);
        Preconditions.RequireArgument(checkResult == 0,
            "The specified file is not an OLE Structured Storage file.");

Sample Code:

string pathAndFilename = @"C:\outlookmail.msg"
IStorage storage;
int hResult = StgOpenStorage(pathAndFilename, null, STGM.READ_ONLY, IntPtr.Zero, 0, out storage);
// Then one can use the EnumElements method of the IStorage interface to enum the properties of the OLE2 storage.

        StorageInfo storageRoot = GetStorageRoot(fileName);
        try
        {
            WriteStorage(storageRoot, fileName, writer);
        }
        finally
        {
            CloseStorageRoot(storageRoot);
        }
    }

Alternative Managed API:

See StgOpenStorageEx for an example of using the System.IO.Packaging classes to access OLE structured storage.

    private static StorageInfo GetStorageRoot(string fileName)
    {
        StorageInfo storageRoot = (StorageInfo)InvokeStorageRootMethod(null,
            "Open", fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        if (storageRoot == null)
        {
            Utilities.ThrowInvalidStateFmt("Unable to open \"{0}\" as a structured storage file.", fileName);
        }
        return storageRoot;
    }

Documentation

    private static void CloseStorageRoot(StorageInfo storageRoot)
    {
        InvokeStorageRootMethod(storageRoot, "Close");
    }

    private static object InvokeStorageRootMethod(StorageInfo storageRoot, string methodName, params object[] methodArgs)
    {
        //We need the StorageRoot class to directly open an OSS file.  Unfortunately, it's internal.
        //So we'll have to use Reflection to access it.  This code was inspired by:
        //http://henbo.spaces.live.com/blog/cns!2E073207A544E12!200.entry
        //Note: In early WinFX CTPs the StorageRoot class was public because it was documented
        //here: http://msdn2.microsoft.com/en-us/library/aa480157.aspx

        Type storageRootType = typeof(StorageInfo).Assembly.GetType("System.IO.Packaging.StorageRoot", true, false);
        object result = storageRootType.InvokeMember(methodName,
            BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
            null, storageRoot, methodArgs);
        return result;
    }

    private static void WriteStorage(StorageInfo storageInfo, string storageName, XmlWriter writer)
    {
        writer.WriteStartElement("storage");
        writer.WriteAttributeString("name", storageName);

        StorageInfo[] subStorages = storageInfo.GetSubStorages();
        foreach (StorageInfo subStorage in subStorages)
        {
            WriteStorage(subStorage, subStorage.Name, writer);
        }

        StreamInfo[] streams = storageInfo.GetStreams();
        foreach (StreamInfo stream in streams)
        {
            string hexData = ConvertStreamBytesToHex(stream);
            writer.WriteStartElement("stream");
            writer.WriteAttributeString("name", stream.Name);
            writer.WriteAttributeString("data", hexData);
            writer.WriteEndElement();
        }

        writer.WriteEndElement();
    }

    private static string ConvertStreamBytesToHex(StreamInfo streamInfo)
    {
        using (Stream streamReader = streamInfo.GetStream(FileMode.Open, FileAccess.Read))
        {
            StringBuilder sb = new StringBuilder();
            int currentRead;
            while ((currentRead = streamReader.ReadByte()) >= 0)
            {
                byte currentByte = (byte)currentRead;
                sb.AppendFormat("{0:X2}", currentByte);
            }
            return sb.ToString();
        }
    }

    [DllImport("ole32.dll", CharSet = CharSet.Unicode)]
    private static extern int StgIsStorageFile(string fileName);

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions