Fix and test for #101 - Bug when using SafeAnysizeStruct.

pull/119/head
dahall 2020-01-17 07:54:15 -07:00
parent 16aa08ec14
commit cc7170875f
2 changed files with 4 additions and 4 deletions

View File

@ -123,9 +123,9 @@ namespace Vanara.InteropServices
Size = memSz;
// Marshal base structure - don't use Write to prevent loops
Marshal.StructureToPtr(value, handle, false);
// Push each element of the array into memory
for (var i = 0; i < arrLen; i++)
handle.Write(((Array)arrVal).GetValue(i), baseSz - arrElemSz * (i - 1), memSz);
// Push each element of the array into memory, starting with second item in array since first was pushed by StructureToPtr
for (var i = 1; i < arrLen; i++)
handle.Write(((Array)arrVal).GetValue(i), baseSz + arrElemSz * (i - 1), memSz);
}
}

View File

@ -29,7 +29,7 @@ namespace Vanara.InteropServices.Tests
[Test]
public void ConvertTest()
{
var array = new[] { long.MinValue, 0L, long.MaxValue };
var array = new[] { long.MinValue, 1L, long.MaxValue };
var ts = new TestStruct { iVal = array.Length, array = array };
using var mem = new SafeAnysizeStruct<TestStruct>(ts);