How to write an altered COM Structured Storage file to Disk?

595 views Asked by At

I have a COM Structured Storage File reader implemented that can open Storage and stream objects, that's all happy. But now I want to be able to copy things from one archive to another and rename things and then write things back to the disk. I haven't even adressed the copy and rename operations because I can't seem to even write changes to disk. I tried calling the Root IStorage object's Commit method and it seems to run fine, but I see no changes. I'm implementing this via PInvoke in C#.NET. Here are applicable code elements:

public void Load(string path)
    {
        STATSTG[] storageElementInfos = new STATSTG[1];
        // Populate our storage object
        var result = StgOpenStorage(path, null, STGM.DIRECT | STGM.READWRITE | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out archive);
        // Only proceed if we succeed in populating our SS object
        if (result == 0)
        {
            uint count;
            IEnumSTATSTG elements;
            archive.EnumElements(0, IntPtr.Zero, 0, out elements);
            elements.Next(1, storageElementInfos, out count);
            if (count != 0)
            {
                if ((STGTY)storageElementInfos[0].type == STGTY.STGTY_STORAGE)
                {
                    //create Root Here
                    var root = storageElementInfos[0];
                    Root = new StructuredStorageContainer();
                    Root.element = root;
                    (Root as StructuredStorageContainer).Load(archive);
                }
            }
        }

    }

public void Save()
    {
        if (Root is StructuredStorageContainer)
        {
            (Root as StructuredStorageContainer).Save();
        }
        archive.Commit(0);
    }

Making the Change

        ListArchive((archive.Root as StructuredStorageContainer), 0);
        archive.Root.Name = "Fail";
        archive.Save();

The name definition on the StructuredStorageNode Class.

    internal System.Runtime.InteropServices.ComTypes.STATSTG element;
    public string Name
    {
        get
        {
            return element.pwcsName;
        }
        set
        {
            element.pwcsName = value;
        }
    }
1

There are 1 answers

0
ironfede On

I don't know if P/Invoke is a design-request for your project but if it is not and you need a quick solution you can give a try to my free and open source library for reading/writing Structured Storage files OpenMCDF.

It doesn't use P/Invoke being a 100% C# .net implementation.