Пример #1
0
        public void ReadPointer_MarshalStructure()
        {
            var intPtr = _container.IntPtr;

            var actual0 = Marshal.PtrToStructure <byte>(intPtr + 4);

            Assert.Equal(0x04, actual0);

            var actual1   = Marshal.PtrToStructure <MyStructByte3>(intPtr);
            var expected1 = new MyStructByte3(0x00, 0x01, 0x02);

            Assert.Equal(expected1, actual1);
        }
Пример #2
0
        public void WritePointer_MarshalStructure()
        {
            var intPtr = _container.IntPtr;

            Assert.Equal(0x00, Marshal.ReadByte(intPtr));

            Marshal.StructureToPtr <byte>(0xff, intPtr, fDeleteOld: false);
            Assert.Equal(0xff, Marshal.ReadByte(intPtr));

            var data = new MyStructByte3(0x77, 0x88, 0x99);

            Marshal.StructureToPtr <MyStructByte3>(data, intPtr, fDeleteOld: false);
            var actual = Marshal.ReadInt32(intPtr) & 0x00ff_ffff;

            Assert.Equal(0x0099_8877, actual);
        }
Пример #3
0
        public void WritePointer_UnsafeWrite()
        {
            var intPtr0  = _container.IntPtr;
            var intPtr1  = _container.IntPtr + 2;
            var myStruct = new MyStructByte3(0x01, 0x23, 0x45);

            unsafe
            {
                Unsafe.Write <byte>(intPtr0.ToPointer(), 0x01);
            }
            Assert.Equal(0x01, Marshal.ReadByte(intPtr0));

            unsafe
            {
                // Unalignedにする必要ない(使いたいだけ)
                Unsafe.WriteUnaligned <MyStructByte3>(intPtr1.ToPointer(), myStruct);
                Unsafe.WriteUnaligned <byte>((intPtr1 + 3).ToPointer(), 0x00);
            }
            Assert.Equal(0x00_45_23_01, Marshal.ReadInt32(intPtr1));
        }
Пример #4
0
        public void ReadPointer_Unsafe()
        {
            var intPtr0 = _container.IntPtr + 4;

            unsafe
            {
                var b = Unsafe.Read <byte>(intPtr0.ToPointer());
                Assert.Equal(0x04, b);
            }


            var intPtr1  = _container.IntPtr + 4;
            var expected = new MyStructByte3(0x04, 0x05, 0x06);

            unsafe
            {
                // Unalignedにする必要ない(使いたいだけ)
                var data = Unsafe.ReadUnaligned <MyStructByte3>(intPtr1.ToPointer());
                Assert.Equal(expected, data);
            }
        }