示例#1
0
        public unsafe void EnumStorage()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string   path       = cleaner.GetTestPath();
                IStorage storage    = (IStorage)ComMethods.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.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);
            }
        }
示例#2
0
        public unsafe void RenameStream()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string   path    = cleaner.GetTestPath();
                IStorage storage = (IStorage)ComMethods.CreateStorage(path, InterfaceIds.IID_IStorage);
                IStream  stream  = storage.CreateStream("name", StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive);
                stream.Should().NotBeNull();

                stream.Stat(out var stats, StatFlag.Default);
                stats.GetAndFreeString().Should().Be("name");
                stats.type.Should().Be(StorageType.Stream);

                Action action = () => storage.RenameElement("name", "newname");

                // Can't rename until after we close the stream
                action.Should().Throw <COMException>().And.HResult.Should().Be((int)HResult.STG_E_ACCESSDENIED);
                Marshal.ReleaseComObject(stream);
                action();

                action = () => stream = storage.OpenStream("name", IntPtr.Zero, StorageMode.ShareExclusive);
                action.Should().Throw <COMException>().And.HResult.Should().Be((int)HResult.STG_E_FILENOTFOUND);

                stream = storage.OpenStream("newname", IntPtr.Zero, StorageMode.ShareExclusive);
                stream.Should().NotBeNull();
            }
        }
示例#3
0
 public unsafe void IsIStorage()
 {
     using (var cleaner = new TestFileCleaner())
     {
         string path    = cleaner.GetTestPath();
         object created = ComMethods.CreateStorage(path, InterfaceIds.IID_IStorage);
         ComMethods.IsStorageFile(path).Should().BeTrue();
     }
 }
示例#4
0
        public unsafe void CreateStream()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string   path    = cleaner.GetTestPath();
                IStorage storage = (IStorage)ComMethods.CreateStorage(path, InterfaceIds.IID_IStorage);
                IStream  stream  = storage.CreateStream("mystream", StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive);
                stream.Should().NotBeNull();

                stream.Stat(out var stats, StatFlag.Default);
                stats.GetAndFreeString().Should().Be("mystream");
                stats.type.Should().Be(StorageType.Stream);
            }
        }
示例#5
0
        public unsafe void CreateIStorage()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string path    = cleaner.GetTestPath();
                object created = ComMethods.CreateStorage(path, InterfaceIds.IID_IStorage);
                created.Should().NotBeNull();
                IStorage storage = created as IStorage;
                storage.Should().NotBeNull();
                Storage.FileExists(path).Should().BeTrue();

                storage.Stat(out var stats, StatFlag.Default);
                stats.GetAndFreeString().Should().Be(path);
                stats.type.Should().Be(StorageType.Storage);
                stats.clsid.Should().Be(Guid.Empty, "should be null for newly created objects");
            }
        }
示例#6
0
        public unsafe void OpenIStorage()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string   path    = cleaner.GetTestPath();
                IStorage storage = (IStorage)ComMethods.CreateStorage(path, InterfaceIds.IID_IStorage);
                Guid     guid    = new Guid();
                storage.SetClass(ref guid);
                storage.Commit(StorageCommit.Default);
                Marshal.ReleaseComObject(storage);
                storage = (IStorage)ComMethods.OpenStorage(path, InterfaceIds.IID_IStorage);
                storage.Should().NotBeNull();

                storage.Stat(out var stats, StatFlag.Default);
                stats.GetAndFreeString().Should().Be(path);
                stats.type.Should().Be(StorageType.Storage);
                stats.clsid.Should().Be(guid);
            }
        }
示例#7
0
        public unsafe void ComStreamTextReadWrite()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string   path    = cleaner.GetTestPath();
                IStorage storage = (IStorage)ComMethods.CreateStorage(path, InterfaceIds.IID_IStorage);

                ComStream stream;
                using (stream = new ComStream(storage.CreateStream("mystream", StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive)))
                {
                    using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode, 1024, leaveOpen: true))
                    {
                        writer.WriteLine("This is line one.");
                        stream.Length.Should().Be(0);
                        stream.Position.Should().Be(0);
                        writer.Flush();
                        stream.Length.Should().Be(40);
                        stream.Position.Should().Be(40);
                        writer.WriteLine("This is line two.");
                        stream.Length.Should().Be(40);
                        stream.Position.Should().Be(40);
                        writer.Flush();
                        stream.Length.Should().Be(78);
                        stream.Position.Should().Be(78);
                    }

                    stream.Stream.Should().NotBeNull();

                    using (StreamReader reader = new StreamReader(stream, Encoding.Unicode, detectEncodingFromByteOrderMarks: false, 1024, leaveOpen: true))
                    {
                        stream.Position = 0;
                        stream.Position.Should().Be(0);
                        reader.ReadLine().Should().Be("This is line one.");
                        reader.ReadLine().Should().Be("This is line two.");
                    }

                    stream.Stream.Should().NotBeNull();
                }

                stream.Stream.Should().BeNull();
            }
        }
示例#8
0
        public unsafe void ComStreamConstruction()
        {
            using var cleaner = new TestFileCleaner();
            string   path    = cleaner.GetTestPath();
            IStorage storage = (IStorage)ComMethods.CreateStorage(path, InterfaceIds.IID_IStorage);

            ComStream stream;

            using (stream = new ComStream(storage.CreateStream("mystream", StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive)))
            {
                stream.ToString().Should().Be("mystream");
                stream.StorageMode.Should().Be(StorageMode.ReadWrite | StorageMode.ShareExclusive);
                stream.StorageType.Should().Be(StorageType.Stream);
                stream.CanRead.Should().BeTrue();
                stream.CanSeek.Should().BeTrue();
                stream.CanWrite.Should().BeTrue();
                stream.Length.Should().Be(0);
                stream.Position.Should().Be(0);
            }
        }