Beispiel #1
0
        private IEnumerable <System.Runtime.InteropServices.ComTypes.STATSTG> EnumElements()
        {
            IEnumSTATSTG ssenum = null;

            m_Storage.EnumElements(0, IntPtr.Zero, 0, out ssenum);

            var ssstruct = new System.Runtime.InteropServices.ComTypes.STATSTG[1];

            uint numReturned;

            do
            {
                ssenum.Next(1, ssstruct, out numReturned);

                if (numReturned != 0)
                {
                    yield return(ssstruct[0]);
                }
            } while (numReturned > 0);
        }
Beispiel #2
0
        internal static int EnumElements(this IStorage storage, out System.Runtime.InteropServices.ComTypes.STATSTG[] elements)
        {
            HRESULT      hr         = HRESULT.E_FAIL;
            IEnumSTATSTG enumerator = null;

            elements = new System.Runtime.InteropServices.ComTypes.STATSTG[] { };
            List <System.Runtime.InteropServices.ComTypes.STATSTG> list = new List <System.Runtime.InteropServices.ComTypes.STATSTG>();

            try
            {
                if (NativeMethods.Succeeded(hr = storage.EnumElements(0, IntPtr.Zero, 0, out enumerator)))
                {
                    uint celt = 1;
                    System.Runtime.InteropServices.ComTypes.STATSTG[] statstg = new System.Runtime.InteropServices.ComTypes.STATSTG[1];
                    uint fetched = 0;

                    while ((NativeMethods.Succeeded(hr = enumerator.Next(celt, statstg, out fetched))) && (fetched > 0))
                    {
                        list.Add(statstg[0]);
                    }

                    elements = list.ToArray();
                }
            }
            catch
            {
            }
            finally
            {
                if (enumerator != null)
                {
                    enumerator.FinalRelease();
                }
            }

            return(0);
        }
Beispiel #3
0
        public unsafe void EnumStorage()
        {
            using var cleaner = new TestFileCleaner();
            string   path       = cleaner.GetTestPath();
            IStorage storage    = (IStorage)Com.CreateStorage(path, InterfaceIds.IID_IStorage);
            IStream  stream     = storage.CreateStream("mystream", StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive);
            IStorage subStorage = storage.CreateStorage("substorage", StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive);

            storage.Commit();

            IEnumSTATSTG e = storage.EnumElements();

            WInterop.Com.Native.STATSTG stat = default;
            int count = 0;

            while (e.Next(1, ref stat) > 0)
            {
                count++;
                switch (stat.type)
                {
                case StorageType.Storage:
                    stat.GetAndFreeString().Should().Be("substorage");
                    break;

                case StorageType.Stream:
                    stat.GetAndFreeString().Should().Be("mystream");
                    break;

                default:
                    false.Should().BeTrue($"unexpected type {stat.type}");
                    break;
                }
            }

            count.Should().Be(2);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string filename = @"f:\temp\treta2.msg";

            if (StgIsStorageFile(filename) == 0)
            {
                IStorage storage = null;
                if (StgOpenStorage(
                        filename,
                        null,
                        STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE,
                        IntPtr.Zero,
                        0,
                        out storage) == 0)
                {
                    System.Runtime.InteropServices.ComTypes.STATSTG statstg;
                    storage.Stat(out statstg, (uint)STATFLAG.STATFLAG_DEFAULT);

                    IEnumSTATSTG pIEnumStatStg = null;
                    storage.EnumElements(0, IntPtr.Zero, 0, out pIEnumStatStg);

                    System.Runtime.InteropServices.ComTypes.STATSTG[] regelt = { statstg };
                    uint fetched = 0;
                    uint res     = pIEnumStatStg.Next(1, regelt, out fetched);

                    if (res == 0)
                    {
                        while (res != 1)
                        {
                            string strNode    = statstg.pwcsName;
                            bool   bNodeFound = false;

                            Console.WriteLine(strNode);

                            if (strNode == "__substg1.0_0E04001E" ||
                                strNode == "__substg1.0_0E1D001E" ||
                                strNode == "__substg1.0_1000001E" ||
                                strNode == "__substg1.0_1013001E")
                            {
                                bNodeFound = true;
                            }

                            if (bNodeFound)
                            {
                                switch (statstg.type)
                                {
                                case (int)STGTY.STGTY_STORAGE:
                                {
                                    IStorage pIChildStorage;
                                    storage.OpenStorage(statstg.pwcsName,
                                                        null,
                                                        (uint)(STGM.READ | STGM.SHARE_EXCLUSIVE),
                                                        IntPtr.Zero,
                                                        0,
                                                        out pIChildStorage);
                                }
                                break;

                                case (int)STGTY.STGTY_STREAM:
                                {
                                    IStream pIStream;
                                    storage.OpenStream(statstg.pwcsName,
                                                       IntPtr.Zero,
                                                       (uint)(STGM.READ | STGM.SHARE_EXCLUSIVE),
                                                       0,
                                                       out pIStream);

                                    byte[] data = new byte[255];

                                    pIStream.Read(data, 255, IntPtr.Zero);
                                }
                                break;
                                }
                            }

                            res = pIEnumStatStg.Next(1, regelt, out fetched);
                        }
                    }
                }
            }

            Console.ReadLine();
        }
Beispiel #5
0
        public static void OpenAndExtractStorages(string filename, string dir)
        {
            IStorage rootStorage = null;
            int      hresult     = Ole32.StgOpenStorage(filename, null, STGM.STGM_READ | STGM.STGM_SHARE_EXCLUSIVE, IntPtr.Zero, 0, out rootStorage);

            if ((hresult == S_OK) && (rootStorage != null))
            {
                if (IsPatch(rootStorage))
                {
                    try
                    {
                        IEnumSTATSTG rootStorageEnum = null;
                        rootStorage.EnumElements(0, IntPtr.Zero, 0, out rootStorageEnum);

                        STATSTG[] enumStg    = new STATSTG[] { new STATSTG() };
                        uint      numFetched = 0;
                        rootStorageEnum.Next(1, enumStg, out numFetched);

                        while (numFetched == 1)
                        {
                            if (enumStg[0].type == (uint)STGTY.STGTY_STORAGE)
                            {
                                // Save the nested transform storages with an .mst extension
                                SaveStorage(rootStorage, dir, enumStg[0].pwcsName, ".mst");
                            }

                            rootStorageEnum.Next(1, enumStg, out numFetched);
                        }

                        if (enumStg != null)
                        {
                            Marshal.ReleaseComObject(rootStorageEnum);
                        }

                        if (rootStorage != null)
                        {
                            Marshal.ReleaseComObject(rootStorage);
                        }

                        using (Database installDatabase = new Database(filename, DatabaseOpenMode.ReadOnly))
                            using (View view = installDatabase.OpenView("SELECT `Name`, `Data` FROM `_Streams`"))
                            {
                                view.Execute();

                                foreach (Record record in view)
                                {
                                    SaveStream(record, dir);
                                    record.Close();
                                }
                            }
                    }
                    finally
                    {
                        if (rootStorage != null)
                        {
                            Marshal.ReleaseComObject(rootStorage);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        public static List <string> GetUsers(string filename)
        {
            List <string> users = new List <string>();

            if (StgIsStorageFile(filename) == 0)
            {
                IStorage storage = null;

                if (StgOpenStorage(
                        filename,
                        null,
                        STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE,
                        IntPtr.Zero,
                        0,
                        out storage) == 0)
                {
                    System.Runtime.InteropServices.ComTypes.STATSTG statstg;
                    storage.Stat(out statstg, (uint)STATFLAG.STATFLAG_DEFAULT);
                    IEnumSTATSTG pIEnumStatStg = null;
                    storage.EnumElements(0, IntPtr.Zero, 0, out pIEnumStatStg);
                    System.Runtime.InteropServices.ComTypes.STATSTG[] regelt = { statstg };
                    uint fetched = 0;
                    uint res     = pIEnumStatStg.Next(1, regelt, out fetched);
                    if (res == 0)
                    {
                        while (res != 1)
                        {
                            string strNode    = statstg.pwcsName;
                            bool   bNodeFound = false;
                            if (strNode.IndexOf("Container.Contents") != -1)
                            {
                                bNodeFound = true;
                            }
                            if (bNodeFound)
                            {
                                switch (statstg.type)
                                {
                                case (int)STGTY.STGTY_STORAGE:
                                {
                                    IStorage pIChildStorage;
                                    storage.OpenStorage(statstg.pwcsName,
                                                        null,
                                                        (uint)(STGM.READ | STGM.SHARE_EXCLUSIVE),
                                                        IntPtr.Zero,
                                                        0,
                                                        out pIChildStorage);
                                }
                                break;

                                case (int)STGTY.STGTY_STREAM:
                                {
                                    IStream pIStream;
                                    storage.OpenStream(statstg.pwcsName,
                                                       IntPtr.Zero,
                                                       (uint)(STGM.READ | STGM.SHARE_EXCLUSIVE),
                                                       0,
                                                       out pIStream);
                                    if (statstg.cbSize > 0)
                                    {
                                        byte[] data = new byte[statstg.cbSize];
                                        pIStream.Read(data, (int)statstg.cbSize, IntPtr.Zero);
                                        Encoding en     = Encoding.Default;
                                        string   tmpStr = en.GetString(data);

                                        int pos = tmpStr.IndexOf("Page");
                                        while (pos > 0)
                                        {
                                            tmpStr = tmpStr.Remove(0, pos);
                                            pos    = tmpStr.IndexOf(",\"");
                                            if (pos > 0)
                                            {
                                                tmpStr = tmpStr.Remove(0, pos + 2);
                                                users.Add(tmpStr.Substring(0, tmpStr.IndexOf('"')));
                                            }
                                            pos = tmpStr.IndexOf("Page");
                                        }
                                    }
                                    pIStream = null;
                                }
                                break;
                                }
                            }
                            if ((res = pIEnumStatStg.Next(1, regelt, out fetched)) != 1)
                            {
                                statstg = regelt[0];
                            }
                        }
                        storage = null;
                    }
                }
            }
            return(users);
        }