示例#1
0
        public void StringNamedFieldATest()
        {
            var ts = new TestStrStructA {
                iVal = str.Length + 1, array = str
            };
            var strOffset = Marshal.OffsetOf <TestStrStructA>(nameof(TestStrStructA.array)).ToInt64();

            IVanaraMarshaler m = new AnySizeStringMarshaler <TestStrStructA>(nameof(TestStrStructA.iVal));

            using (var mem = m.MarshalManagedToNative(ts))
            {
                Assert.That((long)mem.Size, Is.GreaterThanOrEqualTo(strOffset + (str.Length + 1)));
                Assert.That(StringHelper.GetString(((IntPtr)mem).Offset(strOffset), CharSet.Ansi), Is.EqualTo(str));

                var tsout = (TestStrStructA)m.MarshalNativeToManaged(mem, mem.Size);
                Assert.That(tsout.iVal, Is.EqualTo(ts.iVal));
                Assert.That(tsout.array, Is.EquivalentTo(str));
            }

            m = new AnySizeStringMarshaler <TestStrStructA>(nameof(TestStrStructA.iVal) + ":br");
            using (var mem = m.MarshalManagedToNative(ts))
            {
                var newStr = str.Substring(0, Math.Min(ts.iVal, str.Length));
                Assert.That((long)mem.Size, Is.GreaterThanOrEqualTo(strOffset + ts.iVal));
                Assert.That(StringHelper.GetString(((IntPtr)mem).Offset(strOffset), CharSet.Ansi), Is.EqualTo(newStr));

                var tsout = (TestStrStructA)m.MarshalNativeToManaged(mem, mem.Size);
                Assert.That(tsout.iVal, Is.EqualTo(ts.iVal));
                Assert.That(tsout.array, Is.EquivalentTo(newStr));
            }
        }
示例#2
0
        public void StringNoFieldTest()
        {
            var ts = new TestStrStructU {
                iVal = str.Length + 1, array = str
            };
            var strOffset = Marshal.OffsetOf <TestStrStructU>(nameof(TestStrStructU.array)).ToInt64();

            IVanaraMarshaler m = new AnySizeStringMarshaler <TestStrStructU>("*");

            using var mem = m.MarshalManagedToNative(ts);

            Assert.That((long)mem.Size, Is.GreaterThanOrEqualTo(strOffset + (str.Length + 1) * StringHelper.GetCharSize(CharSet.Unicode)));
            Assert.That(StringHelper.GetString(((IntPtr)mem).Offset(strOffset), CharSet.Unicode), Is.EqualTo(str));

            var tsout = (TestStrStructU)m.MarshalNativeToManaged(mem, mem.Size);

            Assert.That(tsout.iVal, Is.EqualTo(ts.iVal));
            Assert.That(tsout.array, Is.EquivalentTo(str));
        }