示例#1
0
    static 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);
        }
        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 }
         }
        };
        bool expectedException = false;
        size = Marshal.SizeOf(structure2);
        memory = Marshal.AllocHGlobal(size);
        try
        {
            Marshal.StructureToPtr(structure2, memory, false);
        }
        catch (ArgumentException)
        {
            expectedException = true;
        }
        finally
        {
            Marshal.FreeHGlobal(memory);
        }
        if (!expectedException)
            throw new Exception("Expected ArgumentException");

        // 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);
        }
        finally
        {
            Marshal.FreeHGlobal(memory);
        }
    }
示例#2
0
    static 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);
        }
        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
                }
            }
        };
        bool expectedException = false;

        size   = Marshal.SizeOf(structure2);
        memory = Marshal.AllocHGlobal(size);
        try
        {
            Marshal.StructureToPtr(structure2, memory, false);
        }
        catch (ArgumentException)
        {
            expectedException = true;
        }
        finally
        {
            Marshal.FreeHGlobal(memory);
        }
        if (!expectedException)
        {
            throw new Exception("Expected ArgumentException");
        }

        // 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);
        }
        finally
        {
            Marshal.FreeHGlobal(memory);
        }
    }