Esempio n. 1
0
        public void MarshalTheValueToAPointer()
        {
            var lpStubStructure = IntPtr.Zero;
            var target          = new MarshallingService();

            try
            {
                var sizeOfStubStructure = Marshal.SizeOf <StubStructure>();
                var value = new StubStructure
                {
                    Field1 = 1,
                    Field2 = 2,
                    Field3 = 3,
                    Field4 = "4"
                };

                lpStubStructure = Marshal.AllocHGlobal(sizeOfStubStructure);
                target.StructureToPtr(value, lpStubStructure);

                var result = Marshal.PtrToStructure <StubStructure>(lpStubStructure);

                Assert.AreEqual(1, result.Field1);
                Assert.AreEqual(2, result.Field2);
                Assert.AreEqual(3, result.Field3);
                Assert.AreEqual("4", result.Field4);
            }
            finally
            {
                if (lpStubStructure != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpStubStructure);
                }
            }
        }
Esempio n. 2
0
        public void ThrowAnExceptionWhenThePtrIsZero()
        {
            var target = new MarshallingService();

            Assert.Throws <ArgumentNullException>(() => target.StructureToPtr(new StubStructure(), IntPtr.Zero));
        }