Пример #1
0
    public static int test_0_byvaltstr()
    {
        ByValTStrStruct s = new ByValTStrStruct();

        IntPtr p2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ByValTStrStruct)));

        Marshal.StructureToPtr(s, p2, false);

        /* Check that the ByValTStr is initialized correctly */
        for (int i = 0; i < 4; ++i)
        {
            if (Marshal.ReadByte(p2, i) != 0)
            {
                return(31);
            }
        }

        s.s1 = "ABCD";
        s.i  = 55;

        Marshal.StructureToPtr(s, p2, false);

        ByValTStrStruct s2 = (ByValTStrStruct)Marshal.PtrToStructure(p2, typeof(ByValTStrStruct));

        /* The fourth char is lost because of null-termination */
        if (s2.s1 != "ABC")
        {
            return(32);
        }

        if (s2.i != 55)
        {
            return(33);
        }

        // Check that decoding also respects the size, even when there is no null terminator
        byte[] data   = Encoding.ASCII.GetBytes("ABCDXXXX");
        int    size   = Marshal.SizeOf(typeof(ByValTStrStruct));
        IntPtr buffer = Marshal.AllocHGlobal(size);

        Marshal.Copy(data, 0, buffer, size);

        s2 = (ByValTStrStruct)Marshal.PtrToStructure(buffer, typeof(ByValTStrStruct));
        if (s2.s1 != "ABC")
        {
            return(34);
        }

        return(0);
    }
Пример #2
0
	public static int test_0_byvaltstr () {
		ByValTStrStruct s = new ByValTStrStruct ();

		IntPtr p2 = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (ByValTStrStruct)));
		Marshal.StructureToPtr(s, p2, false);

		/* Check that the ByValTStr is initialized correctly */
		for (int i = 0; i < 4; ++i)
			if (Marshal.ReadByte (p2, i) != 0)
				return 31;

		s.s1 = "ABCD";
		s.i = 55;

		Marshal.StructureToPtr(s, p2, false);

		ByValTStrStruct s2 = (ByValTStrStruct)Marshal.PtrToStructure (p2, typeof (ByValTStrStruct));

		/* The fourth char is lost because of null-termination */
		if (s2.s1 != "ABC")
			return 32;

		if (s2.i != 55)
			return 33;

		// Check that decoding also respects the size, even when there is no null terminator
		byte[] data = Encoding.ASCII.GetBytes ("ABCDXXXX");
		int size = Marshal.SizeOf (typeof (ByValTStrStruct));
		IntPtr buffer = Marshal.AllocHGlobal (size);
		Marshal.Copy (data, 0, buffer, size);

		s2 = (ByValTStrStruct)Marshal.PtrToStructure (buffer, typeof (ByValTStrStruct));
		if (s2.s1 != "ABC")
			return 34;

		return 0;
	}