Example #1
0
        public void ComEnumeratorTest5()
        {
            // Test IntPtr -> string conversion
            var fn = System.IO.Path.GetTempFileName();
            var hr = StgCreateStorageEx(fn,
                                        STGM.STGM_DELETEONRELEASE | STGM.STGM_CREATE | STGM.STGM_DIRECT | STGM.STGM_READWRITE | STGM.STGM_SHARE_EXCLUSIVE,
                                        STGFMT.STGFMT_DOCFILE, 0, IntPtr.Zero, IntPtr.Zero, typeof(IStorage).GUID, out object iptr);

            Assert.That(hr, Is.EqualTo((HRESULT)HRESULT.S_OK));
            var istg = (IStorage)iptr;

            Assert.That(istg, Is.InstanceOf <IStorage>());
            var istgc = istg.CreateStorage("temp",
                                           STGM.STGM_CREATE | STGM.STGM_DIRECT | STGM.STGM_WRITE | STGM.STGM_SHARE_EXCLUSIVE, 0, 0);

            istgc.Commit(STGC.STGC_DEFAULT);
            var istr = istg.CreateStream("stream1",
                                         STGM.STGM_CREATE | STGM.STGM_DIRECT | STGM.STGM_WRITE | STGM.STGM_SHARE_EXCLUSIVE, 0, 0);
            var strb = "Some string text".GetBytes();

            istr.Write(strb, strb.Length, IntPtr.Zero);
            istr.Commit((int)STGC.STGC_DEFAULT);
            istg.Commit(STGC.STGC_DEFAULT);
            var ee = istg.EnumElements(0, IntPtr.Zero, 0);

            // Test IEnumerable
            bool Next(out STATSTG p)
            {
                var a = new STATSTG[1];
                var b = ee.Next(1, a, out var f).Succeeded&& f == 1;

                p = b ? a[0] : default(STATSTG);
                return(b);
            }

            var e = new IEnumFromNext <STATSTG>(Next, ee.Reset);

            foreach (var p in e)
            {
                //Assert.That(p.cbSize, Is.Not.Zero);
                TestContext.WriteLine(p.pwcsName);
            }

            // Test IEnumerator
            var g = e.GetEnumerator();

            g.Reset();
            Assert.That(g.MoveNext(), Is.True);
            g.Dispose();
            Assert.That(g.MoveNext(), Is.False);
        }
Example #2
0
        public void ComEnumeratorTest6()
        {
            var pidls = new[]
            {
                KNOWNFOLDERID.FOLDERID_Documents.PIDL(), KNOWNFOLDERID.FOLDERID_Pictures.PIDL(),
                KNOWNFOLDERID.FOLDERID_Videos.PIDL()
            }.Select(p => (IntPtr)p).ToArray();
            var hr = SHCreateShellItemArrayFromIDLists((uint)pidls.Length, pidls, out IShellItemArray iarr);

            Assert.That(hr, Is.EqualTo((HRESULT)HRESULT.S_OK));
            var ei = iarr.EnumItems();

            // Test IEnumerable
            bool Next(out IShellItem p)
            {
                var a = new IShellItem[1];
                var b = ei.Next(1, a, out var f).Succeeded&& f == 1;

                p = b ? a[0] : null;
                return(b);
            }

            var e = new IEnumFromNext <IShellItem>(Next, ei.Reset);

            foreach (var p in e)
            {
                Assert.That(p, Is.Not.Null);
                TestContext.WriteLine(p.GetDisplayName(SIGDN.SIGDN_PARENTRELATIVE));
                Marshal.ReleaseComObject(p);
            }

            // Test IEnumerator
            var g = e.GetEnumerator();

            g.Reset();
            Assert.That(g.MoveNext(), Is.True);
            g.Dispose();
            Assert.That(g.MoveNext(), Is.False);
        }