Esempio n. 1
0
        public void StructureToPtr_InvalidLengthByValArrayInStruct_ThrowsArgumentException()
        {
            var structure = new StructWithByValArray
            {
                array = new StructWithIntField[]
                {
                    new StructWithIntField {
                        value = 1
                    },
                    new StructWithIntField {
                        value = 2
                    },
                    new StructWithIntField {
                        value = 3
                    },
                    new StructWithIntField {
                        value = 4
                    }
                }
            };
            int    size   = Marshal.SizeOf(structure);
            IntPtr memory = Marshal.AllocHGlobal(size);

            try
            {
                Assert.Throws <ArgumentException>(() => Marshal.StructureToPtr(structure, memory, false));
                Assert.Throws <ArgumentException>(() => Marshal.StructureToPtr(structure, memory, true));
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }
Esempio n. 2
0
        public void StructureToPtr_OverflowByValArrayInStruct_Success()
        {
            var structure = new StructWithByValArray()
            {
                array = new StructWithIntField[]
                {
                    new StructWithIntField {
                        value = 1
                    },
                    new StructWithIntField {
                        value = 2
                    },
                    new StructWithIntField {
                        value = 3
                    },
                    new StructWithIntField {
                        value = 4
                    },
                    new StructWithIntField {
                        value = 5
                    },
                    new StructWithIntField {
                        value = 6
                    }
                }
            };

            int    size   = Marshal.SizeOf(structure);
            IntPtr memory = Marshal.AllocHGlobal(size);

            try
            {
                Marshal.StructureToPtr(structure, memory, false);
                Marshal.StructureToPtr(structure, memory, true);
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }
Esempio n. 3
0
        public void VerifyByValArrayInStruct()
        {
            // equal
            var structure1 = new StructWithByValArray()
            {
                array = new StructWithIntField[]
                {
                    new StructWithIntField {
                        value = 1
                    },
                    new StructWithIntField {
                        value = 2
                    },
                    new StructWithIntField {
                        value = 3
                    },
                    new StructWithIntField {
                        value = 4
                    },
                    new StructWithIntField {
                        value = 5
                    }
                }
            };
            int    size   = Marshal.SizeOf(structure1);
            IntPtr memory = Marshal.AllocHGlobal(size);

            try
            {
                Marshal.StructureToPtr(structure1, memory, false);
                Marshal.StructureToPtr(structure1, memory, true);
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            // underflow
            var structure2 = new StructWithByValArray()
            {
                array = new StructWithIntField[]
                {
                    new StructWithIntField {
                        value = 1
                    },
                    new StructWithIntField {
                        value = 2
                    },
                    new StructWithIntField {
                        value = 3
                    },
                    new StructWithIntField {
                        value = 4
                    }
                }
            };

            size   = Marshal.SizeOf(structure2);
            memory = Marshal.AllocHGlobal(size);
            try
            {
                Assert.Throws <ArgumentException>(() => Marshal.StructureToPtr(structure2, memory, false));
                Assert.Throws <ArgumentException>(() => Marshal.StructureToPtr(structure2, memory, true));
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            // overflow
            var structure3 = new StructWithByValArray()
            {
                array = new StructWithIntField[]
                {
                    new StructWithIntField {
                        value = 1
                    },
                    new StructWithIntField {
                        value = 2
                    },
                    new StructWithIntField {
                        value = 3
                    },
                    new StructWithIntField {
                        value = 4
                    },
                    new StructWithIntField {
                        value = 5
                    },
                    new StructWithIntField {
                        value = 6
                    }
                }
            };

            size   = Marshal.SizeOf(structure3);
            memory = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.StructureToPtr(structure3, memory, false);
                Marshal.StructureToPtr(structure3, memory, true);
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }
        }